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 |
||
15 | class ObjectField implements WebField { |
||
16 | |||
17 | /** @var TypeFactory */ |
||
18 | private $types; |
||
19 | |||
20 | /** @var FieldRegistry */ |
||
21 | private $fields; |
||
22 | |||
23 | /** |
||
24 | * @param TypeFactory $types |
||
25 | * @param FieldRegistry $fields |
||
26 | */ |
||
27 | public function __construct(TypeFactory $types, FieldRegistry $fields) { |
||
31 | |||
32 | /** |
||
33 | * @param Parameter $parameter |
||
34 | * @return bool |
||
35 | */ |
||
36 | public function handles(Parameter $parameter) { |
||
39 | |||
40 | /** |
||
41 | * @param Parameter $parameter |
||
42 | * @return array|\rtens\domin\delivery\web\Element[] |
||
43 | */ |
||
44 | public function headElements(Parameter $parameter) { |
||
52 | |||
53 | /** |
||
54 | * @param Parameter $parameter |
||
55 | * @param array $serialized |
||
56 | * @return object |
||
57 | */ |
||
58 | public function inflate(Parameter $parameter, $serialized) { |
||
61 | |||
62 | /** |
||
63 | * @param Parameter $parameter |
||
64 | * @param mixed $value |
||
65 | * @return string |
||
66 | */ |
||
67 | public function render(Parameter $parameter, $value) { |
||
77 | |||
78 | private function createInstance(Parameter $parameter, $properties) { |
||
89 | |||
90 | private function inflateProperties(Parameter $parameter, array $serialized) { |
||
100 | |||
101 | private function inflateProperty($serialized, Parameter $parameter) { |
||
104 | |||
105 | private function renderPropertyFields(Parameter $parameter, $object) { |
||
113 | |||
114 | private function renderPropertyField(Property $property, Parameter $param, $object) { |
||
120 | |||
121 | /** |
||
122 | * @param Parameter $param |
||
123 | * @return WebField |
||
124 | */ |
||
125 | private function getField(Parameter $param) { |
||
128 | |||
129 | View Code Duplication | private function getClass(Parameter $parameter) { |
|
136 | |||
137 | private function makePropertyParameter(Parameter $parameter, Property $property) { |
||
140 | |||
141 | private function getProperties(Parameter $parameter, $object = null) { |
||
150 | } |
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.