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 RecordOriginConstraint |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $recordOriginField; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | private $recordOriginBlacklist; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | private $exceptionFieldMap; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | private $changedObjectPaths; |
||
38 | |||
39 | /** |
||
40 | * RecordOriginConstraint constructor. |
||
41 | * |
||
42 | * @param ConstraintUtils $utils Utils |
||
43 | * @param string $recordOriginField name of the recordOrigin field |
||
44 | * @param array $recordOriginBlacklist list of recordOrigin values that cannot be modified |
||
45 | * @param array $exceptionFieldMap field map from compiler pass with excluded fields |
||
46 | */ |
||
47 | public function __construct( |
||
58 | |||
59 | /** |
||
60 | * Checks the recordOrigin rules and sets error in event if needed |
||
61 | * |
||
62 | * @param ConstraintEventSchema $event event class |
||
63 | * |
||
64 | * @return void |
||
65 | */ |
||
66 | public function checkRecordOrigin(ConstraintEventSchema $event) |
||
168 | |||
169 | /** |
||
170 | * recursive helperfunction that walks through two arrays/objects of the same structure, |
||
171 | * compares the values and writes the paths containining differences into the $this->changedObjectPaths |
||
172 | * |
||
173 | * @param mixed $object the first of the datastructures to compare |
||
174 | * @param mixed $compareObject the second of the datastructures to compare |
||
175 | * @param array $path the array holding the current path |
||
176 | * @return array returning the child nodes in an array |
||
177 | */ |
||
178 | private function getChangedObjectPaths($object, $compareObject, $path = []) |
||
179 | { |
||
180 | $return = []; |
||
181 | foreach ($object as $fieldName => $value) { |
||
182 | if (is_object($compareObject)) { |
||
183 | $compareValue = $compareObject->$fieldName; |
||
184 | } else { |
||
185 | $compareValue = $compareObject[$fieldName]; |
||
186 | } |
||
187 | $path[]=$fieldName; |
||
188 | if (is_object($value) || is_array($value)) { |
||
189 | $return[$fieldName] = $this->getChangedObjectPaths($value, $compareValue, $path); |
||
190 | } else { |
||
191 | if ($value!=$compareValue) { |
||
192 | $this->changedObjectPaths[] = implode('.', $path); |
||
193 | } |
||
194 | } |
||
195 | array_pop($path); |
||
196 | } |
||
197 | return $return; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * if the user provides properties that are in the exception list but not on the currently saved |
||
202 | * object, we try here to synthetically add them to our representation. and yes, this won't support |
||
203 | * exclusions in an array structure for the moment, but that is also not needed for now. |
||
204 | * |
||
205 | * @param string $expression the expression |
||
206 | * @param object $obj the object |
||
207 | * |
||
208 | * @return object the modified object |
||
209 | */ |
||
210 | private function addProperties($expression, $obj) |
||
235 | } |
||
236 |
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.