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

HeaderItem::setValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\request\header;
3
4
/**
5
 * HTTP请求头
6
 *
7
 */
8
class HeaderItem
9
{
10
    /**
11
     * 头部名
12
     *
13
     * @var string
14
     */
15
    protected $name;
16
    /**
17
     * 头部值
18
     *
19
     * @var mixed
20
     */
21
    protected $value;
22
23
    public function __construct(string $name, string $value)
24
    {
25
        $name = \strtolower(\str_replace('_', $name));
0 ignored issues
show
Bug introduced by
The call to str_replace() has too few arguments starting with subject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
        $name = \strtolower(/** @scrutinizer ignore-call */ \str_replace('_', $name));

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
26
        $this->name = $name;
27
        $this->value = $value;
28
    }
29
30
    /**
31
     * 从 $_SERVER 变量获取
32
     *
33
     * @param array $server
34
     * @return HeaderItem[] 头部数组
35
     */
36
    public static function buildFromServer(array $server):array
37
    {
38
        $headers = [];
39
        foreach ($server as $key => $value) {
40
            if (strpos($key, 'HTTP_') === 0) {
41
                $name = substr($key, strlen('HTTP_'));
42
                $headers[$name] = $value;
43
            }
44
        }
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 array. 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...
45
    }
46
47
    /**
48
     * 获取头部信息
49
     *
50
     * @param array $headers
51
     * @return HeaderItem[]
52
     */
53
    public static function build(array $headers):array
54
    {
55
        $build = [];
56
        foreach ($headers as $name => $value) {
57
            if ($value instanceof HeaderItem) {
58
                $header = $value;
59
            } else {
60
                $header = new HeaderItem($name, $value);
61
            }
62
            $build[$header->getName()] = $header;
63
        }
64
        return $build;
65
    }
66
67
    /**
68
     * Get 头部名
69
     *
70
     * @return  string
71
     */
72
    public function getName()
73
    {
74
        return $this->name;
75
    }
76
77
    /**
78
     * Set 头部名
79
     *
80
     * @param  string  $name  头部名
81
     *
82
     * @return  self
83
     */
84
    public function setName(string $name)
85
    {
86
        $this->name = $name;
87
88
        return $this;
89
    }
90
91
    /**
92
     * Get 头部值
93
     *
94
     * @return  mixed
95
     */
96
    public function getValue()
97
    {
98
        return $this->value;
99
    }
100
101
    /**
102
     * Set 头部值
103
     *
104
     * @param  mixed  $value  头部值
105
     *
106
     * @return  self
107
     */
108
    public function setValue($value)
109
    {
110
        $this->value = $value;
111
112
        return $this;
113
    }
114
}
115