Passed
Push — sudav3 ( ac6dc6...d2f44f )
by 世昌
02:12
created

ArrayDataTrait::offsetSet()   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 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace suda\orm\struct;
3
4
use InvalidArgumentException;
5
use suda\orm\struct\MagicArrayAccessTrait;
0 ignored issues
show
Bug introduced by
The type suda\orm\struct\MagicArrayAccessTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
trait ArrayDataTrait  
8
{
9
    use MagicArrayAccessTrait;
10
    /**
11
     * 表数据
12
     *
13
     * @var array
14
     */
15
    protected $data = [];
16
17
    /**
18
     * 设置值
19
     *
20
     * @param string $name
21
     * @param mixed $value
22
     */
23
    public function __set(string $name, $value)
24
    {
25
        $this->assertFieldName($name);
26
        $this->data[$name] = $value;
27
    }
28
29
    
30
    
31
    /**
32
     * 获取参数值
33
     *
34
     * @param string $name
35
     * @return mixed
36
     */
37
    public function __get(string $name)
38
    {
39
        $this->assertFieldName($name);
40
        return $this->data[$name] ?? null;
41
    }
42
43
    /**
44
     * 判断是否设置
45
     *
46
     * @param string $name
47
     * @return boolean
48
     */
49
    public function __isset(string $name)
50
    {
51
        $this->assertFieldName($name);
52
        return array_key_exists($name, $this->data);
53
    }
54
55
    /**
56
     * 取消设置值
57
     *
58
     * @param string $name
59
     */
60
    public function __unset(string $name)
61
    {
62
        $this->assertFieldName($name);
63
        unset($this->data[$name]);
64
    }
65
66
    /**
67
     * 断言字段
68
     *
69
     * @param string $name
70
     * @return void
71
     */
72
    protected function assertFieldName(string $name)
73
    {
74
        if ($this->checkFieldExist($name) === false) {
75
            throw new InvalidArgumentException(sprintf('[%s] has no attribute %s', static::class, $name), 0);
76
        }
77
    }
78
79
    /**
80
     * 检查字段是否存在
81
     *
82
     * @param string $name
83
     * @return boolean
84
     */
85
    public function checkFieldExist(string $name)
86
    {
87
        return strlen($name) > 0;
88
    }
89
}
90