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 |
||
| 19 | class PhpVarsCheckRouter implements MiddlewareInterface |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * The logger used by Splash. |
||
| 23 | * |
||
| 24 | * @var LoggerInterface |
||
| 25 | */ |
||
| 26 | private $log; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * A simple counter to check requests' length (GET, POST, REQUEST). |
||
| 30 | * |
||
| 31 | * @var int |
||
| 32 | */ |
||
| 33 | private $count; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @Important |
||
| 37 | * |
||
| 38 | * @param LoggerInterface $log The logger used by Splash |
||
| 39 | */ |
||
| 40 | public function __construct(LoggerInterface $log = null) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Get the min in 2 values if there exist. |
||
| 47 | * |
||
| 48 | * @param int $val1 |
||
| 49 | * @param int $val2 |
||
| 50 | * |
||
| 51 | * @return int|NULL |
||
| 52 | */ |
||
| 53 | private function getMinInConfiguration($val1, $val2) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Returns the number of bytes from php.ini parameter. |
||
| 70 | * |
||
| 71 | * @param $val |
||
| 72 | * |
||
| 73 | * @return int|string |
||
| 74 | */ |
||
| 75 | private static function iniGetBytes($val) |
||
| 76 | { |
||
| 77 | $val = trim(ini_get($val)); |
||
| 78 | if ($val != '') { |
||
| 79 | $last = strtolower( |
||
| 80 | $val{strlen($val) - 1} |
||
| 81 | ); |
||
| 82 | } else { |
||
| 83 | $last = ''; |
||
| 84 | } |
||
| 85 | $val = (int) $val; |
||
| 86 | switch ($last) { |
||
| 87 | // The 'G' modifier is available since PHP 5.1.0 |
||
| 88 | case 'g': |
||
| 89 | $val *= 1024; |
||
| 90 | case 'm': |
||
| 91 | $val *= 1024; |
||
| 92 | case 'k': |
||
| 93 | $val *= 1024; |
||
| 94 | } |
||
| 95 | |||
| 96 | return $val; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Count number of element in array. |
||
| 101 | * |
||
| 102 | * @param mixed $item |
||
| 103 | * @param mixed $key |
||
| 104 | */ |
||
| 105 | private function countRecursive($item, $key) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Process an incoming request and/or response. |
||
| 112 | * |
||
| 113 | * Accepts a server-side request and a response instance, and does |
||
| 114 | * something with them. |
||
| 115 | * |
||
| 116 | * If the response is not complete and/or further processing would not |
||
| 117 | * interfere with the work done in the middleware, or if the middleware |
||
| 118 | * wants to delegate to another process, it can use the `$out` callable |
||
| 119 | * if present. |
||
| 120 | * |
||
| 121 | * If the middleware does not return a value, execution of the current |
||
| 122 | * request is considered complete, and the response instance provided will |
||
| 123 | * be considered the response to return. |
||
| 124 | * |
||
| 125 | * Alternately, the middleware may return a response instance. |
||
| 126 | * |
||
| 127 | * Often, middleware will `return $out();`, with the assumption that a |
||
| 128 | * later middleware will return a response. |
||
| 129 | * |
||
| 130 | * @param Request $request |
||
| 131 | * @param Response $response |
||
| 132 | * @param null|callable $out |
||
| 133 | * |
||
| 134 | * @return null|Response |
||
| 135 | * |
||
| 136 | * @throws SplashException |
||
| 137 | */ |
||
| 138 | public function __invoke(Request $request, Response $response, callable $out = null) |
||
| 199 | } |
||
| 200 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.