Completed
Push — master ( 3bec47...6f7c35 )
by Mathieu
01:33
created

Cache   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 19
loc 57
rs 10
wmc 8
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 19 19 5
A getInstance() 0 5 1
A set() 0 5 1
A get() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Suricate;
3
4
/**
5
 * Cache
6
 *
7
 * @package Suricate
8
 * @author  Mathieu LESNIAK <[email protected]>
9
 *
10
 * @property string $type
11
 */
12
13
class Cache extends Service implements Interfaces\ICache
14
{
15
    protected $parametersList = array('type');
16
    public static $container;
17
18 View Code Duplication
    protected function init()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
    {
20
        if (static::$container === null) {
21
            switch ($this->type) {
22
                case 'memcache':
23
                    static::$container = Suricate::CacheMemcache(true);
24
                    break;
25
                case 'apc':
26
                    static::$container = Suricate::CacheApc(true);
27
                    break;
28
                case 'file':
29
                    static::$container = Suricate::CacheFile(true);
30
                    break;
31
                default:
32
                    throw new \Exception("Unknown cache type " . $this->type);
33
                    break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
34
            }
35
        }
36
    }
37
38
    public function getInstance()
39
    {
40
        $this->init();
41
        return static::$container;
42
    }
43
44
    /**
45
     * Setter
46
     * Put a variable in cache
47
     * @param string $variable The key that will be associated with the item.
48
     * @param mixed $value    The variable to store.
49
     * @param int $expiry   Expiration time of the item. If it's equal to zero, the item will never expire.
50
     *                      You can also use Unix timestamp or a number of seconds starting from current time,
51
     *                      but in the latter case the number of seconds may not exceed 2592000 (30 days).
52
     */
53
    public function set($variable, $value, $expiry = null)
54
    {
55
        $this->init();
56
        return static::$container->set($variable, $value, $expiry);
57
    }
58
59
    /**
60
     * Get a variable from cache
61
     * @param  string $variable The key to fetch
62
     * @return mixed           Data fetched from cache, false if not found
63
     */
64
    public function get($variable)
65
    {
66
        $this->init();
67
        return static::$container->get($variable);
68
    }
69
}
70