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 |
||
22 | class CachingLoader extends CachingLoaderDecorator |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * @var CollectionInterface $cache |
||
27 | */ |
||
28 | protected $cache; |
||
29 | |||
30 | /** |
||
31 | * @var string $identifier |
||
32 | */ |
||
33 | protected $identifier; |
||
34 | |||
35 | |||
36 | |||
37 | /** |
||
38 | * CachingLoader constructor. |
||
39 | * |
||
40 | * @param LoaderDecoratorInterface $loader |
||
41 | * @param CollectionInterface $cache |
||
42 | * @param string $identifier |
||
43 | * @throws InvalidDataTypeException |
||
44 | */ |
||
45 | public function __construct( |
||
69 | |||
70 | |||
71 | |||
72 | /** |
||
73 | * @return string |
||
74 | */ |
||
75 | public function identifier() |
||
79 | |||
80 | |||
81 | |||
82 | /** |
||
83 | * @param string $identifier |
||
84 | * @throws InvalidDataTypeException |
||
85 | */ |
||
86 | private function setIdentifier($identifier) |
||
93 | |||
94 | |||
95 | /** |
||
96 | * @param string $fqcn |
||
97 | * @param mixed $object |
||
98 | * @return bool |
||
99 | * @throws InvalidArgumentException |
||
100 | */ |
||
101 | public function share($fqcn, $object) |
||
116 | |||
117 | |||
118 | /** |
||
119 | * @param string $fqcn |
||
120 | * @param array $arguments |
||
121 | * @param bool $shared |
||
122 | * @return mixed |
||
123 | */ |
||
124 | public function load($fqcn, $arguments = array(), $shared = true) |
||
150 | |||
151 | |||
152 | |||
153 | /** |
||
154 | * empties cache and calls reset() on loader if method exists |
||
155 | */ |
||
156 | public function reset() |
||
161 | /** |
||
162 | * build a string representation of a class' name and arguments |
||
163 | * |
||
164 | * @param string $fqcn |
||
165 | * @param array $arguments |
||
166 | * @return string |
||
167 | */ |
||
168 | private function getClassIdentifier($fqcn, $arguments = array()) |
||
176 | |||
177 | |||
178 | /** |
||
179 | * build a string representation of a class' arguments |
||
180 | * (mostly because Closures can't be serialized) |
||
181 | * |
||
182 | * @param array $arguments |
||
183 | * @return string |
||
184 | */ |
||
185 | private function getIdentifierForArguments(array $arguments) |
||
207 | |||
208 | |||
209 | } |
||
210 | // End of file CachingLoader.php |
||
212 |