Complex classes like Driver often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Driver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class Driver extends DriverAbstract |
||
| 32 | { |
||
| 33 | use PathSeekerTrait, StandardPsr6StructureTrait; |
||
| 34 | /** |
||
| 35 | * |
||
| 36 | */ |
||
| 37 | const FILES_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() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param \Psr\Cache\CacheItemInterface $item |
||
| 63 | * @return mixed |
||
| 64 | * @throws \InvalidArgumentException |
||
| 65 | */ |
||
| 66 | public function driverWrite(CacheItemInterface $item) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param string $key |
||
| 107 | * @return mixed |
||
| 108 | * @throws \InvalidArgumentException |
||
| 109 | */ |
||
| 110 | public function driverRead($key) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param \Psr\Cache\CacheItemInterface $item |
||
| 135 | * @return bool |
||
| 136 | * @throws \InvalidArgumentException |
||
| 137 | */ |
||
| 138 | public function driverDelete(CacheItemInterface $item) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return bool |
||
| 157 | */ |
||
| 158 | public function driverClear() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @return bool |
||
| 192 | */ |
||
| 193 | public function driverConnect() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param \Psr\Cache\CacheItemInterface $item |
||
| 200 | * @return bool |
||
| 201 | * @throws \InvalidArgumentException |
||
| 202 | */ |
||
| 203 | public function driverIsHit(CacheItemInterface $item) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param string $optionName |
||
| 218 | * @param mixed $optionValue |
||
| 219 | * @return bool |
||
| 220 | * @throws \InvalidArgumentException |
||
| 221 | */ |
||
| 222 | public static function isValidOption($optionName, $optionValue) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @return string |
||
| 249 | * @throws \phpFastCache\Exceptions\phpFastCacheCoreException |
||
| 250 | */ |
||
| 251 | public function getFilesDir() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @return array |
||
| 258 | */ |
||
| 259 | public static function getValidOptions() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return array |
||
| 266 | */ |
||
| 267 | public static function getRequiredOptions() |
||
| 271 | |||
| 272 | /******************** |
||
| 273 | * |
||
| 274 | * PSR-6 Extended Methods |
||
| 275 | * |
||
| 276 | *******************/ |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @return driverStatistic |
||
| 280 | * @throws \phpFastCache\Exceptions\phpFastCacheCoreException |
||
| 281 | * @throws \phpFastCache\Exceptions\phpFastCacheDriverException |
||
| 282 | */ |
||
| 283 | public function getStats() |
||
| 300 | } |
$this->getFilesDir()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:
if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) { throw new \InvalidArgumentException('This input is not allowed.'); }For numeric data, we recommend to explicitly cast the data: