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 NameHelper |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * @param \stdClass $precedingItem |
||
23 | * @param array $currentAuthor |
||
24 | * @return bool |
||
25 | */ |
||
26 | public static function identicalAuthors($precedingItem, $currentAuthor) |
||
39 | |||
40 | /** |
||
41 | * @param \stdClass $preceding |
||
42 | * @param \stdClass $name |
||
43 | * @return bool |
||
44 | */ |
||
45 | public static function precedingHasAuthor($preceding, $name) |
||
54 | |||
55 | |||
56 | /** |
||
57 | * removes the field $particle from $data and appends its content to the $namePart field of $data |
||
58 | * @param \stdClass $data |
||
59 | * @param string $namePart |
||
60 | * @param string $particle |
||
61 | */ |
||
62 | View Code Duplication | public static function appendParticleTo(&$data, $namePart, $particle) |
|
69 | |||
70 | /** |
||
71 | * removes the field $particle from $data and prepends its content to the $namePart field of $data |
||
72 | * @param \stdClass $data |
||
73 | * @param string $namePart ("given"|"family") |
||
74 | * @param string $particle |
||
75 | */ |
||
76 | View Code Duplication | public static function prependParticleTo(&$data, $namePart, $particle) |
|
83 | |||
84 | /** |
||
85 | * @param array $persons1 |
||
86 | * @param array $persons2 |
||
87 | * @return bool |
||
88 | */ |
||
89 | public static function sameNames($persons1, $persons2) |
||
105 | |||
106 | /** |
||
107 | * @param $data |
||
108 | * @return string |
||
109 | * @throws CiteProcException |
||
110 | */ |
||
111 | public static function normalizeName($data) |
||
118 | } |
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.