Completed
Push — master ( deaa6d...35cd82 )
by Krzysztof
12s
created

ResultCollection::canUseResults()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace KGzocha\Searcher\Result;
4
5
/**
6
 * @author Krzysztof Gzocha <[email protected]>
7
 * @package KGzocha\Searcher\Result
8
 */
9
class ResultCollection implements ResultCollectionInterface
10
{
11
    /**
12
     * @var array
13
     */
14
    private $results;
15
16
    /**
17
     * @param \Traversable|array $results
18
     */
19 13
    public function __construct($results = [])
20
    {
21 13
        if ($this->canUseResults($results)) {
22 13
            $this->results = $results;
0 ignored issues
show
Documentation Bug introduced by
It seems like $results can also be of type object<Traversable>. However, the property $results 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...
23
24 13
            return;
25
        }
26
27
        $this->results = [];
28
    }
29
30
    /**
31
     * @inheritdoc
32
     */
33
    public function getResults()
34
    {
35
        return $this->results;
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41 7
    public function count()
42
    {
43 7
        return count($this->results);
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49 5
    public function jsonSerialize()
50
    {
51 5
        return $this->results;
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57 1
    public function getIterator()
58
    {
59 1
        return new \ArrayIterator($this->results);
60
    }
61
62
    /**
63
     * @param \Traversable|array $results
64
     * @return bool
65
     */
66 13
    private function canUseResults($results = [])
67
    {
68 13
        return is_array($results) || $results instanceof \Traversable;
69
    }
70
}
71