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 |
||
18 | class SubHydratingHandler implements HydratingHandlerInterface |
||
19 | { |
||
20 | /** @var string */ |
||
21 | protected $key; |
||
22 | public function getKey() { return $this->key; } |
||
24 | |||
25 | /** @var string */ |
||
26 | protected $className; |
||
27 | public function getClassName() { return $this->className; } |
||
29 | |||
30 | /** @var Hydrator */ |
||
31 | protected $hydrator; |
||
32 | public function getHydrator() { return $this->hydrator; } |
||
34 | |||
35 | /** @var ValidatorInterface[] */ |
||
36 | protected $validators; |
||
37 | public function getValidators() { return $this->validators; } |
||
40 | |||
41 | /** @var mixed */ |
||
42 | protected $defaultValue; |
||
43 | public function getDefaultValue() { return $this->defaultValue; } |
||
45 | |||
46 | /** @var string */ |
||
47 | protected $errorMessage; |
||
48 | public function getErrorMessage() { return $this->errorMessage; } |
||
50 | |||
51 | /** @var GetterInterface */ |
||
52 | protected $getter; |
||
53 | public function getGetter() { return $this->getter; } |
||
55 | |||
56 | /** |
||
57 | * SubHydratingHandler constructor. |
||
58 | * @param string $key |
||
59 | * @param string $className |
||
60 | * @param Hydrator $hydrator |
||
61 | * @param ValidatorInterface[] $validators |
||
62 | * @param mixed $defaultValue |
||
63 | * @param string $errorMessage |
||
64 | * @param GetterInterface $getter |
||
65 | */ |
||
66 | public function __construct(string $key, string $className, Hydrator $hydrator, array $validators = [], $defaultValue = null, string $errorMessage = "", GetterInterface $getter = null) |
||
76 | |||
77 | /** |
||
78 | * @param array $data |
||
79 | * @param array $targetData |
||
80 | * @param $object |
||
81 | * |
||
82 | * @throws HydratingException |
||
83 | */ |
||
84 | public function handle(array $data, array &$targetData, $object = null) |
||
116 | |||
117 | /** |
||
118 | * @param mixed $parsedValue |
||
119 | * @param mixed $contextObject |
||
120 | * |
||
121 | * @throws HydratingException |
||
122 | */ |
||
123 | View Code Duplication | private function validate($parsedValue, $contextObject = null) |
|
133 | |||
134 | /** |
||
135 | * @param $object |
||
136 | * @return mixed |
||
137 | */ |
||
138 | protected function getSubObject($object) |
||
145 | } |
||
146 |
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.