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 |
||
28 | class Driver extends DriverAbstract |
||
29 | { |
||
30 | use StandardPsr6StructureTrait; |
||
31 | const PREFIX = 'PFC_'; |
||
32 | |||
33 | /** |
||
34 | * Driver constructor. |
||
35 | * @param array $config |
||
36 | * @throws phpFastCacheDriverException |
||
37 | */ |
||
38 | View Code Duplication | public function __construct(array $config = []) |
|
1 ignored issue
–
show
|
|||
39 | { |
||
40 | $this->setup($config); |
||
41 | |||
42 | if (!$this->driverCheck()) { |
||
43 | throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName())); |
||
44 | } |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @return bool |
||
49 | */ |
||
50 | public function driverCheck() |
||
58 | |||
59 | /** |
||
60 | * @return bool |
||
61 | */ |
||
62 | protected function driverConnect() |
||
66 | |||
67 | /** |
||
68 | * @param \Psr\Cache\CacheItemInterface $item |
||
69 | * @return mixed |
||
70 | * @throws \InvalidArgumentException |
||
71 | */ |
||
72 | protected function driverWrite(CacheItemInterface $item) |
||
91 | |||
92 | /** |
||
93 | * @param \Psr\Cache\CacheItemInterface $item |
||
94 | * @return mixed |
||
95 | * @throws \phpFastCache\Exceptions\phpFastCacheDriverException |
||
96 | */ |
||
97 | protected function driverRead(CacheItemInterface $item) |
||
98 | { |
||
99 | $this->driverConnect(); |
||
100 | // return null if no caching |
||
101 | // return value if in caching |
||
102 | $keyword = self::PREFIX . $item->getKey(); |
||
103 | $x = isset($_COOKIE[ $keyword ]) ? $this->decode(json_decode($_COOKIE[ $keyword ], true)) : false; |
||
104 | |||
105 | if ($x == false) { |
||
106 | return null; |
||
107 | } else { |
||
108 | if (!is_scalar($this->driverUnwrapData($x)) && !is_null($this->driverUnwrapData($x))) { |
||
109 | throw new phpFastCacheDriverException('Hacking attempt: The decoding returned a non-scalar value, Cookie driver does not allow this.'); |
||
110 | } |
||
111 | |||
112 | return $x; |
||
113 | } |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param $key |
||
118 | * @return int |
||
119 | */ |
||
120 | protected function driverReadExpirationDate($key) |
||
128 | |||
129 | /** |
||
130 | * @param \Psr\Cache\CacheItemInterface $item |
||
131 | * @return bool |
||
132 | * @throws \InvalidArgumentException |
||
133 | */ |
||
134 | protected function driverDelete(CacheItemInterface $item) |
||
149 | |||
150 | /** |
||
151 | * @return bool |
||
152 | */ |
||
153 | protected function driverClear() |
||
169 | |||
170 | /******************** |
||
171 | * |
||
172 | * PSR-6 Extended Methods |
||
173 | * |
||
174 | *******************/ |
||
175 | |||
176 | /** |
||
177 | * @return driverStatistic |
||
178 | */ |
||
179 | public function getStats() |
||
198 | } |
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.