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 |
||
13 | class Filesystem extends FilesystemCache{ |
||
14 | /** |
||
15 | * @var Stopwatch |
||
16 | */ |
||
17 | protected $stopwatch; |
||
18 | protected $stopwatchAdditionalTags = array(); |
||
19 | |||
20 | /** |
||
21 | * @param Stopwatch $stopwatch |
||
22 | */ |
||
23 | public function setStopwatch(Stopwatch $stopwatch) |
||
27 | |||
28 | /** |
||
29 | * @param array $tags |
||
30 | */ |
||
31 | public function setStopwatchTags(array $tags) |
||
35 | |||
36 | /** |
||
37 | * @param $methodName |
||
38 | * @return StopwatchEvent|null |
||
39 | */ |
||
40 | protected function getStopwatchEvent($methodName) |
||
50 | |||
51 | |||
52 | /** |
||
53 | * Fetches an entry from the cache. |
||
54 | * |
||
55 | * @param string $id The id of the cache entry to fetch. |
||
56 | * |
||
57 | * @return mixed|false The cached data or FALSE, if no cache entry exists for the given id. |
||
58 | */ |
||
59 | View Code Duplication | protected function doFetch($id) |
|
69 | |||
70 | /** |
||
71 | * Tests if an entry exists in the cache. |
||
72 | * |
||
73 | * @param string $id The cache id of the entry to check for. |
||
74 | * |
||
75 | * @return bool TRUE if a cache entry exists for the given cache id, FALSE otherwise. |
||
76 | */ |
||
77 | View Code Duplication | protected function doContains($id) |
|
86 | |||
87 | /** |
||
88 | ** |
||
89 | * Puts data into the cache. |
||
90 | * |
||
91 | * @param string $id The cache id. |
||
92 | * @param string $data The cache entry/data. |
||
93 | * @param int $lifeTime The lifetime. If != 0, sets a specific lifetime for this |
||
94 | * cache entry (0 => infinite lifeTime). |
||
95 | * |
||
96 | * @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise. |
||
97 | */ |
||
98 | protected function doSave($id, $data, $lifeTime = 0) |
||
107 | } |
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.