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 |
||
20 | abstract class AbstractConfig |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $data = []; |
||
27 | |||
28 | protected $contextObject; |
||
29 | |||
30 | protected $finalClass = false; |
||
31 | |||
32 | protected $extraFieldsAllowed = null; |
||
33 | |||
34 | /** |
||
35 | * TypeConfig constructor. |
||
36 | * @param array $configData |
||
37 | * @param mixed $contextObject |
||
38 | * @param bool $finalClass |
||
39 | * |
||
40 | * @throws ConfigurationException |
||
41 | * @throws ValidationException |
||
42 | */ |
||
43 | 104 | public function __construct(array $configData, $contextObject = null, $finalClass = false) |
|
61 | |||
62 | 101 | public function getContextRules() |
|
75 | |||
76 | abstract public function getRules(); |
||
77 | |||
78 | 54 | public function getName() |
|
82 | |||
83 | 3 | public function getType() |
|
84 | 1 | { |
|
85 | 3 | return $this->get('type'); |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * @return null|callable |
||
90 | */ |
||
91 | 23 | public function getResolveFunction() |
|
95 | |||
96 | /** |
||
97 | * @return string|null |
||
98 | */ |
||
99 | 1 | public function getResolveString() |
|
103 | |||
104 | 49 | protected function build() |
|
107 | |||
108 | /** |
||
109 | * @param $key |
||
110 | * @param null $defaultValue |
||
111 | * @return mixed|null|callable |
||
112 | */ |
||
113 | 68 | public function get($key, $defaultValue = null) |
|
117 | |||
118 | 9 | public function set($key, $value) |
|
124 | |||
125 | 2 | public function __call($method, $arguments) |
|
142 | |||
143 | |||
144 | } |
||
145 |
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.