Completed
Push — master ( 969a45...f408b7 )
by Krzysztof
9s
created

ResultCollection::getResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

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 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace KGzocha\Searcher\Result;
4
5
/**
6
 * Can be used to hold results from searching when developer is not 100% sure
7
 * if searching process will return array of objects or a number, or null, or whatever.
8
 * This class regardless the constructor will always be iteratable, so in worse case scenario
9
 * there will be no results inside, but your controllers will still work.
10
 * It's not recommended to use it on development environment due to eventual problems with debugging.
11
 *
12
 * @author Krzysztof Gzocha <[email protected]>
13
 */
14
class ResultCollection implements ResultCollectionInterface
15
{
16
    /**
17
     * @var array
18
     */
19
    private $results;
20
21
    /**
22
     * @param \Traversable|array $results
23
     */
24 19
    public function __construct($results = [])
25
    {
26 19
        if ($this->canUseResults($results)) {
27 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...
28
29 13
            return;
30
        }
31
32 6
        $this->results = [];
33 6
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 12
    public function getResults()
39
    {
40 12
        return $this->results;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 13
    public function count()
47
    {
48 13
        return count($this->results);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 5
    public function jsonSerialize()
55
    {
56 5
        return $this->results;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 1
    public function getIterator()
63
    {
64 1
        return new \ArrayIterator($this->results);
65
    }
66
67
    /**
68
     * @param \Traversable|array $results
69
     *
70
     * @return bool
71
     */
72 19
    private function canUseResults($results = [])
73
    {
74 19
        return is_array($results)
75 19
            || (is_object($results) && $results instanceof \Traversable);
76
    }
77
}
78