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 |
||
16 | class Emoji { |
||
17 | |||
18 | /** |
||
19 | * Get the Emojis from emoji.php |
||
20 | * @return array |
||
21 | */ |
||
22 | private function getEmojis() : array |
||
26 | |||
27 | /** |
||
28 | * Get the emoji by passing the commonly used emoji name |
||
29 | * @param string $emojiName |
||
30 | * @return unicode |
||
31 | * @throws \Unicodeveloper\Emoji\Exceptions\IsNull |
||
32 | * @throws \Unicodeveloper\Emoji\Exceptions\UnknownUnicode |
||
33 | */ |
||
34 | View Code Duplication | public function findByAlias($emojiName = null) : string |
|
48 | |||
49 | /** |
||
50 | * Get the emoji by passing the commonly used emoji name |
||
51 | * Alias for findByAlias |
||
52 | * @param string $emojiName |
||
53 | * @return unicode |
||
54 | */ |
||
55 | public function findByName($emojiName = null) : string |
||
59 | |||
60 | /** |
||
61 | * Get the emoji name by passing the unicode value |
||
62 | * @param string $unicode |
||
63 | * @return string |
||
64 | * @throws \Unicodeveloper\Emoji\Exceptions\IsNull |
||
65 | * @throws \Unicodeveloper\Emoji\Exceptions\UnknownUnicode |
||
66 | */ |
||
67 | View Code Duplication | public function findByUnicode($unicode = null) : string |
|
81 | |||
82 | /** |
||
83 | * Ensure that a proper exception is thrown for methods that do not exist |
||
84 | * @param string $method |
||
85 | * @param array $parameters |
||
86 | * @throws \Unicodeveloper\Emoji\Exceptions\UnknownMethod |
||
87 | */ |
||
88 | public function __call($method, $parameters) |
||
94 | |||
95 | } |
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.