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:
Complex classes like SubArrayHydratingHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SubArrayHydratingHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class SubArrayHydratingHandler implements HydratingHandlerInterface |
||
16 | { |
||
17 | /** @var string */ |
||
18 | protected $key; |
||
19 | public function getKey() { return $this->key; } |
||
21 | |||
22 | /** @var string */ |
||
23 | protected $className; |
||
24 | public function getClassName() { return $this->className; } |
||
26 | |||
27 | /** @var Hydrator */ |
||
28 | protected $hydrator; |
||
29 | public function getHydrator() { return $this->hydrator; } |
||
31 | |||
32 | /** @var string */ |
||
33 | protected $errorMessage; |
||
34 | public function getErrorMessage() { return $this->errorMessage; } |
||
36 | |||
37 | /** @var string */ |
||
38 | private $subErrorMessage; |
||
39 | public function getSubErrorMessage() { return $this->subErrorMessage; } |
||
41 | |||
42 | /** @var ValidatorInterface[] */ |
||
43 | private $validators; |
||
44 | public function getValidators() { return $this->validators; } |
||
47 | |||
48 | /** @var GetterInterface */ |
||
49 | protected $getter; |
||
50 | public function getGetter() { return $this->getter; } |
||
52 | |||
53 | /** @var bool */ |
||
54 | private $associative; |
||
55 | public function getAssociative() { return $this->associative; } |
||
57 | |||
58 | /** |
||
59 | * SubArrayHydratingHandler constructor. |
||
60 | * @param string $key |
||
61 | * @param string $className |
||
62 | * @param Hydrator $hydrator |
||
63 | * @param ValidatorInterface[] $validators |
||
64 | * @param string $errorMessage |
||
65 | * @param string $subErrorMessage |
||
66 | * @param GetterInterface $getter |
||
67 | * @param bool $associative |
||
68 | */ |
||
69 | public function __construct(string $key, string $className, Hydrator $hydrator, array $validators = [], string $errorMessage = "", string $subErrorMessage = "", GetterInterface $getter = null, bool $associative = false) |
||
80 | |||
81 | /** |
||
82 | * @param array $data |
||
83 | * @param array $targetData |
||
84 | * @param $object |
||
85 | * |
||
86 | * @throws HydratingException |
||
87 | */ |
||
88 | public function handle(array $data, array &$targetData, $object = null) |
||
123 | |||
124 | /** |
||
125 | * @param string $key |
||
126 | * @param array|null $data |
||
127 | * @param array $array |
||
128 | * @param array $errorsMap |
||
129 | * @return bool |
||
130 | */ |
||
131 | protected function handleSub($key, $data, &$array, &$errorsMap) |
||
166 | |||
167 | /** |
||
168 | * @param $object |
||
169 | * @return array |
||
170 | */ |
||
171 | protected function getSubArray($object) |
||
178 | |||
179 | /** |
||
180 | * @param mixed $parsedValue |
||
181 | * @param mixed $contextObject |
||
182 | * |
||
183 | * @throws HydratingException |
||
184 | */ |
||
185 | View Code Duplication | private function validate($parsedValue, $contextObject = null) |
|
195 | } |
||
196 |
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.