1 | <?php |
||
34 | class IpstackMiddleware |
||
35 | { |
||
36 | use LoggerAwareTrait; |
||
37 | |||
38 | |||
39 | /** |
||
40 | * Maps ipstack response fields to Request attribute Names. |
||
41 | * |
||
42 | * Array keys are ipstack fields, values are attribute names, |
||
43 | * for example: |
||
44 | * |
||
45 | * array( |
||
46 | * "country_code" => "X-IpstackCountryCode", |
||
47 | * "language" => "X-IpstackLanguage" |
||
48 | * ); |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | public $ipstack_attributes = array(); |
||
53 | |||
54 | |||
55 | /** |
||
56 | * Specifies the ipstack response fields that will be requested always. |
||
57 | * |
||
58 | * @see https://ipstack.com/documentation#fields |
||
59 | * @var array |
||
60 | */ |
||
61 | public $ipstack_default_fields = array("ip", "country_code", "country_name"); |
||
62 | |||
63 | |||
64 | /** |
||
65 | * Request attribute with IP address as described here: |
||
66 | * http://www.slimframework.com/docs/v3/cookbook/ip-address.html |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | public $ip_address_attribute = "ip_address"; |
||
71 | |||
72 | |||
73 | /** |
||
74 | * Request attribute to store the full ipstack information |
||
75 | * |
||
76 | * @var string |
||
77 | */ |
||
78 | public $ipstack_attribute = "ipstack"; |
||
79 | |||
80 | |||
81 | /** |
||
82 | * @var IpstackClientInterface |
||
83 | */ |
||
84 | public $ipstack_client; |
||
85 | |||
86 | |||
87 | /** |
||
88 | * @param IpstackClientInterface $ipstack_client IpstackClient |
||
89 | * @param string $ip_address_attribute Optional: Request attribute name with Client IP address |
||
90 | * @param array $request_attributes Optional: Map ipstack fields to request attributes |
||
|
|||
91 | * @param LoggerInterface|null $logger Optional: PSR-3 Logger |
||
92 | */ |
||
93 | 48 | public function __construct( IpstackClientInterface $ipstack_client, string $ip_address_attribute = null, array $ipstack_attributes = array(), LoggerInterface $logger = null ) |
|
101 | |||
102 | |||
103 | /** |
||
104 | * @param ServerRequestInterface $request |
||
105 | * @param ResponseInterface $response |
||
106 | * @param callable $next |
||
107 | * |
||
108 | * @return ResponseInterface |
||
109 | */ |
||
110 | 40 | public function __invoke( ServerRequestInterface $request, ResponseInterface $response, callable $next ) |
|
133 | |||
134 | |||
135 | |||
136 | /** |
||
137 | * Returns the client's IP, either from request attribute name or REMOTE_ADDR. |
||
138 | * |
||
139 | * @param ServerRequestInterface $request The request |
||
140 | * @return string Client IP address string |
||
141 | */ |
||
142 | 40 | protected function getClientIp(ServerRequestInterface $request) : string |
|
163 | |||
164 | |||
165 | |||
166 | /** |
||
167 | * Asks the ipstack API about information for the given IP. |
||
168 | * |
||
169 | * If something goes wrong, an array with default values |
||
170 | * will be returned. |
||
171 | * |
||
172 | * @param string $client_ip |
||
173 | * @return array ipstack response excerpt. |
||
174 | */ |
||
175 | 16 | protected function askIpStack( string $client_ip ) : array |
|
210 | |||
211 | |||
212 | |||
213 | /** |
||
214 | * Checks wether a given IP address is not empty and valid IPv4 or IP46. |
||
215 | * |
||
216 | * @param string $client_ip |
||
217 | * @return bool |
||
218 | */ |
||
219 | 40 | protected function assertClientIp( string $client_ip ) : bool |
|
240 | |||
241 | } |
||
242 |
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.