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:
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 |
||
| 33 | class Driver extends DriverAbstract |
||
| 34 | { |
||
| 35 | use PathSeekerTrait, StandardPsr6StructureTrait; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * |
||
| 39 | */ |
||
| 40 | const FILE_DIR = 'sqlite'; |
||
| 41 | /** |
||
| 42 | * |
||
| 43 | */ |
||
| 44 | const INDEXING_FILE = 'indexing'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var int |
||
| 48 | */ |
||
| 49 | protected $maxSize = 10; // 10 mb |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var int |
||
| 53 | */ |
||
| 54 | protected $currentDB = 1; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | protected $SqliteDir = ''; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var null |
||
| 63 | */ |
||
| 64 | protected $indexing; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Driver constructor. |
||
| 68 | * @param array $config |
||
| 69 | * @throws phpFastCacheDriverException |
||
| 70 | */ |
||
| 71 | public function __construct(array $config = []) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @return string |
||
| 88 | * @throws \phpFastCache\Exceptions\phpFastCacheCoreException |
||
| 89 | */ |
||
| 90 | public function getSqliteDir() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | public function driverCheck() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * INIT NEW DB |
||
| 105 | * @param \PDO $db |
||
| 106 | */ |
||
| 107 | public function initDB(\PDO $db) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * INIT Indexing DB |
||
| 118 | * @param \PDO $db |
||
| 119 | */ |
||
| 120 | public function initIndexing(\PDO $db) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * INIT Instant DB |
||
| 140 | * Return Database of Keyword |
||
| 141 | * @param $keyword |
||
| 142 | * @return int |
||
| 143 | */ |
||
| 144 | public function indexing($keyword) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param $keyword |
||
| 211 | * @param bool $reset |
||
| 212 | * @return PDO |
||
| 213 | */ |
||
| 214 | public function getDb($keyword, $reset = false) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param \Psr\Cache\CacheItemInterface $item |
||
| 247 | * @return mixed |
||
| 248 | * @throws \InvalidArgumentException |
||
| 249 | */ |
||
| 250 | protected function driverWrite(CacheItemInterface $item) |
||
| 251 | { |
||
| 252 | /** |
||
| 253 | * Check for Cross-Driver type confusion |
||
| 254 | */ |
||
| 255 | if ($item instanceof Item) { |
||
| 256 | $skipExisting = isset($this->config[ 'skipExisting' ]) ? $this->config[ 'skipExisting' ] : false; |
||
| 257 | $toWrite = true; |
||
| 258 | |||
| 259 | // check in cache first |
||
| 260 | $in_cache = $this->driverRead($item); |
||
| 261 | |||
| 262 | if ($skipExisting == true) { |
||
| 263 | if ($in_cache == null) { |
||
| 264 | $toWrite = true; |
||
| 265 | } else { |
||
| 266 | $toWrite = false; |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | if ($toWrite == true) { |
||
| 271 | try { |
||
| 272 | $stm = $this->getDb($item->getKey()) |
||
| 273 | ->prepare("INSERT OR REPLACE INTO `caching` (`keyword`,`object`,`exp`) values(:keyword,:object,:exp)"); |
||
| 274 | $stm->execute([ |
||
| 275 | ':keyword' => $item->getKey(), |
||
| 276 | ':object' => $this->encode($this->driverPreWrap($item)), |
||
| 277 | ':exp' => time() + $item->getTtl(), |
||
| 278 | ]); |
||
| 279 | |||
| 280 | return true; |
||
| 281 | } catch (\PDOException $e) { |
||
| 282 | |||
| 283 | try { |
||
| 284 | $stm = $this->getDb($item->getKey(), true) |
||
| 285 | ->prepare("INSERT OR REPLACE INTO `caching` (`keyword`,`object`,`exp`) values(:keyword,:object,:exp)"); |
||
| 286 | $stm->execute([ |
||
| 287 | ':keyword' => $item->getKey(), |
||
| 288 | ':object' => $this->encode($this->driverPreWrap($item)), |
||
| 289 | ':exp' => time() + $item->getTtl(), |
||
| 290 | ]); |
||
| 291 | } catch (PDOException $e) { |
||
| 292 | return false; |
||
| 293 | } |
||
| 294 | } |
||
| 295 | } |
||
| 296 | |||
| 297 | return false; |
||
| 298 | } else { |
||
| 299 | throw new \InvalidArgumentException('Cross-Driver type confusion detected'); |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param \Psr\Cache\CacheItemInterface $item |
||
| 305 | * @return mixed |
||
| 306 | */ |
||
| 307 | protected function driverRead(CacheItemInterface $item) |
||
| 308 | { |
||
| 309 | try { |
||
| 310 | $stm = $this->getDb($item->getKey()) |
||
| 311 | ->prepare("SELECT * FROM `caching` WHERE `keyword`=:keyword AND (`exp` >= :U) LIMIT 1"); |
||
| 312 | $stm->execute([ |
||
| 313 | ':keyword' => $item->getKey(), |
||
| 314 | ':U' => time(), |
||
| 315 | ]); |
||
| 316 | $row = $stm->fetch(PDO::FETCH_ASSOC); |
||
| 317 | |||
| 318 | } catch (PDOException $e) { |
||
| 319 | try { |
||
| 320 | $stm = $this->getDb($item->getKey(), true) |
||
| 321 | ->prepare("SELECT * FROM `caching` WHERE `keyword`=:keyword AND (`exp` >= :U) LIMIT 1"); |
||
| 322 | $stm->execute([ |
||
| 323 | ':keyword' => $item->getKey(), |
||
| 324 | ':U' => time(), |
||
| 325 | ]); |
||
| 326 | $row = $stm->fetch(PDO::FETCH_ASSOC); |
||
| 327 | } catch (PDOException $e) { |
||
| 328 | return null; |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | if (isset($row[ 'id' ])) { |
||
| 333 | /** |
||
| 334 | * @var $item ExtendedCacheItemInterface |
||
| 335 | */ |
||
| 336 | $item = $this->decode($row[ 'object' ]); |
||
| 337 | if ($item instanceof ExtendedCacheItemInterface && $item->isExpired()) { |
||
| 338 | $this->driverDelete($item); |
||
| 339 | |||
| 340 | return null; |
||
| 341 | } |
||
| 342 | |||
| 343 | return $this->decode($row[ 'object' ]); |
||
| 344 | } |
||
| 345 | |||
| 346 | return null; |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @param \Psr\Cache\CacheItemInterface $item |
||
| 351 | * @return bool |
||
| 352 | * @throws \InvalidArgumentException |
||
| 353 | */ |
||
| 354 | protected function driverDelete(CacheItemInterface $item) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @return bool |
||
| 380 | */ |
||
| 381 | protected function driverClear() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @return bool |
||
| 399 | */ |
||
| 400 | protected function driverConnect() |
||
| 410 | |||
| 411 | /******************** |
||
| 412 | * |
||
| 413 | * PSR-6 Extended Methods |
||
| 414 | * |
||
| 415 | *******************/ |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @return driverStatistic |
||
| 419 | * @throws PDOException |
||
| 420 | */ |
||
| 421 | View Code Duplication | public function getStats() |
|
| 437 | |||
| 438 | /** |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | public function __sleep() |
||
| 445 | } |
$this->getSqliteDir()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: