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 |
||
30 | class Driver extends DriverAbstract |
||
31 | { |
||
32 | use PathSeekerTrait; |
||
33 | |||
34 | /** |
||
35 | * |
||
36 | */ |
||
37 | const FILE_DIR = 'files'; |
||
38 | |||
39 | /** |
||
40 | * Driver constructor. |
||
41 | * @param array $config |
||
42 | * @throws phpFastCacheDriverException |
||
43 | */ |
||
44 | public function __construct(array $config = []) |
||
52 | |||
53 | /** |
||
54 | * @return bool |
||
55 | */ |
||
56 | public function driverCheck() |
||
57 | { |
||
58 | return is_writable($this->getFileDir()) || @mkdir($this->getFileDir(), $this->setChmodAuto(), true); |
||
|
|||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param \Psr\Cache\CacheItemInterface $item |
||
63 | * @return mixed |
||
64 | * @throws \InvalidArgumentException |
||
65 | */ |
||
66 | protected function driverWrite(CacheItemInterface $item) |
||
104 | |||
105 | /** |
||
106 | * @param \Psr\Cache\CacheItemInterface $item |
||
107 | * @return mixed |
||
108 | */ |
||
109 | protected function driverRead(CacheItemInterface $item) |
||
110 | { |
||
111 | /** |
||
112 | * Check for Cross-Driver type confusion |
||
113 | */ |
||
114 | $file_path = $this->getFilePath($item->getKey()); |
||
115 | if (!file_exists($file_path)) { |
||
116 | return null; |
||
117 | } |
||
118 | |||
119 | $content = $this->readfile($file_path); |
||
120 | $object = $this->decode($content); |
||
121 | |||
122 | if ($this->driverUnwrapTime($object)->getTimestamp() < time()) { |
||
123 | @unlink($file_path); |
||
124 | |||
125 | return null; |
||
126 | } |
||
127 | |||
128 | return $object; |
||
129 | |||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param \Psr\Cache\CacheItemInterface $item |
||
134 | * @return bool |
||
135 | * @throws \InvalidArgumentException |
||
136 | */ |
||
137 | protected function driverDelete(CacheItemInterface $item) |
||
153 | |||
154 | /** |
||
155 | * @return bool |
||
156 | */ |
||
157 | protected function driverClear() |
||
161 | |||
162 | /** |
||
163 | * @return bool |
||
164 | */ |
||
165 | protected function driverConnect() |
||
169 | |||
170 | /** |
||
171 | * @param string $optionName |
||
172 | * @param mixed $optionValue |
||
173 | * @return bool |
||
174 | * @throws \InvalidArgumentException |
||
175 | */ |
||
176 | public static function isValidOption($optionName, $optionValue) |
||
199 | |||
200 | /** |
||
201 | * @return array |
||
202 | */ |
||
203 | public static function getValidOptions() |
||
207 | |||
208 | /** |
||
209 | * @return array |
||
210 | */ |
||
211 | public static function getRequiredOptions() |
||
215 | |||
216 | /******************** |
||
217 | * |
||
218 | * PSR-6 Extended Methods |
||
219 | * |
||
220 | *******************/ |
||
221 | |||
222 | /** |
||
223 | * @return driverStatistic |
||
224 | * @throws \phpFastCache\Exceptions\phpFastCacheCoreException |
||
225 | * @throws \phpFastCache\Exceptions\phpFastCacheDriverException |
||
226 | */ |
||
227 | View Code Duplication | public function getStats() |
|
243 | } |
$this->getFileDir()
can contain request data and is used in file manipulation context(s) leading to a potential security vulnerability.General Strategies to prevent injection
In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:
For numeric data, we recommend to explicitly cast the data: