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 | public function __construct(string $property) |
||
25 | |||
26 | /** |
||
27 | * @param object $object |
||
28 | * @param mixed $value |
||
29 | */ |
||
30 | public function setValue($object, $value) |
||
36 | |||
37 | /** |
||
38 | * @param object $object |
||
39 | * |
||
40 | * @return mixed |
||
41 | */ |
||
42 | public function getValue($object) |
||
49 | |||
50 | /** |
||
51 | * @param object $object |
||
52 | * |
||
53 | * @return string |
||
54 | */ |
||
55 | private function getClass($object): string |
||
67 | |||
68 | /** |
||
69 | * @param string $class |
||
70 | * |
||
71 | * @return \ReflectionProperty |
||
72 | */ |
||
73 | View Code Duplication | private function getReflectionProperty(string $class): \ReflectionProperty |
|
81 | } |
||
82 |
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.