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 |
||
31 | class Driver extends DriverAbstract |
||
32 | { |
||
33 | /** |
||
34 | * @var SimpleSSDB |
||
35 | */ |
||
36 | public $instance; |
||
37 | |||
38 | /** |
||
39 | * Driver constructor. |
||
40 | * @param array $config |
||
41 | * @throws phpFastCacheDriverException |
||
42 | */ |
||
43 | View Code Duplication | public function __construct(array $config = []) |
|
53 | |||
54 | /** |
||
55 | * @return bool |
||
56 | */ |
||
57 | public function driverCheck() |
||
58 | { |
||
59 | static $driverCheck; |
||
60 | if ($driverCheck === null) { |
||
61 | return ($driverCheck = class_exists('phpssdb\Core\SSDB')); |
||
62 | } |
||
63 | |||
64 | return $driverCheck; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param \Psr\Cache\CacheItemInterface $item |
||
69 | * @return mixed |
||
70 | * @throws \InvalidArgumentException |
||
71 | */ |
||
72 | View Code Duplication | protected function driverWrite(CacheItemInterface $item) |
|
1 ignored issue
–
show
|
|||
73 | { |
||
74 | /** |
||
75 | * Check for Cross-Driver type confusion |
||
76 | */ |
||
77 | if ($item instanceof Item) { |
||
78 | /* if (isset($this->config[ 'skipExisting' ]) && $this->config[ 'skipExisting' ] == true) { |
||
79 | $x = $this->instance->get($item->getKey()); |
||
80 | if ($x === false) { |
||
81 | return false; |
||
82 | } elseif (!is_null($x)) { |
||
83 | return true; |
||
84 | } |
||
85 | }*/ |
||
86 | |||
87 | return $this->instance->setx($item->getKey(), $this->encode($this->driverPreWrap($item)), $item->getTtl()); |
||
88 | } else { |
||
89 | throw new \InvalidArgumentException('Cross-Driver type confusion detected'); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param \Psr\Cache\CacheItemInterface $item |
||
95 | * @return mixed |
||
96 | */ |
||
97 | View Code Duplication | protected function driverRead(CacheItemInterface $item) |
|
1 ignored issue
–
show
|
|||
98 | { |
||
99 | $val = $this->instance->get($item->getKey()); |
||
100 | if ($val == false) { |
||
101 | return null; |
||
102 | } else { |
||
103 | return $this->decode($val); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param \Psr\Cache\CacheItemInterface $item |
||
109 | * @return bool |
||
110 | * @throws \InvalidArgumentException |
||
111 | */ |
||
112 | protected function driverDelete(CacheItemInterface $item) |
||
123 | |||
124 | /** |
||
125 | * @return bool |
||
126 | */ |
||
127 | protected function driverClear() |
||
131 | |||
132 | /** |
||
133 | * @return bool |
||
134 | * @throws phpFastCacheDriverException |
||
135 | */ |
||
136 | protected function driverConnect() |
||
164 | |||
165 | /******************** |
||
166 | * |
||
167 | * PSR-6 Extended Methods |
||
168 | * |
||
169 | *******************/ |
||
170 | |||
171 | /** |
||
172 | * @return driverStatistic |
||
173 | */ |
||
174 | public function getStats() |
||
190 | } |
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.