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 |
||
16 | class ReflectionClass |
||
17 | { |
||
18 | private $class; |
||
19 | private $properties; |
||
20 | private $injectionStrategy; |
||
21 | private $instanciator; |
||
22 | |||
23 | 5 | View Code Duplication | public function __construct( |
43 | |||
44 | /** |
||
45 | * Add a property to be injected in the new object |
||
46 | * |
||
47 | * @param string $property |
||
48 | * @param mixed $value |
||
49 | * |
||
50 | * @return self |
||
51 | */ |
||
52 | 2 | public function withProperty(string $property, $value): self |
|
61 | |||
62 | /** |
||
63 | * Add a set of properties that need to be injected |
||
64 | * |
||
65 | * @param array<string, mixed> $properties |
||
66 | * |
||
67 | * @return self |
||
68 | */ |
||
69 | 1 | View Code Duplication | public function withProperties(array $properties): self |
84 | |||
85 | /** |
||
86 | * Return the collection of properties that will be injected in the object |
||
87 | * |
||
88 | * @return MapInterface<string, mixed> |
||
89 | */ |
||
90 | 1 | public function properties(): MapInterface |
|
94 | |||
95 | /** |
||
96 | * Return the list of injection strategies used |
||
97 | * |
||
98 | * @return InjectionStrategiesInterface |
||
99 | */ |
||
100 | 1 | public function injectionStrategy(): InjectionStrategyInterface |
|
104 | |||
105 | /** |
||
106 | * Return the object instanciator |
||
107 | * |
||
108 | * @return InstanciatorInterface |
||
109 | */ |
||
110 | 1 | public function instanciator(): InstanciatorInterface |
|
114 | |||
115 | /** |
||
116 | * Return a new instance of the class |
||
117 | * |
||
118 | * @return object |
||
119 | */ |
||
120 | 2 | public function build() |
|
139 | } |
||
140 |
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.