Completed
Push — master ( fd62a8...5cbc9e )
by Filipe
04:02
created

QueryObject::all()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 25
ccs 20
cts 20
cp 1
rs 8.8571
cc 2
eloc 18
nc 2
nop 0
crap 2
1
<?php
2
3
/**
4
 * This file is part of slick/orm package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Orm\Repository\QueryObject;
11
12
use Slick\Database\Sql\Select;
13
use Slick\Orm\Entity\EntityCollection;
14
use Slick\Orm\EntityInterface;
15
use Slick\Orm\RepositoryInterface;
16
17
/**
18
 * QueryObject
19
 *
20
 * @package Slick\Orm\Repository
21
 * @author  Filipe Silva <[email protected]>
22
 */
23
class QueryObject extends Select implements QueryObjectInterface
24
{
25
26
    /**
27
     * @var RepositoryInterface
28
     */
29
    protected $repository;
30
31
    /**
32
     * For triggering events
33
     */
34
    use SelectEventTriggers;
35
36
    /**
37
     * QueryObject has a repository as a dependency.
38
     *
39
     * @param RepositoryInterface $repository
40
     */
41 12
    public function __construct(RepositoryInterface $repository)
42
    {
43 12
        $this->repository = $repository;
44 12
        $this->adapter = $repository->getAdapter();
45 12
        parent::__construct(
46 12
            $repository->getEntityDescriptor()->getTableName(),
47 12
            $repository->getEntityDescriptor()->getTableName().'.*'
48 12
        );
49 12
    }
50
51
    /**
52
     * Retrieve all records matching this select query
53
     *
54
     * @return \Slick\Database\RecordList
55
     */
56 6
    public function all()
57
    {
58 6
        $cid = $this->getId($this);
59 6
        $collection = $this->repository
60 6
            ->getCollectionsMap()
61 6
            ->get($cid, false);
62
63 6
        if (false === $collection) {
64 4
            $this->triggerBeforeSelect(
65 4
                $this,
66 4
                $this->getRepository()->getEntityDescriptor()
67 4
            );
68 4
            $data = $this->adapter->query($this, $this->getParameters());
69 4
            $collection = $this->repository->getEntityMapper()
70 4
                ->createFrom($data);
71 4
            $this->triggerAfterSelect(
72 4
                $data,
73
                $collection
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $this->repository->getEn...er()->createFrom($data) on line 69 can also be of type array<integer,object<Sli...EntityMapperInterface>> or object<Slick\Orm\EntityInterface>; however, Slick\Orm\Repository\Que...s::triggerAfterSelect() does only seem to accept object<Slick\Orm\Entity\EntityCollection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
74 4
            );
75 4
            $this->repository->getCollectionsMap()->set($cid, $collection);
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $this->repository->getEn...er()->createFrom($data) on line 69 can also be of type array<integer,object<Sli...EntityMapperInterface>> or object<Slick\Orm\EntityInterface>; however, Slick\Orm\Entity\CollectionsMapInterface::set() does only seem to accept object<Slick\Orm\Entity\EntityCollection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
76 4
            $this->updateIdentityMap($collection);
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $this->repository->getEn...er()->createFrom($data) on line 69 can also be of type array<integer,object<Sli...EntityMapperInterface>> or object<Slick\Orm\EntityInterface>; however, Slick\Orm\Repository\Que...ct::updateIdentityMap() does only seem to accept object<Slick\Orm\Entity\EntityCollection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
77 4
        }
78
79 6
        return $collection;
80
    }
81
82
    /**
83
     * Retrieve first record matching this select query
84
     *
85
     * @return EntityInterface|null
86
     */
87 2
    public function first()
88
    {
89 2
        $sql = clone($this);
90 2
        $sql->limit(1);
91 2
        $cid = $this->getId($sql);
92
93 2
        $collection = $this->repository
94 2
            ->getCollectionsMap()
95 2
            ->get($cid, false);
96
97 2
        if (false === $collection) {
98 2
            $this->triggerBeforeSelect(
99 2
                $sql,
100 2
                $this->getRepository()->getEntityDescriptor()
101 2
            );
102 2
            $data = $this->adapter->query($sql, $sql->getParameters());
103 2
            $collection = $this->repository->getEntityMapper()
104 2
                ->createFrom($data);
105 2
            $this->triggerAfterSelect(
106 2
                $data,
107
                $collection
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $this->repository->getEn...er()->createFrom($data) on line 103 can also be of type array<integer,object<Sli...EntityMapperInterface>> or object<Slick\Orm\EntityInterface>; however, Slick\Orm\Repository\Que...s::triggerAfterSelect() does only seem to accept object<Slick\Orm\Entity\EntityCollection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
108 2
            );
109 2
            $this->repository->getCollectionsMap()->set($cid, $collection);
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $this->repository->getEn...er()->createFrom($data) on line 103 can also be of type array<integer,object<Sli...EntityMapperInterface>> or object<Slick\Orm\EntityInterface>; however, Slick\Orm\Entity\CollectionsMapInterface::set() does only seem to accept object<Slick\Orm\Entity\EntityCollection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
110 2
            $this->updateIdentityMap($collection);
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $this->repository->getEn...er()->createFrom($data) on line 103 can also be of type array<integer,object<Sli...EntityMapperInterface>> or object<Slick\Orm\EntityInterface>; however, Slick\Orm\Repository\Que...ct::updateIdentityMap() does only seem to accept object<Slick\Orm\Entity\EntityCollection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
111 2
        }
112
113 2
        return $collection->isEmpty()
0 ignored issues
show
Bug introduced by
The method isEmpty does only exist in Slick\Orm\Entity\EntityCollection, but not in Slick\Orm\EntityInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
114 2
            ? null
115 2
            : $collection[0];
116
    }
117
118
    /**
119
     * Gets the id for this query
120
     *
121
     * @param QueryObjectInterface $query
122
     *
123
     * @return string
124
     */
125 8
    protected function getId(QueryObjectInterface $query)
126
    {
127 8
        $str = $query->getQueryString();
128 8
        $search = array_keys($query->getParameters());
129 8
        $values = array_values($query->getParameters());
130 8
        return str_replace($search, $values, $str);
131
    }
132
133
    /**
134
     * Registers every entity in collection to the repository identity map
135
     *
136
     * @param EntityCollection $collection
137
     *
138
     * @return self
139
     */
140 6
    protected function updateIdentityMap(EntityCollection $collection)
141
    {
142 6
        if ($collection->isEmpty()) {
143 2
            return $this;
144
        }
145
146 4
        foreach ($collection as $entity)
147
        {
148 4
            $this->repository->getIdentityMap()->set($entity);
149 4
        }
150 4
        return $this;
151
    }
152
153
    /**
154
     * Returns the repository that is using this query object
155
     *
156
     * @return RepositoryInterface
157
     */
158 10
    public function getRepository()
159
    {
160 10
        return $this->repository;
161
    }
162
163
    /**
164
     * Gets current entity class name
165
     *
166
     * @return string
167
     */
168 6
    public function getEntityClassName()
169
    {
170 6
        return $this->repository->getEntityDescriptor()->className();
171
    }
172
}