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 |
||
19 | trait ExtendedCacheItemPoolTrait |
||
20 | { |
||
21 | /** |
||
22 | * @param string $tagName |
||
23 | * @return \phpFastCache\Cache\ExtendedCacheItemInterface[] |
||
24 | * @throws InvalidArgumentException |
||
25 | */ |
||
26 | public function getItemsByTag($tagName) |
||
41 | |||
42 | /** |
||
43 | * @param array $tagNames |
||
44 | * @return \phpFastCache\Cache\ExtendedCacheItemInterface[] |
||
45 | * @throws InvalidArgumentException |
||
46 | */ |
||
47 | public function getItemsByTags(array $tagNames) |
||
56 | |||
57 | /** |
||
58 | * @param string $tagName |
||
59 | * @return bool|null |
||
60 | * @throws InvalidArgumentException |
||
61 | */ |
||
62 | public function deleteItemsByTag($tagName) |
||
78 | |||
79 | /** |
||
80 | * @param array $tagNames |
||
81 | * @return bool|null |
||
82 | * @throws InvalidArgumentException |
||
83 | */ |
||
84 | View Code Duplication | public function deleteItemsByTags(array $tagNames) |
|
96 | |||
97 | /** |
||
98 | * @inheritdoc |
||
99 | */ |
||
100 | View Code Duplication | public function incrementItemsByTag($tagName, $step = 1) |
|
112 | |||
113 | /** |
||
114 | * @inheritdoc |
||
115 | */ |
||
116 | View Code Duplication | public function incrementItemsByTags(array $tagNames, $step = 1) |
|
129 | |||
130 | /** |
||
131 | * @inheritdoc |
||
132 | */ |
||
133 | View Code Duplication | public function decrementItemsByTag($tagName, $step = 1) |
|
145 | |||
146 | /** |
||
147 | * @inheritdoc |
||
148 | */ |
||
149 | View Code Duplication | public function decrementItemsByTags(array $tagNames, $step = 1) |
|
162 | |||
163 | /** |
||
164 | * @inheritdoc |
||
165 | */ |
||
166 | View Code Duplication | public function appendItemsByTag($tagName, $data) |
|
178 | |||
179 | /** |
||
180 | * @inheritdoc |
||
181 | */ |
||
182 | View Code Duplication | public function appendItemsByTags(array $tagNames, $data) |
|
195 | |||
196 | /** |
||
197 | * @inheritdoc |
||
198 | */ |
||
199 | View Code Duplication | public function prependItemsByTag($tagName, $data) |
|
211 | |||
212 | /** |
||
213 | * @inheritdoc |
||
214 | */ |
||
215 | View Code Duplication | public function prependItemsByTags(array $tagNames, $data) |
|
228 | } |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.