Completed
Pull Request — master (#93)
by Krzysztof
05:28 queued 02:51
created

AbstractCollection::checkItems()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
3
namespace KGzocha\Searcher;
4
5
use Traversable;
6
7
/**
8
 * @author Krzysztof Gzocha <[email protected]>
9
 */
10
abstract class AbstractCollection implements \Countable, \IteratorAggregate
11
{
12
    /**
13
     * @var array
14
     */
15
    private $items;
16
17
    /**
18
     * @param \Traversable|array $items
19
     */
20 48
    public function __construct($items = [])
21
    {
22 48
        $this->items = [];
23 48
        $this->isTraversable($items);
24 33
        $this->checkItems($items);
25
26 23
        $this->items = $items;
1 ignored issue
show
Documentation Bug introduced by
It seems like $items can also be of type object<Traversable>. However, the property $items is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
27 23
    }
28
29
    /**
30
     * @param object $item
31
     *
32
     * @return bool
33
     */
34
    abstract protected function isItemValid($item);
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 1
    public function getIterator()
40
    {
41 1
        return new \ArrayIterator($this->items);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 3
    public function count()
48
    {
49 3
        return count($this->items);
50
    }
51
52
    /**
53
     * @param mixed $item
54
     *
55
     * @return $this
56
     */
57 5
    protected function addItem($item)
58
    {
59 5
        $this->items[] = $item;
60
61 5
        return $this;
62
    }
63
64
    /**
65
     * @param string $name
66
     * @param mixed  $item
67
     *
68
     * @return $this
69
     */
70 3
    protected function addNamedItem($name, $item)
71
    {
72 3
        $this->items[$name] = $item;
73
74 3
        return $this;
75
    }
76
77
    /**
78
     * @return array
79
     */
80 8
    protected function getItems()
81
    {
82 8
        return $this->items;
83
    }
84
85
    /**
86
     * @param string$name
87
     *
88
     * @return mixed|null
89
     */
90 2
    protected function getNamedItem($name)
91
    {
92 2
        if (array_key_exists($name, $this->items)) {
93 1
            return $this->items[$name];
94
        }
95
96 1
        return;
97
    }
98
99
    /**
100
     * @param mixed $items
101
     *
102
     * @throws \InvalidArgumentException
103
     */
104 48
    private function isTraversable($items)
105
    {
106 48
        if (!is_array($items) && !$items instanceof \Traversable) {
107 15
            throw new \InvalidArgumentException(sprintf(
108 15
                'Argument passed to collection %s needs to be an array or traversable object',
109
                get_class($this)
110
            ));
111
        }
112 33
    }
113
114
    /**
115
     * @param array|\Traversable $items
116
     *
117
     * @throws \InvalidArgumentException
118
     */
119 33
    private function checkItems($items)
120
    {
121 33
        foreach ($items as $item) {
122 17
            if ($this->isItemValid($item)) {
123 7
                continue;
124
            }
125
126 10
            throw new \InvalidArgumentException(sprintf(
127 10
                'At least one item in collection "%s" is invalid.',
128
                get_class($this)
129
            ));
130
        }
131 23
    }
132
}
133