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 ObjectProperty implements HydratorInterface, StrategyEnabledInterface |
||
16 | { |
||
17 | /** |
||
18 | * The list with strategies that this hydrator has. |
||
19 | * |
||
20 | * @var \ArrayObject |
||
21 | */ |
||
22 | protected $strategies; |
||
23 | |||
24 | /** |
||
25 | * Initializes a new instance of this class. |
||
26 | */ |
||
27 | 73 | public function __construct() |
|
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | */ |
||
35 | 15 | public function extract($object) |
|
57 | |||
58 | /** |
||
59 | * @inheritDoc |
||
60 | */ |
||
61 | 14 | public function hydrate(array $data, $object) |
|
94 | |||
95 | /** |
||
96 | * @inheritDoc |
||
97 | */ |
||
98 | 1 | public function canHydrate($object) |
|
102 | |||
103 | /** |
||
104 | * @inheritDoc |
||
105 | */ |
||
106 | 67 | public function addStrategy($name, StrategyInterface $strategy) |
|
112 | |||
113 | /** |
||
114 | * @inheritDoc |
||
115 | */ |
||
116 | 21 | public function getStrategy($name) |
|
120 | |||
121 | /** |
||
122 | * @inheritDoc |
||
123 | */ |
||
124 | 30 | public function hasStrategy($name) |
|
128 | |||
129 | /** |
||
130 | * @inheritDoc |
||
131 | */ |
||
132 | 1 | public function removeStrategy($name) |
|
138 | |||
139 | /** |
||
140 | * Converts a value for extraction. If no strategy exists the plain value is returned. |
||
141 | * |
||
142 | * @param string $name The name of the strategy to use. |
||
143 | * @param mixed $value The value that should be converted. |
||
144 | * |
||
145 | * @return mixed |
||
146 | */ |
||
147 | 14 | View Code Duplication | public function extractValue($name, $value) |
155 | |||
156 | /** |
||
157 | * Converts a value for hydration. If no strategy exists the plain value is returned. |
||
158 | * |
||
159 | * @param string $name The name of the strategy to use. |
||
160 | * @param mixed $value The value that should be converted. |
||
161 | * |
||
162 | * @return mixed |
||
163 | */ |
||
164 | 13 | View Code Duplication | public function hydrateValue($name, $value) |
172 | } |
||
173 |
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.