Completed
Push — master ( 7bcfa7...7f7fda )
by Andrii
01:58
created

ObjectTrait::__set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * Collection library for Yii2.
4
 *
5
 * @link      https://github.com/hiqdev/yii2-collection
6
 * @package   yii2-collection
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\collection\yii;
12
13
use hiqdev\php\collection\BaseTrait;
14
use yii\base\ArrayableTrait;
15
16
/**
17
 * ObjectTrait.
18
 * Intended to be used for yii\base\BaseObject descendants.
19
 * Uses canSet/GetPropperty and magic functions to provide compatible getter/setter mechanisms.
20
 */
21
trait ObjectTrait
22
{
23
    use ArrayableTrait;
24
    use BaseTrait {
25
        BaseTrait::fields insteadof ArrayableTrait;
26
    }
27
28
    /**
29
     * Returns property of item by name.
30
     * @param mixed $name
31
     * @return mixed
32
     */
33
    public function get($name)
34
    {
35
        return $this->__get($name);
36
    }
37
38
    /**
39
     * Sets an item. Silently resets if already exists.
40
     * @param int|string   $name
41
     * @param mixed        $value the element value
42
     * @param string|array $where where to put, see [[setItem()]]
43
     * @see setItem()
44
     */
45
    public function set($name, $value, $where = '')
46
    {
47
        if (($name && $this->canSetProperty($name)) || strpos($name, 'on ') === 0 || strpos($name, 'as ') === 0) {
48
            parent::__set($name, $value);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (__set() instead of set()). Are you sure this is correct? If so, you might want to change this to $this->__set().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
49
        } else {
50
            $this->setItem($name, $value, $where);
51
        }
52
    }
53
54
    /**
55
     * Adds an item. Does not touch if already exists.
56
     * @param int|string   $name  item name
57
     * @param array        $value item value
58
     * @param string|array $where where to put, see [[setItem()]]
59
     * @return $this for chaining
60
     * @see setItem()
61
     */
62
    public function add($name, $value = null, $where = '')
63
    {
64
        if (!$this->has($name)) {
65
            $this->set($name, $value, $where);
66
        }
67
68
        return $this;
69
    }
70
71
    /**
72
     * Check collection has the item.
73
     * @param string $name item name
74
     * @return bool whether item exist
75
     */
76
    public function has($name)
77
    {
78
        return ($name && $this->hasProperty($name)) || $this->hasItem($name);
79
    }
80
81
    /**
82
     * Delete an item.
83
     * @param $name
84
     */
85
    public function delete($name)
86
    {
87
        $this->__unset($name);
88
    }
89
90
    /**
91
     * This method is overridden to support accessing items like properties.
92
     * @param string $name item or property name
93
     * @return mixed item of found or the named property value
94
     */
95
    public function __get($name)
96
    {
97
        if ($name && $this->canGetProperty($name)) {
98
            return parent::__get($name);
99
        } else {
100
            return $this->getItem($name);
101
        }
102
    }
103
104
    /**
105
     * This method is overridden to support accessing items like properties.
106
     * @param string $name  item or property name
107
     * @param string $value value to be set
108
     * @return mixed item of found or the named property value
109
     */
110
    public function __set($name, $value)
111
    {
112
        $this->set($name, $value);
113
    }
114
115
    /**
116
     * Checks if a property value is null.
117
     * This method overrides the parent implementation by checking if the named item is loaded.
118
     * @param string $name the property name or the event name
119
     * @return bool whether the property value is null
120
     */
121
    public function __isset($name)
122
    {
123
        return ($name && parent::__isset($name)) || $this->issetItem($name);
124
    }
125
126
    /**
127
     * Checks if a property value is null.
128
     * This method overrides the parent implementation by checking if the named item is loaded.
129
     * @param string $name the property name or the event name
130
     * @return bool whether the property value is null
131
     */
132
    public function __unset($name)
133
    {
134
        if ($name && $this->canSetProperty($name)) {
135
            parent::__unset($name);
136
        } else {
137
            $this->unsetItem($name);
138
        }
139
    }
140
}
141