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 |
||
24 | class HgStack extends CommandStack |
||
25 | { |
||
26 | |||
27 | /** |
||
28 | * @param string $pathToHg |
||
29 | */ |
||
30 | public function __construct($pathToHg = 'hg') |
||
34 | |||
35 | /** |
||
36 | * Executes `hg clone` |
||
37 | * |
||
38 | * @param string $repo |
||
39 | * @param string $to |
||
40 | * |
||
41 | * @return $this |
||
42 | */ |
||
43 | public function cloneRepo($repo, $to = '') |
||
47 | |||
48 | /** |
||
49 | * Executes `hg add` command with files to add by pattern |
||
50 | * |
||
51 | * @param string $include |
||
52 | * @param string $exclude |
||
53 | * |
||
54 | * @return $this |
||
55 | */ |
||
56 | public function add($include = '', $exclude = '') |
||
68 | |||
69 | /** |
||
70 | * Executes `hg commit` command with a message |
||
71 | * |
||
72 | * @param string $message |
||
73 | * @param string $options |
||
74 | * |
||
75 | * @return $this |
||
76 | */ |
||
77 | public function commit($message, $options = '') |
||
81 | |||
82 | /** |
||
83 | * Executes `hg pull` command. |
||
84 | * |
||
85 | * @param string $branch |
||
86 | * |
||
87 | * @return $this |
||
88 | */ |
||
89 | View Code Duplication | public function pull($branch = '') |
|
97 | |||
98 | /** |
||
99 | * Executes `hg push` command |
||
100 | * |
||
101 | * @param string $branch |
||
102 | * |
||
103 | * @return $this |
||
104 | */ |
||
105 | View Code Duplication | public function push($branch = '') |
|
113 | |||
114 | /** |
||
115 | * Performs hg merge |
||
116 | * |
||
117 | * @param string $revision |
||
118 | * |
||
119 | * @return $this |
||
120 | */ |
||
121 | public function merge($revision = '') |
||
129 | |||
130 | /** |
||
131 | * Executes `hg tag` command |
||
132 | * |
||
133 | * @param string $tag_name |
||
134 | * @param string $message |
||
135 | * |
||
136 | * @return $this |
||
137 | */ |
||
138 | View Code Duplication | public function tag($tag_name, $message = '') |
|
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | public function run() |
||
154 | } |
||
155 |
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.