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 |
||
11 | class Remote implements LoggerAwareInterface |
||
12 | { |
||
13 | use ExecWithRedactionTrait; |
||
14 | use LoggerAwareTrait; |
||
15 | |||
16 | protected $remote; |
||
17 | |||
18 | public function __construct($remote) |
||
22 | |||
23 | public static function create($remote, $api = null) |
||
29 | |||
30 | public static function fromDir($dir, $remote = 'origin') |
||
35 | |||
36 | public function addAuthentication($api = null) |
||
42 | |||
43 | public function valid() |
||
47 | |||
48 | public function projectWithOrg() |
||
52 | |||
53 | // https://{$token}:[email protected]/{$projectWithOrg}.git"; |
||
54 | // [email protected]:{$projectWithOrg}.git |
||
55 | View Code Duplication | public static function projectWithOrgFromUrl($remote) |
|
63 | |||
64 | public function url() |
||
68 | |||
69 | public function org() |
||
75 | |||
76 | public function project() |
||
82 | |||
83 | public function host() |
||
90 | |||
91 | /** |
||
92 | * Return an associative array of tag names -> sha |
||
93 | * |
||
94 | * @return array |
||
95 | */ |
||
96 | public function tags($constraint_arg, $stable = true, $tag_prefix = '') |
||
117 | |||
118 | /** |
||
119 | * Delete a tag from the remote |
||
120 | */ |
||
121 | public function delete($tag) |
||
125 | |||
126 | protected function satisfies($tag, $version_constraints) |
||
139 | |||
140 | // @deprecated: we should just use semver everywhere, not regex. |
||
141 | // The downside to this theory is that WordPress doesn't use semver. |
||
142 | protected function appearsToBeSemver($arg) |
||
146 | |||
147 | public function releases($majorVersion /*= '[0-9]+'*/, $stable, $tag_prefix) |
||
151 | |||
152 | /** |
||
153 | * Return the latest release in the specified major version series |
||
154 | * |
||
155 | * TODO: allow for beta or RC builds (by request via a second parameter) |
||
156 | */ |
||
157 | public function latest($majorVersion /*= '[0-9]+'*/, $stable, $tag_prefix) |
||
163 | |||
164 | /** |
||
165 | * Return 'true' if the provided tag exists on this remote. |
||
166 | */ |
||
167 | public function has($tag_to_check, $majorVersion = '[0-9]+', $stable = false, $tag_prefix = '') |
||
172 | |||
173 | /** |
||
174 | * Return a sanitized version of this remote (sans authentication string) |
||
175 | */ |
||
176 | public function __toString() |
||
183 | |||
184 | /** |
||
185 | * Run a git function on the local working copy. Fail on error. |
||
186 | * |
||
187 | * @return string stdout |
||
188 | */ |
||
189 | public function git($cmd, $replacements = [], $redacted = []) |
||
193 | } |
||
194 |
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.