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 |
||
13 | class LastnameMapper extends AbstractMapper |
||
14 | { |
||
15 | protected $prefixes = []; |
||
16 | |||
17 | protected $matchSinglePart = false; |
||
18 | |||
19 | public function __construct(array $prefixes, bool $matchSinglePart = false) |
||
24 | |||
25 | /** |
||
26 | * map lastnames in the parts array |
||
27 | * |
||
28 | * @param array $parts the name parts |
||
29 | * @return array the mapped parts |
||
30 | */ |
||
31 | public function map(array $parts): array |
||
39 | |||
40 | /** |
||
41 | * we map the parts in reverse order because it makes more |
||
42 | * sense to parse for the lastname starting from the end |
||
43 | * |
||
44 | * @param array $parts |
||
45 | * @return array |
||
46 | */ |
||
47 | protected function mapParts(array $parts): array |
||
85 | |||
86 | private function isCombinedWithPrefix(string $part): bool |
||
96 | |||
97 | /** |
||
98 | * skip through the parts we want to ignore and return the start index |
||
99 | * |
||
100 | * @param array $parts |
||
101 | * @return int |
||
102 | */ |
||
103 | protected function skipIgnoredParts(array $parts): int |
||
115 | |||
116 | /** |
||
117 | * indicates if we should stop mapping at the given index $k |
||
118 | * |
||
119 | * the assumption is that lastname parts have already been found |
||
120 | * but we want to see if we should add more parts |
||
121 | * |
||
122 | * @param array $parts |
||
123 | * @param int $k |
||
124 | * @return bool |
||
125 | */ |
||
126 | protected function shouldStopMapping(array $parts, int $k): bool |
||
142 | |||
143 | /** |
||
144 | * indicates if the given part should be ignored (skipped) during mapping |
||
145 | * |
||
146 | * @param $part |
||
147 | * @return bool |
||
148 | */ |
||
149 | protected function isIgnoredPart($part) { |
||
152 | |||
153 | /** |
||
154 | * remap ignored parts as lastname |
||
155 | * |
||
156 | * if the mapping did not derive any lastname this is called to transform |
||
157 | * any previously ignored parts into lastname parts |
||
158 | * |
||
159 | * @param array $parts |
||
160 | * @return array |
||
161 | */ |
||
162 | protected function remapIgnored(array $parts): array |
||
178 | |||
179 | /** |
||
180 | * @param array $parts |
||
181 | * @param int $index |
||
182 | * @return bool |
||
183 | */ |
||
184 | protected function isFollowedByLastnamePart(array $parts, int $index): bool |
||
190 | |||
191 | /** |
||
192 | * Assuming that the part at the given index is matched as a prefix, |
||
193 | * determines if the prefix should be applied to the lastname. |
||
194 | * |
||
195 | * We only apply it to the lastname if we already have at least one |
||
196 | * lastname part and there are other parts left in |
||
197 | * the name (this effectively prioritises firstname over prefix matching). |
||
198 | * |
||
199 | * This expects the parts array and index to be in the original order. |
||
200 | * |
||
201 | * @param array $parts |
||
202 | * @param int $index |
||
203 | * @return bool |
||
204 | */ |
||
205 | protected function isApplicablePrefix(array $parts, int $index): bool |
||
213 | |||
214 | /** |
||
215 | * check if the given word is a lastname prefix |
||
216 | * |
||
217 | * @param string $word the word to check |
||
218 | * @return bool |
||
219 | */ |
||
220 | protected function isPrefix($word): bool |
||
224 | |||
225 | /** |
||
226 | * find the next non-nickname index in parts |
||
227 | * |
||
228 | * @param $parts |
||
229 | * @param $startIndex |
||
230 | * @return int|void |
||
231 | */ |
||
232 | View Code Duplication | protected function skipNicknameParts($parts, $startIndex) |
|
244 | } |
||
245 |
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.