Passed
Push — master ( 2ddbad...aa2fc1 )
by 世昌
02:16
created

Request::getMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\request;
3
4
use nebula\request\header\HeaderItem;
5
use nebula\request\attribute\URIAttribute;
6
7
8
9
/**
10
 * HTTP请求解析器
11
 *
12
 */
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) {
62
        if (array_key_exists(strtolower($name), $this->headers)) {
63
            return $this->headers[$name];
64
        }
65
        return $default;
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
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return nebula\request\Request. Consider adding a return statement or allowing null as return value.

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:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
106
}