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 |
||
| 7 | class FileCache implements CacheInterface |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var string |
||
| 11 | */ |
||
| 12 | private $dir; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * FileCache constructor. |
||
| 16 | * |
||
| 17 | * @param string $dir |
||
| 18 | */ |
||
| 19 | public function __construct($dir) |
||
| 27 | |||
| 28 | /** |
||
| 29 | * {@inheritDoc} |
||
| 30 | */ |
||
| 31 | View Code Duplication | public function loadClassMetadataFromCache(\ReflectionClass $class) |
|
| 40 | |||
| 41 | /** |
||
| 42 | * {@inheritDoc} |
||
| 43 | */ |
||
| 44 | public function putClassMetadataInCache(ClassMetadata $metadata) |
||
| 45 | { |
||
| 46 | $path = $this->getFileName($metadata); |
||
| 47 | if (file_exists($path) && !is_writable($path)) { |
||
| 48 | throw new \RuntimeException("Cache file {$path} is not writable."); |
||
| 49 | } |
||
| 50 | |||
| 51 | if (false === (@file_put_contents($path, |
||
| 52 | '<?php return unserialize(' . var_export(serialize($metadata), true) . ');') |
||
| 53 | )) { |
||
| 54 | throw new \RuntimeException("Can't not write new cache file to {$path}"); |
||
| 55 | }; |
||
| 56 | |||
| 57 | // Let's not break filesystems which do not support chmod. |
||
| 58 | @chmod($path, 0666 & ~umask()); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * {@inheritDoc} |
||
| 63 | */ |
||
| 64 | View Code Duplication | public function evictClassMetadataFromCache(\ReflectionClass $class) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @param ClassMetadata $metadata |
||
| 74 | * |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | private function getFileName(ClassMetadata $metadata) |
||
| 81 | } |
||
| 82 |