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 |
||
| 12 | class Parser |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * The build metadata component. |
||
| 16 | */ |
||
| 17 | const BUILD = 'build'; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The major version number component. |
||
| 21 | */ |
||
| 22 | const MAJOR = 'major'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The minor version number component. |
||
| 26 | */ |
||
| 27 | const MINOR = 'minor'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The patch version number component. |
||
| 31 | */ |
||
| 32 | const PATCH = 'patch'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The pre-release version number component. |
||
| 36 | */ |
||
| 37 | const PRE_RELEASE = 'pre'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Returns a Version builder for the string representation. |
||
| 41 | * |
||
| 42 | * @param string $version The string representation. |
||
| 43 | * |
||
| 44 | * @return Builder A Version builder. |
||
| 45 | */ |
||
| 46 | public static function toBuilder($version) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Returns the components of the string representation. |
||
| 55 | * |
||
| 56 | * @param string $version The string representation. |
||
| 57 | * |
||
| 58 | * @return array The components of the version. |
||
| 59 | * |
||
| 60 | * @throws InvalidStringRepresentationException If the string representation |
||
| 61 | * is invalid. |
||
| 62 | */ |
||
| 63 | public static function toComponents($version) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Returns a Version instance for the string representation. |
||
| 98 | * |
||
| 99 | * @param string $version The string representation. |
||
| 100 | * |
||
| 101 | * @return Version A Version instance. |
||
| 102 | */ |
||
| 103 | public static function toVersion($version) |
||
| 115 | } |
||
| 116 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.