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 // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase |
||
17 | class SemverVersioning implements VersioningPlugin { |
||
18 | use PluginTrait; |
||
19 | |||
20 | /** |
||
21 | * Parse a semver version. |
||
22 | * |
||
23 | * @param string $version Version. |
||
24 | * @return array With components: |
||
25 | * - major: (int) Major version. |
||
26 | * - minor: (int) Minor version. |
||
27 | * - patch: (int) Patch version. |
||
28 | * - prerelease: (string|null) Pre-release string. |
||
29 | * - buildinfo: (string|null) Build metadata string. |
||
30 | * @throws InvalidArgumentException If the version number is not in a recognized format. |
||
31 | */ |
||
32 | public function parseVersion( $version ) { |
||
45 | |||
46 | /** |
||
47 | * Check and normalize a version number. |
||
48 | * |
||
49 | * @param string|array $version Version string, or array as from `parseVersion()`. |
||
50 | * @return string Normalized version. |
||
51 | * @throws InvalidArgumentException If the version number is not in a recognized format. |
||
52 | */ |
||
53 | public function normalizeVersion( $version ) { |
||
83 | |||
84 | /** |
||
85 | * Validate an `$extra` array. |
||
86 | * |
||
87 | * @param array $extra Extra components for the version. See `nextVersion()`. |
||
88 | * @return array |
||
89 | * @throws InvalidArgumentException If the `$extra` data is invalid. |
||
90 | */ |
||
91 | View Code Duplication | private function validateExtra( array $extra ) { |
|
115 | |||
116 | /** |
||
117 | * Determine the next version given a current version and a set of changes. |
||
118 | * |
||
119 | * @param string $version Current version. |
||
120 | * @param ChangeEntry[] $changes Changes. |
||
121 | * @param array $extra Extra components for the version. |
||
122 | * - prerelease: (string|null) Prerelease version, e.g. "dev", "alpha", or "beta", if any. See semver docs for accepted values. |
||
123 | * - buildinfo: (string|null) Build info, if any. See semver docs for accepted values. |
||
124 | * @return string |
||
125 | * @throws InvalidArgumentException If the version number is not in a recognized format, or other arguments are invalid. |
||
126 | */ |
||
127 | public function nextVersion( $version, array $changes, array $extra = array() ) { |
||
159 | |||
160 | /** |
||
161 | * Compare two version numbers. |
||
162 | * |
||
163 | * @param string $a First version. |
||
164 | * @param string $b Second version. |
||
165 | * @return int Less than, equal to, or greater than 0 depending on whether `$a` is less than, equal to, or greater than `$b`. |
||
166 | * @throws InvalidArgumentException If the version numbers are not in a recognized format. |
||
167 | */ |
||
168 | public function compareVersions( $a, $b ) { |
||
214 | |||
215 | /** |
||
216 | * Return a valid "first" version number. |
||
217 | * |
||
218 | * @param array $extra Extra components for the version, as for `nextVersion()`. |
||
219 | * @return string |
||
220 | */ |
||
221 | public function firstVersion( array $extra = array() ) { |
||
230 | |||
231 | } |
||
232 |