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 BackgroundNode implements ScenarioLikeInterface |
||
19 | { |
||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $title; |
||
24 | /** |
||
25 | * @var StepNode[] |
||
26 | */ |
||
27 | private $steps = array(); |
||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $keyword; |
||
32 | /** |
||
33 | * @var integer |
||
34 | */ |
||
35 | private $line; |
||
36 | |||
37 | /** |
||
38 | * Initializes background. |
||
39 | * |
||
40 | * @param null|string $title |
||
41 | * @param StepNode[] $steps |
||
42 | * @param string $keyword |
||
43 | * @param integer $line |
||
44 | */ |
||
45 | 200 | View Code Duplication | public function __construct($title, array $steps, $keyword, $line) |
52 | |||
53 | /** |
||
54 | * Returns node type string |
||
55 | * |
||
56 | * @return string |
||
57 | */ |
||
58 | public function getNodeType() |
||
62 | |||
63 | /** |
||
64 | * Returns background title. |
||
65 | * |
||
66 | * @return null|string |
||
67 | */ |
||
68 | 1 | public function getTitle() |
|
69 | { |
||
70 | 1 | return $this->title; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * Checks if background has steps. |
||
75 | * |
||
76 | * @return Boolean |
||
77 | */ |
||
78 | 2 | public function hasSteps() |
|
82 | |||
83 | /** |
||
84 | * Returns background steps. |
||
85 | * |
||
86 | * @return StepNode[] |
||
87 | */ |
||
88 | 2 | public function getSteps() |
|
92 | |||
93 | /** |
||
94 | * Returns background keyword. |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | public function getKeyword() |
||
102 | |||
103 | /** |
||
104 | * Returns background declaration line number. |
||
105 | * |
||
106 | * @return integer |
||
107 | */ |
||
108 | 2 | public function getLine() |
|
112 | } |
||
113 |
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.