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 GroupSingleCharacters extends AbstractPass |
||
14 | { |
||
15 | /** |
||
16 | * {@inheritdoc} |
||
17 | */ |
||
18 | 4 | protected function runPass(array $strings) |
|
19 | { |
||
20 | 4 | $singles = $this->getSingleCharStrings($strings); |
|
21 | 4 | $cnt = count($singles); |
|
22 | 4 | if ($cnt > 1 && $cnt < count($strings)) |
|
23 | { |
||
24 | // Remove the singles from the input, then prepend them as a new string |
||
25 | 1 | $strings = array_diff_key($strings, $singles); |
|
26 | 1 | array_unshift($strings, [array_values($singles)]); |
|
27 | } |
||
28 | |||
29 | 4 | return $strings; |
|
30 | } |
||
31 | |||
32 | /** |
||
33 | * Return an array of every single-char string in given list of strings |
||
34 | * |
||
35 | * @param array[] $strings |
||
36 | * @return array[] |
||
37 | */ |
||
38 | 4 | View Code Duplication | protected function getSingleCharStrings(array $strings) |
51 | } |
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.