ObjectTrait   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.94%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 2
dl 0
loc 120
ccs 31
cts 33
cp 0.9394
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 4 1
B set() 0 8 5
A add() 0 8 2
A has() 0 4 3
A delete() 0 4 1
A __get() 0 8 3
A __set() 0 4 1
A __isset() 0 4 3
A __unset() 0 8 3
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\yii2\collection;
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 20
    public function get($name)
34
    {
35 20
        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 80
    public function set($name, $value, $where = '')
46
    {
47 80
        if (($name && $this->canSetProperty($name)) || strpos($name, 'on ') === 0 || strpos($name, 'as ') === 0) {
0 ignored issues
show
Bug introduced by
It seems like canSetProperty() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
48 80
            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 80
        } else {
50 42
            $this->setItem($name, $value, $where);
51
        }
52 80
    }
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 20
    public function add($name, $value = null, $where = '')
63
    {
64 20
        if (!$this->has($name)) {
65 15
            $this->set($name, $value, $where);
66 15
        }
67
68 20
        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 49
    public function has($name)
77
    {
78 49
        return ($name && $this->hasProperty($name)) || $this->hasItem($name);
0 ignored issues
show
Bug introduced by
It seems like hasProperty() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
79
    }
80
81
    /**
82
     * Delete an item.
83
     * @param $name
84
     */
85 12
    public function delete($name)
86
    {
87 12
        $this->__unset($name);
88 12
    }
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 28
    public function __get($name)
96
    {
97 28
        if ($name && $this->canGetProperty($name)) {
0 ignored issues
show
Bug introduced by
It seems like canGetProperty() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
98 1
            return parent::__get($name);
99
        } else {
100 28
            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 80
    public function __set($name, $value)
111
    {
112 80
        $this->set($name, $value);
113 80
    }
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 5
    public function __isset($name)
122
    {
123 5
        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 12
    public function __unset($name)
133
    {
134 12
        if ($name && $this->canSetProperty($name)) {
0 ignored issues
show
Bug introduced by
It seems like canSetProperty() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
135
            parent::__unset($name);
136
        } else {
137 12
            $this->unsetItem($name);
138
        }
139 12
    }
140
}
141