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 |
||
31 | class GitStack extends CommandStack |
||
32 | { |
||
33 | /** |
||
34 | * @param string $pathToGit |
||
35 | */ |
||
36 | public function __construct($pathToGit = 'git') |
||
37 | { |
||
38 | $this->executable = $pathToGit; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Executes `git clone` |
||
43 | * |
||
44 | * @param string $repo |
||
45 | * @param string $to |
||
46 | * @param string $branch |
||
47 | * |
||
48 | * @return $this |
||
49 | */ |
||
50 | View Code Duplication | public function cloneRepo($repo, $to = "", $branch = "") |
|
|
|||
51 | { |
||
52 | $cmd = ['clone', $repo, $to]; |
||
53 | if (!empty($branch)) { |
||
54 | $cmd[] = "--branch $branch"; |
||
55 | } |
||
56 | return $this->exec($cmd); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Executes `git clone` with depth 1 as default |
||
61 | * |
||
62 | * @param string $repo |
||
63 | * @param string $to |
||
64 | * @param string $branch |
||
65 | * @param int $depth |
||
66 | * |
||
67 | * @return $this |
||
68 | */ |
||
69 | View Code Duplication | public function cloneShallow($repo, $to = '', $branch = "", $depth = 1) |
|
78 | |||
79 | /** |
||
80 | * Executes `git add` command with files to add pattern |
||
81 | * |
||
82 | * @param string $pattern |
||
83 | * |
||
84 | * @return $this |
||
85 | */ |
||
86 | public function add($pattern) |
||
90 | |||
91 | /** |
||
92 | * Executes `git commit` command with a message |
||
93 | * |
||
94 | * @param string $message |
||
95 | * @param string $options |
||
96 | * |
||
97 | * @return $this |
||
98 | */ |
||
99 | public function commit($message, $options = "") |
||
104 | |||
105 | /** |
||
106 | * Executes `git pull` command. |
||
107 | * |
||
108 | * @param string $origin |
||
109 | * @param string $branch |
||
110 | * |
||
111 | * @return $this |
||
112 | */ |
||
113 | public function pull($origin = '', $branch = '') |
||
117 | |||
118 | /** |
||
119 | * Executes `git push` command |
||
120 | * |
||
121 | * @param string $origin |
||
122 | * @param string $branch |
||
123 | * |
||
124 | * @return $this |
||
125 | */ |
||
126 | public function push($origin = '', $branch = '') |
||
130 | |||
131 | /** |
||
132 | * Performs git merge |
||
133 | * |
||
134 | * @param string $branch |
||
135 | * |
||
136 | * @return $this |
||
137 | */ |
||
138 | public function merge($branch) |
||
142 | |||
143 | /** |
||
144 | * Executes `git checkout` command |
||
145 | * |
||
146 | * @param string $branch |
||
147 | * |
||
148 | * @return $this |
||
149 | */ |
||
150 | public function checkout($branch) |
||
154 | |||
155 | /** |
||
156 | * Executes `git tag` command |
||
157 | * |
||
158 | * @param string $tag_name |
||
159 | * @param string $message |
||
160 | * |
||
161 | * @return $this |
||
162 | */ |
||
163 | View Code Duplication | public function tag($tag_name, $message = "") |
|
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | public function run() |
||
179 | } |
||
180 |
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.