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 | /** |
||
99 | * @var string |
||
100 | */ |
||
101 | public $success_loglevel = "notice"; |
||
102 | |||
103 | |||
104 | /** |
||
105 | * @var string |
||
106 | */ |
||
107 | public $invalid_ip_loglevel = "error"; |
||
108 | |||
109 | |||
110 | /** |
||
111 | * @var string |
||
112 | */ |
||
113 | public $ipstack_error_loglevel = "error"; |
||
114 | |||
115 | |||
116 | /** |
||
117 | * @param IpstackClientInterface $ipstack_client IpstackClient |
||
118 | * @param string $ip_address_attribute Optional: Request attribute name with Client IP address |
||
119 | * @param array $request_attributes Optional: Map ipstack fields to request attributes |
||
|
|||
120 | * @param LoggerInterface|null $logger Optional: PSR-3 Logger |
||
121 | */ |
||
122 | 48 | public function __construct( IpstackClientInterface $ipstack_client, string $ip_address_attribute = null, array $ipstack_attributes = array(), LoggerInterface $logger = null, string $success_loglevel = null , string $invalid_ip_loglevel = null , string $ipstack_error_loglevel = null ) |
|
133 | |||
134 | |||
135 | /** |
||
136 | * PSR-15 "Single pass" pattern |
||
137 | * |
||
138 | * @param ServerRequestInterface $request [description] |
||
139 | * @param RequestHandlerInterface $handler [description] |
||
140 | * @return ResponseInterface |
||
141 | */ |
||
142 | 40 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface |
|
160 | |||
161 | |||
162 | |||
163 | /** |
||
164 | * Slim3-style "Double Pass" pattern |
||
165 | * |
||
166 | * @param ServerRequestInterface $request |
||
167 | * @param ResponseInterface $response |
||
168 | * @param callable $next |
||
169 | * |
||
170 | * @return ResponseInterface |
||
171 | */ |
||
172 | 40 | public function __invoke( ServerRequestInterface $request, ResponseInterface $response, callable $next ) |
|
191 | |||
192 | |||
193 | |||
194 | /** |
||
195 | * Perfoms the middelware action |
||
196 | * |
||
197 | * @param ServerRequestInterface $request |
||
198 | * @return bool |
||
199 | */ |
||
200 | 40 | protected function business( ServerRequestInterface $request ) |
|
213 | |||
214 | |||
215 | |||
216 | /** |
||
217 | * Returns the client's IP, either from request attribute name or REMOTE_ADDR. |
||
218 | * |
||
219 | * @param ServerRequestInterface $request The request |
||
220 | * @return string Client IP address string |
||
221 | */ |
||
222 | 40 | protected function getClientIp(ServerRequestInterface $request) : string |
|
243 | |||
244 | |||
245 | |||
246 | /** |
||
247 | * Asks the ipstack API about information for the given IP. |
||
248 | * |
||
249 | * If something goes wrong, an array with default values |
||
250 | * will be returned. |
||
251 | * |
||
252 | * @param string $client_ip |
||
253 | * @return array ipstack response excerpt. |
||
254 | */ |
||
255 | 16 | protected function askIpStack( string $client_ip ) : array |
|
296 | |||
297 | |||
298 | |||
299 | /** |
||
300 | * Checks wether a given IP address is not empty and valid IPv4 or IP46. |
||
301 | * |
||
302 | * @param string $client_ip |
||
303 | * @return bool |
||
304 | */ |
||
305 | 40 | protected function assertClientIp( string $client_ip ) : bool |
|
326 | |||
327 | } |
||
328 |
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.