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 |
||
| 24 | class WordpressVersioning implements VersioningPlugin { |
||
| 25 | use PluginTrait; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Define any command line options the versioning plugin wants to accept. |
||
| 29 | * |
||
| 30 | * @return InputOption[] |
||
| 31 | */ |
||
| 32 | public function getOptions() { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Parse a WordPress-style version. |
||
| 40 | * |
||
| 41 | * @param string $version Version. |
||
| 42 | * @return array With components: |
||
| 43 | * - major: (float) Major version. |
||
| 44 | * - point: (int) Point version. |
||
| 45 | * - prerelease: (string|null) Pre-release string. |
||
| 46 | * - buildinfo: (string|null) Build metadata string. |
||
| 47 | * @throws InvalidArgumentException If the version number is not in a recognized format. |
||
| 48 | */ |
||
| 49 | private function parseVersion( $version ) { |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Check and normalize a version number. |
||
| 63 | * |
||
| 64 | * @param string $version Version string. |
||
| 65 | * @return string Normalized version. |
||
| 66 | * @throws InvalidArgumentException If the version number is not in a recognized format. |
||
| 67 | */ |
||
| 68 | public function normalizeVersion( $version ) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Validate an `$extra` array. |
||
| 98 | * |
||
| 99 | * @param array $extra Extra components for the version. See `nextVersion()`. |
||
| 100 | * @return array |
||
| 101 | * @throws InvalidArgumentException If the `$extra` data is invalid. |
||
| 102 | */ |
||
| 103 | View Code Duplication | private function validateExtra( array $extra ) { |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Determine the next version given a current version and a set of changes. |
||
| 130 | * |
||
| 131 | * @param string $version Current version. |
||
| 132 | * @param ChangeEntry[] $changes Changes. |
||
| 133 | * @param array $extra Extra components for the version. |
||
| 134 | * @return string |
||
| 135 | * @throws InvalidArgumentException If the version number is not in a recognized format, or other arguments are invalid. |
||
| 136 | */ |
||
| 137 | public function nextVersion( $version, array $changes, array $extra = array() ) { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Extract the index and count from a prerelease string. |
||
| 155 | * |
||
| 156 | * @param string|null $s String. |
||
| 157 | * @return array Two elements: index and count. |
||
| 158 | * @throws InvalidArgumentException If the string is invalid. |
||
| 159 | */ |
||
| 160 | private function parsePrerelease( $s ) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Compare two version numbers. |
||
| 174 | * |
||
| 175 | * @param string $a First version. |
||
| 176 | * @param string $b Second version. |
||
| 177 | * @return int Less than, equal to, or greater than 0 depending on whether `$a` is less than, equal to, or greater than `$b`. |
||
| 178 | * @throws InvalidArgumentException If the version numbers are not in a recognized format. |
||
| 179 | */ |
||
| 180 | public function compareVersions( $a, $b ) { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Return a valid "first" version number. |
||
| 201 | * |
||
| 202 | * @param array $extra Extra components for the version, as for `nextVersion()`. |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | public function firstVersion( array $extra = array() ) { |
||
| 213 | |||
| 214 | } |
||
| 215 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: