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 |
||
12 | class AdapterMemcached implements iAdapter |
||
13 | { |
||
14 | /** |
||
15 | * @var bool |
||
16 | */ |
||
17 | public $installed = false; |
||
18 | |||
19 | /** |
||
20 | * @var \Memcached |
||
21 | */ |
||
22 | private $memcached; |
||
23 | |||
24 | /** |
||
25 | * __construct |
||
26 | * |
||
27 | * @param \Memcached|null $memcached |
||
28 | */ |
||
29 | 9 | public function __construct($memcached = null) |
|
30 | { |
||
31 | 9 | if ($memcached instanceof \Memcached) { |
|
|
|||
32 | 9 | $this->setMemcached($memcached); |
|
33 | 9 | } |
|
34 | 9 | } |
|
35 | |||
36 | /** |
||
37 | * @param \Memcached $memcached |
||
38 | */ |
||
39 | 9 | public function setMemcached(\Memcached $memcached) { |
|
40 | 9 | $this->memcached = $memcached; |
|
41 | 9 | $this->installed = true; |
|
42 | |||
43 | 9 | $this->setSettings(); |
|
44 | 9 | } |
|
45 | |||
46 | /** |
||
47 | * @inheritdoc |
||
48 | */ |
||
49 | 2 | public function exists($key) |
|
50 | { |
||
51 | 2 | return $this->get($key) !== false; |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * @inheritdoc |
||
56 | */ |
||
57 | 5 | public function get($key) |
|
58 | { |
||
59 | 5 | return $this->memcached->get($key); |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * @inheritdoc |
||
64 | */ |
||
65 | 9 | public function installed() |
|
69 | |||
70 | /** |
||
71 | * @inheritdoc |
||
72 | */ |
||
73 | public function remove($key) |
||
77 | |||
78 | /** |
||
79 | * @inheritdoc |
||
80 | */ |
||
81 | public function removeAll() |
||
85 | |||
86 | /** |
||
87 | * @inheritdoc |
||
88 | */ |
||
89 | 3 | View Code Duplication | public function set($key, $value) |
90 | { |
||
91 | // Make sure we are under the proper limit |
||
92 | 3 | if (strlen($this->memcached->getOption(\Memcached::OPT_PREFIX_KEY) . $key) > 250) { |
|
93 | throw new InvalidArgumentException('The passed cache key is over 250 bytes:' . print_r($key, true)); |
||
94 | } |
||
95 | |||
96 | 3 | return $this->memcached->set($key, $value); |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * @inheritdoc |
||
101 | */ |
||
102 | 1 | public function setExpired($key, $value, $ttl) |
|
110 | |||
111 | /** |
||
112 | * Set the MemCached settings. |
||
113 | */ |
||
114 | 9 | private function setSettings() |
|
127 | |||
128 | } |
||
129 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.