1 | <?php |
||||||||
2 | namespace nebula\common; |
||||||||
3 | |||||||||
4 | /** |
||||||||
5 | * 数组点获取类 |
||||||||
6 | */ |
||||||||
7 | class ArrayDotAccess implements \ArrayAccess |
||||||||
8 | { |
||||||||
9 | protected $value; |
||||||||
10 | |||||||||
11 | public function __construct(array $array) |
||||||||
12 | { |
||||||||
13 | $this->value = $array; |
||||||||
14 | } |
||||||||
15 | |||||||||
16 | public function offsetSet($offset, $value) |
||||||||
17 | { |
||||||||
18 | if (is_null($offset)) { |
||||||||
19 | $this->value[] = $value; |
||||||||
20 | } else { |
||||||||
21 | static::set($this->value, $offset, $value); |
||||||||
22 | } |
||||||||
23 | } |
||||||||
24 | |||||||||
25 | public function offsetExists($offset) |
||||||||
26 | { |
||||||||
27 | return static::exist($offset); |
||||||||
0 ignored issues
–
show
|
|||||||||
28 | } |
||||||||
29 | |||||||||
30 | public function offsetUnset($offset) |
||||||||
31 | { |
||||||||
32 | static::unset($this->value, $offset); |
||||||||
0 ignored issues
–
show
The call to
nebula\common\ArrayDotAccess::unset() has too few arguments starting with value .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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...
|
|||||||||
33 | } |
||||||||
34 | |||||||||
35 | public function offsetGet($offset) |
||||||||
36 | { |
||||||||
37 | return static::get($this->value, $offset); |
||||||||
38 | } |
||||||||
39 | |||||||||
40 | /** |
||||||||
41 | * 获取数组元素 |
||||||||
42 | * |
||||||||
43 | * @param array $array |
||||||||
44 | * @param string $name 查询列 |
||||||||
45 | * @param mixed $def 查询的默认值 |
||||||||
46 | * @return mixed 查询的值 |
||||||||
47 | */ |
||||||||
48 | public static function get(array $array, string $name, $def = null) |
||||||||
49 | { |
||||||||
50 | $path = explode('.', $name); |
||||||||
51 | while ($key = array_shift($path)) { |
||||||||
52 | if (is_array($array) && array_key_exists($key, $array)) { |
||||||||
53 | $array = $array[$key]; |
||||||||
54 | } else { |
||||||||
55 | return $def; |
||||||||
56 | } |
||||||||
57 | } |
||||||||
58 | return $array; |
||||||||
59 | } |
||||||||
60 | |||||||||
61 | /** |
||||||||
62 | * 检查元素是否存在 |
||||||||
63 | * |
||||||||
64 | * @param array $array |
||||||||
65 | * @param string $name |
||||||||
66 | * @return boolean |
||||||||
67 | */ |
||||||||
68 | public static function exist(array $array, string $name) |
||||||||
69 | { |
||||||||
70 | $path = explode('.', $name); |
||||||||
71 | while ($key = array_shift($path)) { |
||||||||
72 | if (array_key_exists($key, $array)) { |
||||||||
73 | $array = $array[$key]; |
||||||||
74 | } else { |
||||||||
75 | return false; |
||||||||
76 | } |
||||||||
77 | } |
||||||||
78 | return true; |
||||||||
79 | } |
||||||||
80 | |||||||||
81 | /** |
||||||||
82 | * 设置数组的值 |
||||||||
83 | * |
||||||||
84 | * @param array $array |
||||||||
85 | * @param string $name |
||||||||
86 | * @param mixed $value |
||||||||
87 | * @param mixed $def |
||||||||
88 | * @return array 设置后的数组 |
||||||||
89 | */ |
||||||||
90 | public static function set(array &$array, string $name, $value, $def=null):array |
||||||||
91 | { |
||||||||
92 | $path = explode('.', $name); |
||||||||
93 | $root = &$array; |
||||||||
94 | while (count($path) > 1) { |
||||||||
95 | $key = array_shift($path); |
||||||||
96 | if (is_array($array)) { |
||||||||
97 | if (!array_key_exists($key, $array)) { |
||||||||
98 | $array[$key] = []; |
||||||||
99 | } |
||||||||
100 | } else { |
||||||||
101 | $array=[]; |
||||||||
102 | } |
||||||||
103 | $array = &$array[$key]; |
||||||||
104 | } |
||||||||
105 | $key = array_shift($path); |
||||||||
106 | if (is_array($array) && array_key_exists($key, $array) && is_array($array[$key]) && is_array($value)) { |
||||||||
107 | $array[$key] = array_merge($array[$key], is_array($def) ? $def : [], $value); |
||||||||
108 | } else { |
||||||||
109 | $array[$key] = is_null($value) ? $def : $value; |
||||||||
110 | } |
||||||||
111 | return $root; |
||||||||
112 | } |
||||||||
113 | |||||||||
114 | public static function unset(array &$array, string $name, $value, $def=null):array |
||||||||
0 ignored issues
–
show
The parameter
$def is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.
Loading history...
The parameter
$name is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.
Loading history...
The parameter
$value is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.
Loading history...
|
|||||||||
115 | { |
||||||||
116 | $path = explode('.', $offset); |
||||||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||||||
117 | while (count($path) > 1) { |
||||||||
118 | $key = array_shift($path); |
||||||||
119 | if (is_array($array)) { |
||||||||
120 | if (!array_key_exists($key, $array)) { |
||||||||
121 | $array[$key] = []; |
||||||||
122 | } |
||||||||
123 | } else { |
||||||||
124 | $array=[]; |
||||||||
125 | } |
||||||||
126 | $array = &$array[$key]; |
||||||||
127 | } |
||||||||
128 | $key = array_shift($path); |
||||||||
129 | unset($array[$key]); |
||||||||
0 ignored issues
–
show
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...
|
|||||||||
130 | } |
||||||||
131 | |||||||||
132 | /** |
||||||||
133 | * 转换成正常数组 |
||||||||
134 | * |
||||||||
135 | * @return array |
||||||||
136 | */ |
||||||||
137 | public function toArray():array { |
||||||||
138 | return $this->value; |
||||||||
139 | } |
||||||||
140 | } |
||||||||
141 |
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.