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 |
||
9 | final class Factory |
||
10 | { |
||
11 | private $calendars; |
||
12 | |||
13 | private $translators; |
||
14 | |||
15 | /** |
||
16 | * @param Calendar[] $calendars |
||
17 | * @param array[] $translators |
||
18 | */ |
||
19 | 12 | public function __construct(array $calendars, array $translators) |
|
26 | |||
27 | 12 | public static function create() |
|
41 | |||
42 | /** |
||
43 | * Get a translator for a country and locale. |
||
44 | * |
||
45 | * @param string $alpha3 https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3 |
||
46 | * @param string $locale https://msdn.microsoft.com/en-us/library/windows/desktop/dd757512(v=vs.85).aspx |
||
47 | * |
||
48 | * @throws NoTranslatorFound |
||
49 | * |
||
50 | * @return Translator |
||
51 | */ |
||
52 | 6 | public function getTranslator($alpha3, $locale) |
|
64 | |||
65 | /** |
||
66 | * Get a calendar for a country. |
||
67 | * |
||
68 | * @param string $alpha3 https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3 |
||
69 | * |
||
70 | * @throws NoCalendarFound |
||
71 | * |
||
72 | * @return Calendar |
||
73 | */ |
||
74 | 4 | public function getCalendar($alpha3) |
|
82 | |||
83 | 12 | private function guardThatTheseAreCalendars(array $calendars) |
|
98 | |||
99 | 12 | private function guardThatTheseAreTranslators(array $translators) |
|
125 | } |
||
126 |
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.