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 |
||
| 17 | class FileStore extends CacheStore |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @invisible |
||
| 21 | * |
||
| 22 | * @var FilesInterface |
||
| 23 | */ |
||
| 24 | protected $files; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | private $directory; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private $extension; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param FilesInterface $files |
||
| 38 | * @param string $directory |
||
| 39 | * @param string $extension |
||
| 40 | */ |
||
| 41 | public function __construct( |
||
| 51 | |||
| 52 | /** |
||
| 53 | * {@inheritdoc} |
||
| 54 | */ |
||
| 55 | public function isAvailable(): bool |
||
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritdoc} |
||
| 62 | */ |
||
| 63 | View Code Duplication | public function has(string $name): bool |
|
| 79 | |||
| 80 | /** |
||
| 81 | * {@inheritdoc} |
||
| 82 | * |
||
| 83 | * @param int $expiration Current expiration time value in seconds (reference). |
||
| 84 | */ |
||
| 85 | View Code Duplication | public function get(string $name, int &$expiration = null) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * {@inheritdoc} |
||
| 103 | */ |
||
| 104 | public function set(string $name, $data, $ttl = null) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritdoc} |
||
| 114 | */ |
||
| 115 | public function delete(string $name) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * {@inheritdoc} |
||
| 122 | */ |
||
| 123 | View Code Duplication | public function inc(string $name, int $delta = 1): int |
|
| 135 | |||
| 136 | /** |
||
| 137 | * {@inheritdoc} |
||
| 138 | */ |
||
| 139 | View Code Duplication | public function dec(string $name, int $delta = 1): int |
|
| 151 | |||
| 152 | /** |
||
| 153 | * {@inheritdoc} |
||
| 154 | */ |
||
| 155 | public function clear() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Create filename using cache name. |
||
| 164 | * |
||
| 165 | * @param string $name |
||
| 166 | * |
||
| 167 | * @return string Filename. |
||
| 168 | */ |
||
| 169 | protected function makeFilename(string $name): string |
||
| 173 | } |
||
| 174 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.