| Total Complexity | 11 |
| Total Lines | 91 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 13 | class Request { |
||
| 14 | |||
| 15 | use URIAttribute; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * 请求方法 |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $method; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * 请求头 |
||
| 26 | * |
||
| 27 | * @var HeaderItem[] |
||
| 28 | */ |
||
| 29 | protected $headers; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * 构建请求 |
||
| 33 | * |
||
| 34 | * @param string $method |
||
| 35 | * @param string $uri |
||
| 36 | * @param array $headers |
||
| 37 | */ |
||
| 38 | public function __construct(string $method, string $uri, array $headers = []) { |
||
| 39 | $this->uri = $uri; |
||
| 40 | $this->method = strtoupper($method); |
||
| 41 | $this->headers = HeaderItem::build($headers); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Get 请求方法 |
||
| 46 | * |
||
| 47 | * @return string |
||
| 48 | */ |
||
| 49 | public function getMethod() |
||
| 50 | { |
||
| 51 | return $this->method; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * 获取请求头 |
||
| 56 | * |
||
| 57 | * @param string $name |
||
| 58 | * @param mixed $default |
||
| 59 | * @return mixed |
||
| 60 | */ |
||
| 61 | public function getHeader(string $name, $default =null) { |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * 判断请求头 |
||
| 70 | * |
||
| 71 | * @param string $name |
||
| 72 | * @return boolean |
||
| 73 | */ |
||
| 74 | public function hasHeader(string $name) { |
||
| 75 | return $this->getHeader($name) !== null; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * 获取请求IP |
||
| 80 | * |
||
| 81 | * @return string |
||
| 82 | */ |
||
| 83 | public function ip() |
||
| 84 | { |
||
| 85 | static $ipFrom = ['HTTP_CLIENT_IP','HTTP_X_FORWARDED_FOR','HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP','HTTP_FORWARDED_FOR','HTTP_FORWARDED','REMOTE_ADDR']; |
||
| 86 | foreach ($ipFrom as $key) { |
||
| 87 | if (array_key_exists($key, $_SERVER)) { |
||
| 88 | foreach (explode(',', $_SERVER[$key]) as $ip) { |
||
| 89 | $ip = trim($ip); |
||
| 90 | if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) { |
||
| 91 | return $ip; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 | } |
||
| 96 | return '127.0.0.1'; |
||
| 97 | } |
||
| 98 | /** |
||
| 99 | * 从环境构建请求 |
||
| 100 | * |
||
| 101 | * @return Request |
||
| 102 | */ |
||
| 103 | public static function buildFromServer(): Request { |
||
| 104 | |||
| 105 | } |
||
|
|
|||
| 106 | } |
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: