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 |
||
22 | class TagFilter extends ComplexFilter |
||
23 | { |
||
24 | protected $filterString; |
||
25 | |||
26 | /** |
||
27 | * Initializes filter. |
||
28 | * |
||
29 | * @param string $filterString Name filter string |
||
30 | */ |
||
31 | 4 | public function __construct($filterString) |
|
35 | |||
36 | /** |
||
37 | * Filters feature according to the filter. |
||
38 | * |
||
39 | * @param FeatureNode $feature |
||
40 | * |
||
41 | * @return FeatureNode |
||
42 | */ |
||
43 | 2 | public function filterFeature(FeatureNode $feature) |
|
86 | |||
87 | /** |
||
88 | * Checks if Feature matches specified filter. |
||
89 | * |
||
90 | * @param FeatureNode $feature Feature instance |
||
91 | * |
||
92 | * @return Boolean |
||
93 | */ |
||
94 | 1 | public function isFeatureMatch(FeatureNode $feature) |
|
98 | |||
99 | /** |
||
100 | * Checks if scenario or outline matches specified filter. |
||
101 | * |
||
102 | * @param FeatureNode $feature Feature node instance |
||
103 | * @param ScenarioInterface $scenario Scenario or Outline node instance |
||
104 | * |
||
105 | * @return Boolean |
||
106 | */ |
||
107 | 3 | public function isScenarioMatch(FeatureNode $feature, ScenarioInterface $scenario) |
|
121 | |||
122 | /** |
||
123 | * Checks that node matches condition. |
||
124 | * |
||
125 | * @param string[] $tags |
||
126 | * |
||
127 | * @return Boolean |
||
128 | */ |
||
129 | 4 | protected function isTagsMatchCondition($tags) |
|
152 | } |
||
153 |
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.