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 |
||
14 | final class Configuration implements ConfigurationInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | const STANDARDS = 'standards'; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | const SNIFFS = 'sniffs'; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | const EXCLUDED_SNIFFS = 'exclude-sniffs'; |
||
30 | |||
31 | /** |
||
32 | * @var MultiCsFileLoaderInterface |
||
33 | */ |
||
34 | private $multiCsFileLoader; |
||
35 | |||
36 | /** |
||
37 | * @var SniffNamingInterface |
||
38 | */ |
||
39 | private $sniffNaming; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | private $multiCsFile; |
||
45 | |||
46 | 5 | public function __construct(MultiCsFileLoaderInterface $multiCsFileLoader, SniffNamingInterface $sniffNaming) |
|
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | 5 | View Code Duplication | public function getActiveSniffs() |
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | 5 | View Code Duplication | public function getActiveStandards() |
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | 2 | View Code Duplication | public function getExcludedSniffs() |
93 | |||
94 | /** |
||
95 | * @return string[] |
||
96 | */ |
||
97 | 3 | private function normalizeSniffsFromClassesToUnderscoreLowercase(array $sniffs) |
|
101 | |||
102 | 5 | private function ensureMultiJsFileIsLoaded() |
|
108 | } |
||
109 |
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.