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 |
||
11 | final class PropertyAccessor implements AccessorInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | private $property; |
||
17 | |||
18 | /** |
||
19 | * @param string $property |
||
20 | */ |
||
21 | 6 | public function __construct(string $property) |
|
25 | |||
26 | /** |
||
27 | * @param object $object |
||
28 | * @param mixed $value |
||
29 | */ |
||
30 | 3 | public function setValue($object, $value) |
|
36 | |||
37 | /** |
||
38 | * @param object $object |
||
39 | * |
||
40 | * @return mixed |
||
41 | */ |
||
42 | 4 | public function getValue($object) |
|
49 | |||
50 | /** |
||
51 | * @param object $object |
||
52 | * |
||
53 | * @return string |
||
54 | */ |
||
55 | 6 | private function getClass($object): string |
|
79 | |||
80 | /** |
||
81 | * @param string $class |
||
82 | * |
||
83 | * @return \ReflectionProperty |
||
84 | */ |
||
85 | 6 | View Code Duplication | private function getReflectionProperty(string $class): \ReflectionProperty |
93 | } |
||
94 |
If you suppress an error, we recommend checking for the error condition explicitly: