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 |
||
32 | class Driver implements ExtendedCacheItemPoolInterface |
||
33 | { |
||
34 | use DriverBaseTrait, MemcacheDriverCollisionDetectorTrait; |
||
35 | |||
36 | /** |
||
37 | * Driver constructor. |
||
38 | * @param array $config |
||
39 | * @throws phpFastCacheDriverException |
||
40 | */ |
||
41 | View Code Duplication | public function __construct(array $config = []) |
|
42 | { |
||
43 | self::checkCollision('Memcached'); |
||
44 | $this->setup($config); |
||
45 | |||
46 | if (!$this->driverCheck()) { |
||
47 | throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName())); |
||
48 | } else { |
||
49 | $this->driverConnect(); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @return bool |
||
55 | */ |
||
56 | public function driverCheck() |
||
60 | |||
61 | /** |
||
62 | * @param \Psr\Cache\CacheItemInterface $item |
||
63 | * @return mixed |
||
64 | * @throws phpFastCacheInvalidArgumentException |
||
65 | */ |
||
66 | View Code Duplication | protected function driverWrite(CacheItemInterface $item) |
|
67 | { |
||
68 | /** |
||
69 | * Check for Cross-Driver type confusion |
||
70 | */ |
||
71 | if ($item instanceof Item) { |
||
72 | $ttl = $item->getTtl(); |
||
73 | |||
74 | // Memcache will only allow a expiration timer less than 2592000 seconds, |
||
75 | // otherwise, it will assume you're giving it a UNIX timestamp. |
||
76 | if ($ttl >= 2592000) { |
||
77 | $ttl = $item->getExpirationDate()->getTimestamp(); |
||
78 | } |
||
79 | |||
80 | return $this->instance->set($item->getKey(), $this->driverPreWrap($item), $ttl); |
||
81 | } else { |
||
82 | throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected'); |
||
83 | } |
||
84 | |||
85 | return true; |
||
|
|||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param \Psr\Cache\CacheItemInterface $item |
||
90 | * @return null|array |
||
91 | */ |
||
92 | View Code Duplication | protected function driverRead(CacheItemInterface $item) |
|
102 | |||
103 | /** |
||
104 | * @param \Psr\Cache\CacheItemInterface $item |
||
105 | * @return bool |
||
106 | * @throws phpFastCacheInvalidArgumentException |
||
107 | */ |
||
108 | View Code Duplication | protected function driverDelete(CacheItemInterface $item) |
|
119 | |||
120 | /** |
||
121 | * @return bool |
||
122 | */ |
||
123 | protected function driverClear() |
||
127 | |||
128 | /** |
||
129 | * @return bool |
||
130 | */ |
||
131 | protected function driverConnect() |
||
178 | |||
179 | /******************** |
||
180 | * |
||
181 | * PSR-6 Extended Methods |
||
182 | * |
||
183 | *******************/ |
||
184 | |||
185 | /** |
||
186 | * @return DriverStatistic |
||
187 | */ |
||
188 | View Code Duplication | public function getStats() |
|
203 | } |
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
orexit
statements that have been added for debug purposes.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.