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 |
||
18 | class ScenarioNode implements ScenarioInterface |
||
19 | { |
||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $title; |
||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | private $tags = array(); |
||
28 | /** |
||
29 | * @var StepNode[] |
||
30 | */ |
||
31 | private $steps = array(); |
||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $keyword; |
||
36 | /** |
||
37 | * @var integer |
||
38 | */ |
||
39 | private $line; |
||
40 | |||
41 | /** |
||
42 | * Initializes scenario. |
||
43 | * |
||
44 | * @param null|string $title |
||
45 | * @param array $tags |
||
46 | * @param StepNode[] $steps |
||
47 | * @param string $keyword |
||
48 | * @param integer $line |
||
49 | */ |
||
50 | 245 | View Code Duplication | public function __construct($title, array $tags, array $steps, $keyword, $line) |
58 | |||
59 | /** |
||
60 | * Returns node type string |
||
61 | * |
||
62 | * @return string |
||
63 | */ |
||
64 | public function getNodeType() |
||
68 | |||
69 | /** |
||
70 | * Returns scenario title. |
||
71 | * |
||
72 | * @return null|string |
||
73 | */ |
||
74 | 7 | public function getTitle() |
|
78 | |||
79 | /** |
||
80 | * Checks if scenario is tagged with tag. |
||
81 | * |
||
82 | * @param string $tag |
||
83 | * |
||
84 | * @return Boolean |
||
85 | */ |
||
86 | public function hasTag($tag) |
||
90 | |||
91 | /** |
||
92 | * Checks if scenario has tags (both inherited from feature and own). |
||
93 | * |
||
94 | * @return Boolean |
||
95 | */ |
||
96 | 1 | public function hasTags() |
|
100 | |||
101 | /** |
||
102 | * Returns scenario tags (including inherited from feature). |
||
103 | * |
||
104 | * @return array |
||
105 | */ |
||
106 | 3 | public function getTags() |
|
110 | |||
111 | /** |
||
112 | * Checks if scenario has steps. |
||
113 | * |
||
114 | * @return Boolean |
||
115 | */ |
||
116 | 1 | public function hasSteps() |
|
120 | |||
121 | /** |
||
122 | * Returns scenario steps. |
||
123 | * |
||
124 | * @return StepNode[] |
||
125 | */ |
||
126 | 3 | public function getSteps() |
|
130 | |||
131 | /** |
||
132 | * Returns scenario keyword. |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | public function getKeyword() |
||
140 | |||
141 | /** |
||
142 | * Returns scenario declaration line number. |
||
143 | * |
||
144 | * @return integer |
||
145 | */ |
||
146 | 16 | public function getLine() |
|
150 | } |
||
151 |
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.