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 | private $dir; |
||
| 10 | |||
| 11 | public function __construct($dir) |
||
| 22 | |||
| 23 | /** |
||
| 24 | * {@inheritDoc} |
||
| 25 | */ |
||
| 26 | View Code Duplication | public function loadClassMetadataFromCache(\ReflectionClass $class) |
|
| 27 | { |
||
| 28 | $path = $this->dir.'/'.$this->escapeFileName($class->name).'.cache.php'; |
||
| 29 | if (!@file_exists($path)) { |
||
| 30 | return null; |
||
| 31 | } |
||
| 32 | |||
| 33 | return include $path; |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * {@inheritDoc} |
||
| 38 | */ |
||
| 39 | public function putClassMetadataInCache(ClassMetadata $metadata) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Renames a file with fallback for windows |
||
| 54 | * |
||
| 55 | * @param string $source |
||
| 56 | * @param string $target |
||
| 57 | */ |
||
| 58 | private function renameFile($source, $target) { |
||
| 72 | |||
| 73 | /** |
||
| 74 | * {@inheritDoc} |
||
| 75 | */ |
||
| 76 | View Code Duplication | public function evictClassMetadataFromCache(\ReflectionClass $class) |
|
| 83 | |||
| 84 | /** |
||
| 85 | * @param string $fileName |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | private function escapeFileName($fileName) |
||
| 97 | } |
||
| 98 |