StatsCache   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 104
Duplicated Lines 19.23 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 1
dl 20
loc 104
ccs 0
cts 66
cp 0
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 4
A setRound() 0 3 1
A getRound() 0 3 1
A set() 3 7 3
A setStaticCache() 3 9 4
A get() 7 10 3
A getStatic() 7 10 3
A setCache() 0 4 2
A __call() 0 5 2

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
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
3
4
/**
5
 * A wrapper class used to store values transparently in memcache
6
 * Can be enabled or disabled through site configuration
7
 * Also sets a default time if no time is passed to it to enforce caching
8
 **/
9
class StatsCache {
10
  private $cache, $round;
11
12
  public function __construct($config, $debug) {
13
    $this->config = $config;
0 ignored issues
show
Bug introduced by
The property config does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
14
    $this->debug = $debug;
0 ignored issues
show
Bug introduced by
The property debug does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
15
    if (! $config['memcache']['enabled'] ) {
16
      $this->debug->append("Not storing any values in memcache");
17
    } else {
18
      if (PHP_OS == 'WINNT') {
19
        require_once(CLASS_DIR . '/memcached.class.php');
20
      }
21
      $this->cache = new Memcached();
22
      if ($config['memcache']['sasl'] === true) {
23
        $this->cache->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
0 ignored issues
show
Bug introduced by
The method setOption() does not seem to exist on object<Memcached>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
24
        $this->cache->setSaslAuthData($config['memcache']['sasl']['username'], $config['memcache']['sasl']['password']);
0 ignored issues
show
Bug introduced by
The method setSaslAuthData() does not seem to exist on object<Memcached>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
25
      }
26
    }
27
  }
28
29
  public function setRound($round_id) {
30
    $this->round = $round_id;
31
  }
32
  public function getRound() {
33
    return $this->round;
34
  }
35
36
  /**
37
   * Wrapper around memcache->set
38
   * Do not store values if memcache is disabled
39
   **/
40
  public function set($key, $value, $expiration=NULL) {
41
    if (! $this->config['memcache']['enabled']) return $value;
42 View Code Duplication
    if (empty($expiration))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
43
      $expiration = $this->config['memcache']['expiration'] + rand( -$this->config['memcache']['splay'], $this->config['memcache']['splay']);
44
    $this->debug->append("Storing " . $this->getRound() . '_' . $this->config['memcache']['keyprefix'] . "$key with expiration $expiration", 3);
45
    return $this->cache->set($this->getRound() . '_' . $this->config['memcache']['keyprefix'] . $key, $value, $expiration);
46
  }
47
48
  /**
49
   * Special memcache->set call bypassing any auto-expiration systems
50
   * Can be used as a static, auto-updated cache via crons
51
   **/
52
  public function setStaticCache($key, $value, $expiration=NULL) {
53
    if (! $this->config['memcache']['enabled']) return $value;
54 View Code Duplication
    if (empty($expiration))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
55
      $expiration = $this->config['memcache']['expiration'] + rand( -$this->config['memcache']['splay'], $this->config['memcache']['splay']);
56
    $this->debug->append("Storing " . $this->config['memcache']['keyprefix'] . "$key with expiration $expiration", 3);
57
    if ($this->cache->set($this->config['memcache']['keyprefix'] . $key, $value, $expiration))
58
      return $value;
59
    return false;
60
  }
61
62
  /**
63
   * Wrapper around memcache->get
64
   * Always return false if memcache is disabled
65
   **/
66
  public function get($key, $cache_cb = NULL, &$cas_token = NULL) {
0 ignored issues
show
Unused Code introduced by
The parameter $cache_cb is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $cas_token is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
    if (! $this->config['memcache']['enabled']) return false;
68
    $this->debug->append("Trying to fetch key " . $this->getRound() . '_' . $this->config['memcache']['keyprefix'] . "$key from cache", 3);
69 View Code Duplication
    if ($data = $this->cache->get($this->getRound() . '_' . $this->config['memcache']['keyprefix'].$key)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
70
      $this->debug->append("Found key in cache", 3);
71
      return $data;
72
    } else {
73
      $this->debug->append("Key not found", 3);
74
    }
75
  }
76
77
  /**
78
   * As the static set call, we try to fetch static data here
79
   **/
80
  public function getStatic($key, $cache_cb = NULL, &$cas_token = NULL) {
0 ignored issues
show
Unused Code introduced by
The parameter $cache_cb is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $cas_token is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
    if (! $this->config['memcache']['enabled']) return false;
82
    $this->debug->append("Trying to fetch key " . $this->config['memcache']['keyprefix'] . "$key from cache", 3);
83 View Code Duplication
    if ($data = $this->cache->get($this->config['memcache']['keyprefix'].$key)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
84
      $this->debug->append("Found key in cache", 3);
85
      return $data;
86
    } else {
87
      $this->debug->append("Key not found", 3);
88
    }
89
  }
90
91
  /**
92
   * Another wrapper, we want to store data in memcache and return the actual data
93
   * for further processing
94
   * @param key string Our memcache key
95
   * @param data mixed Our data to store in Memcache
96
   * @param expiration time Our expiration time, see Memcached documentation
97
   * @return data mixed Return our stored data unchanged
98
   **/
99
  public function setCache($key, $data, $expiration=NULL) {
100
    if ($this->config['memcache']['enabled']) $this->set($key, $data, $expiration);
101
    return $data;
102
  }
103
104
  /**
105
   * This method is invoked if the called method was not realised in this class
106
   **/
107
  public function __call($name, $arguments) {
108
    if (! $this->config['memcache']['enabled']) return false;
109
    //Invoke method $name of $this->cache class with array of $arguments
110
    return call_user_func_array(array($this->cache, $name), $arguments);
111
  }
112
}
113
114
$memcache = new StatsCache($config, $debug);
115
$memcache->addServer($config['memcache']['host'], $config['memcache']['port']);
0 ignored issues
show
Documentation Bug introduced by
The method addServer does not exist on object<StatsCache>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
116
// Now we can set our additional key prefix
117
if ($aTmpBlock = $block->getLast()) {
118
  $iRoundId = $aTmpBlock['id'];
119
} else {
120
  $iRoundId = 0;
121
}
122
$memcache->setRound($iRoundId);
123