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 |
||
| 38 | class IpstackMiddleware implements MiddlewareInterface |
||
| 39 | { |
||
| 40 | use LoggerAwareTrait; |
||
| 41 | |||
| 42 | |||
| 43 | /** |
||
| 44 | * Maps ipstack response fields to Request attribute Names. |
||
| 45 | * |
||
| 46 | * Array keys are ipstack fields, values are attribute names, |
||
| 47 | * for example: |
||
| 48 | * |
||
| 49 | * array( |
||
| 50 | * "country_code" => "X-IpstackCountryCode", |
||
| 51 | * "language" => "X-IpstackLanguage" |
||
| 52 | * ); |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | public $ipstack_attributes = array(); |
||
| 57 | |||
| 58 | |||
| 59 | /** |
||
| 60 | * Specifies the ipstack response fields that will be requested always. |
||
| 61 | * |
||
| 62 | * @see https://ipstack.com/documentation#fields |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | public $ipstack_default_fields = array("ip", "country_code", "country_name"); |
||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * Request attribute with IP address as described here: |
||
| 70 | * http://www.slimframework.com/docs/v3/cookbook/ip-address.html |
||
| 71 | * |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | public $ip_address_attribute = "ip_address"; |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * Request attribute to store the full ipstack information |
||
| 79 | * |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | public $ipstack_attribute = "ipstack"; |
||
| 83 | |||
| 84 | |||
| 85 | /** |
||
| 86 | * @var IpstackClientInterface |
||
| 87 | */ |
||
| 88 | public $ipstack_client; |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * @var integer |
||
| 93 | */ |
||
| 94 | public $reponse_error_code = 400; |
||
| 95 | |||
| 96 | |||
| 97 | /** |
||
| 98 | * @param IpstackClientInterface $ipstack_client IpstackClient |
||
| 99 | * @param string $ip_address_attribute Optional: Request attribute name with Client IP address |
||
| 100 | * @param array $request_attributes Optional: Map ipstack fields to request attributes |
||
|
|
|||
| 101 | * @param LoggerInterface|null $logger Optional: PSR-3 Logger |
||
| 102 | */ |
||
| 103 | 48 | View Code Duplication | public function __construct( IpstackClientInterface $ipstack_client, string $ip_address_attribute = null, array $ipstack_attributes = array(), LoggerInterface $logger = null ) |
| 111 | |||
| 112 | |||
| 113 | /** |
||
| 114 | * PSR-15 "Single pass" pattern |
||
| 115 | * |
||
| 116 | * @param ServerRequestInterface $request [description] |
||
| 117 | * @param RequestHandlerInterface $handler [description] |
||
| 118 | * @return ResponseInterface |
||
| 119 | */ |
||
| 120 | 40 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface |
|
| 137 | |||
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * Slim3-style "Double Pass" pattern |
||
| 142 | * |
||
| 143 | * @param ServerRequestInterface $request |
||
| 144 | * @param ResponseInterface $response |
||
| 145 | * @param callable $next |
||
| 146 | * |
||
| 147 | * @return ResponseInterface |
||
| 148 | */ |
||
| 149 | 40 | public function __invoke( ServerRequestInterface $request, ResponseInterface $response, callable $next ) |
|
| 167 | |||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * Perfoms the middelware action |
||
| 172 | * |
||
| 173 | * @param ServerRequestInterface $request |
||
| 174 | * @return bool |
||
| 175 | */ |
||
| 176 | 40 | protected function business( ServerRequestInterface $request ) |
|
| 189 | |||
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * Returns the client's IP, either from request attribute name or REMOTE_ADDR. |
||
| 194 | * |
||
| 195 | * @param ServerRequestInterface $request The request |
||
| 196 | * @return string Client IP address string |
||
| 197 | */ |
||
| 198 | 40 | protected function getClientIp(ServerRequestInterface $request) : string |
|
| 219 | |||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * Asks the ipstack API about information for the given IP. |
||
| 224 | * |
||
| 225 | * If something goes wrong, an array with default values |
||
| 226 | * will be returned. |
||
| 227 | * |
||
| 228 | * @param string $client_ip |
||
| 229 | * @return array ipstack response excerpt. |
||
| 230 | */ |
||
| 231 | 16 | protected function askIpStack( string $client_ip ) : array |
|
| 266 | |||
| 267 | |||
| 268 | |||
| 269 | /** |
||
| 270 | * Checks wether a given IP address is not empty and valid IPv4 or IP46. |
||
| 271 | * |
||
| 272 | * @param string $client_ip |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | 40 | protected function assertClientIp( string $client_ip ) : bool |
|
| 296 | |||
| 297 | } |
||
| 298 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.