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 |
||
| 14 | class Bootup |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * bootstrap |
||
| 18 | */ |
||
| 19 | 1 | public static function initAll() |
|
| 20 | { |
||
| 21 | 1 | \Patchwork\Utf8\Bootup::initAll(); |
|
| 22 | 1 | } |
|
| 23 | |||
| 24 | /** |
||
| 25 | * Get random bytes |
||
| 26 | * |
||
| 27 | * @ref https://github.com/paragonie/random_compat/ |
||
| 28 | * |
||
| 29 | * @param int $length Output length |
||
| 30 | * |
||
| 31 | * @return string|false false on error |
||
| 32 | */ |
||
| 33 | 1 | public static function get_random_bytes($length) |
|
| 34 | { |
||
| 35 | 1 | if (!$length) { |
|
| 36 | 1 | return false; |
|
| 37 | } |
||
| 38 | |||
| 39 | 1 | $length = (int)$length; |
|
| 40 | |||
| 41 | 1 | if ($length <= 0) { |
|
| 42 | 1 | return false; |
|
| 43 | } |
||
| 44 | |||
| 45 | 1 | return random_bytes($length); |
|
|
|
|||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Determines if the current version of PHP is equal to or greater than the supplied value |
||
| 50 | * |
||
| 51 | * @param string |
||
| 52 | * @param string $version |
||
| 53 | * |
||
| 54 | * @return bool TRUE if the current version is $version or higher |
||
| 55 | */ |
||
| 56 | 7 | public static function is_php($version) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * filter request-uri |
||
| 70 | * |
||
| 71 | * @param null $uri |
||
| 72 | * @param bool $exit |
||
| 73 | * |
||
| 74 | * @return bool|mixed|null |
||
| 75 | */ |
||
| 76 | 2 | public static function filterRequestUri($uri = null, $exit = true) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * filter request inputs |
||
| 122 | * |
||
| 123 | * Ensures inputs are well formed UTF-8 |
||
| 124 | * When not, assumes Windows-1252 and converts to UTF-8 |
||
| 125 | * Tests only values, not keys |
||
| 126 | * |
||
| 127 | * @param int $normalization_form |
||
| 128 | * @param string $leading_combining |
||
| 129 | */ |
||
| 130 | 1 | public static function filterRequestInputs($normalization_form = 4 /* n::NFC */, $leading_combining = '◌') |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @param $s |
||
| 168 | * @param int $normalization_form |
||
| 169 | * @param string $leading_combining |
||
| 170 | * |
||
| 171 | * @return array|bool|mixed|string |
||
| 172 | */ |
||
| 173 | 1 | public static function filterString($s, $normalization_form = 4 /* n::NFC */, $leading_combining = '◌') |
|
| 201 | } |
||
| 202 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.