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 |
||
24 | class FixedTaggingCachePool implements TaggableCacheItemPoolInterface |
||
25 | { |
||
26 | /** |
||
27 | * @type TaggableCacheItemPoolInterface |
||
28 | */ |
||
29 | private $cache; |
||
30 | |||
31 | /** |
||
32 | * @type array |
||
33 | */ |
||
34 | private $tags; |
||
35 | |||
36 | /** |
||
37 | * @param TaggableCacheItemPoolInterface $cache |
||
38 | * @param array $tags |
||
39 | */ |
||
40 | public function __construct(TaggableCacheItemPoolInterface $cache, array $tags) |
||
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | public function getItem($key) |
||
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | public function getItems(array $keys = []) |
||
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | public function hasItem($key) |
||
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | public function clear() |
||
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | public function deleteItem($key) |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | public function deleteItems(array $keys) |
||
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | View Code Duplication | public function save(CacheItemInterface $item) |
|
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | View Code Duplication | public function saveDeferred(CacheItemInterface $item) |
|
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | public function commit() |
||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | public function invalidateTag($tag) |
||
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | public function invalidateTags(array $tags) |
||
145 | } |
||
146 |
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.