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 namespace Mascame\Artificer\Fields; |
||
5 | class FieldParser |
||
6 | { |
||
7 | /** |
||
8 | * @var array |
||
9 | */ |
||
10 | protected $types = []; |
||
11 | |||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | public $typeReason = []; |
||
16 | |||
17 | public function __construct(array $types) |
||
21 | |||
22 | /** |
||
23 | * @return array |
||
24 | */ |
||
25 | public function getTypes() |
||
29 | |||
30 | /** |
||
31 | * @param $field |
||
32 | * @return bool|int|mixed|string |
||
33 | */ |
||
34 | public function parse($field) |
||
44 | |||
45 | /** |
||
46 | * @param $name |
||
47 | * @param $types |
||
48 | * @return bool |
||
49 | */ |
||
50 | public function isTypeEqual($name, $types) |
||
62 | |||
63 | /** |
||
64 | * @param $fields |
||
65 | * @param $name |
||
66 | * @param $type |
||
67 | * @return int |
||
68 | */ |
||
69 | public function getSimilarityPoints($fields, $name, $type) |
||
86 | |||
87 | /** |
||
88 | * @param $name |
||
89 | * @param $types |
||
90 | * @return bool|mixed |
||
91 | */ |
||
92 | public function isTypeSimilar($name, $types) |
||
112 | |||
113 | /** |
||
114 | * @param $haystack |
||
115 | * @param $needle |
||
116 | * @return bool |
||
117 | */ |
||
118 | public function isSimilar($haystack, $needle) |
||
124 | |||
125 | /** |
||
126 | * @param $name |
||
127 | * @param $types |
||
128 | * @return bool|int|string |
||
129 | */ |
||
130 | public function isUserType($name, $types) |
||
148 | |||
149 | /** |
||
150 | * @param $name |
||
151 | * @param $types |
||
152 | * @return bool|int|string |
||
153 | */ |
||
154 | public function matchesRegex($name, $types) |
||
170 | |||
171 | /** |
||
172 | * @param $name |
||
173 | * @param $types |
||
174 | * @return bool|int|mixed|string |
||
175 | */ |
||
176 | public function detectType($name) |
||
198 | |||
199 | /** |
||
200 | * @param $name |
||
201 | * @param $value |
||
202 | */ |
||
203 | protected function setTypeReason($name, $value) |
||
207 | |||
208 | } |
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.