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 |
||
14 | class ObjectField implements CliField { |
||
15 | |||
16 | /** @var TypeFactory */ |
||
17 | private $types; |
||
18 | |||
19 | /** @var FieldRegistry */ |
||
20 | private $fields; |
||
21 | |||
22 | /** @var ParameterReader */ |
||
23 | private $reader; |
||
24 | |||
25 | public function __construct(TypeFactory $types, FieldRegistry $fields, ParameterReader $reader) { |
||
30 | |||
31 | /** |
||
32 | * @param Parameter $parameter |
||
33 | * @return bool |
||
34 | */ |
||
35 | public function handles(Parameter $parameter) { |
||
38 | |||
39 | /** |
||
40 | * @param Parameter $parameter |
||
41 | * @param null $serialized |
||
42 | * @return object |
||
43 | */ |
||
44 | public function inflate(Parameter $parameter, $serialized) { |
||
67 | |||
68 | View Code Duplication | private function getClass(Parameter $parameter) { |
|
75 | |||
76 | /** |
||
77 | * @param Parameter $param |
||
78 | * @return CliField |
||
79 | */ |
||
80 | private function getField(Parameter $param) { |
||
83 | |||
84 | /** |
||
85 | * @param Parameter $parameter |
||
86 | * @return string |
||
87 | */ |
||
88 | public function getDescription(Parameter $parameter) { |
||
91 | |||
92 | View Code Duplication | private function getProperties(Parameter $parameter, $object = null) { |
|
101 | } |
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.