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 |
||
12 | class Serializer |
||
13 | { |
||
14 | /** |
||
15 | * @var Escaper |
||
16 | */ |
||
17 | protected $escaper; |
||
18 | |||
19 | /** |
||
20 | * @var OutputInterface |
||
21 | */ |
||
22 | protected $output; |
||
23 | |||
24 | /** |
||
25 | * @param OutputInterface $output |
||
26 | * @param Escaper $escaper |
||
27 | */ |
||
28 | 13 | public function __construct(OutputInterface $output, Escaper $escaper) |
|
33 | |||
34 | /** |
||
35 | * Serialize given strings into a regular expression |
||
36 | * |
||
37 | * @param array[] $strings |
||
38 | * @return string |
||
39 | */ |
||
40 | 13 | public function serializeStrings(array $strings) |
|
53 | |||
54 | /** |
||
55 | * Analyze given strings to determine how to serialize them |
||
56 | * |
||
57 | * The returned array may contains any of the following elements: |
||
58 | * |
||
59 | * - (string) quantifier Either '' or '?' |
||
60 | * - (array) chars List of values from single-char strings |
||
61 | * - (array) strings List of multi-char strings |
||
62 | * |
||
63 | * @param array[] $strings |
||
64 | * @return array |
||
65 | */ |
||
66 | 13 | protected function analyzeStrings(array $strings) |
|
86 | |||
87 | /** |
||
88 | * Build the list of alternations based on given info |
||
89 | * |
||
90 | * @param array $info |
||
91 | * @return string[] |
||
92 | */ |
||
93 | 13 | protected function buildAlternations(array $info) |
|
107 | |||
108 | /** |
||
109 | * Return the portion of strings that are composed of a single character |
||
110 | * |
||
111 | * @param array[] |
||
112 | * @return array String key => codepoint |
||
113 | */ |
||
114 | 13 | View Code Duplication | protected function getChars(array $strings) |
127 | |||
128 | /** |
||
129 | * Get the list of ranges that cover all given values |
||
130 | * |
||
131 | * @param integer[] $values Ordered list of values |
||
132 | * @return array[] List of ranges in the form [start, end] |
||
133 | */ |
||
134 | 10 | protected function getRanges(array $values) |
|
157 | |||
158 | /** |
||
159 | * Test whether a string is optional and has more than one character |
||
160 | * |
||
161 | * @param array $info |
||
162 | * @return bool |
||
163 | */ |
||
164 | 13 | protected function isOneOptionalString(array $info) |
|
169 | |||
170 | /** |
||
171 | * Serialize a given list of values into a character class |
||
172 | * |
||
173 | * @param integer[] $values |
||
174 | * @return string |
||
175 | */ |
||
176 | 10 | protected function serializeCharacterClass(array $values) |
|
195 | |||
196 | /** |
||
197 | * Serialize a given string into a regular expression |
||
198 | * |
||
199 | * @param array $string |
||
200 | * @return string |
||
201 | */ |
||
202 | 5 | protected function serializeString(array $string) |
|
212 | } |
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.