Completed
Push — master ( b416d7...51a9f1 )
by Filipe
02:48
created

QueryObject::updateCollection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 6
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\Event\EntityAdded;
16
use Slick\Orm\Event\EntityRemoved;
17
use Slick\Orm\RepositoryInterface;
18
19
/**
20
 * QueryObject
21
 *
22
 * @package Slick\Orm\Repository
23
 * @author  Filipe Silva <[email protected]>
24
 */
25
class QueryObject extends Select implements QueryObjectInterface
26
{
27
28
    /**
29
     * @var RepositoryInterface
30
     */
31
    protected $repository;
32
33
    /**
34
     * For triggering events
35
     */
36
    use SelectEventTriggers;
37
38
    /**
39
     * QueryObject has a repository as a dependency.
40
     *
41
     * @param RepositoryInterface $repository
42
     */
43 12
    public function __construct(RepositoryInterface $repository)
44
    {
45 12
        $this->repository = $repository;
46 12
        $this->adapter = $repository->getAdapter();
47 12
        parent::__construct(
48 12
            $repository->getEntityDescriptor()->getTableName(),
49 12
            $repository->getEntityDescriptor()->getTableName().'.*'
50 6
        );
51 12
    }
52
53
    /**
54
     * Retrieve all records matching this select query
55
     *
56
     * @return \Slick\Database\RecordList
57
     */
58 6
    public function all()
59
    {
60 6
        $cid = $this->getId($this);
61 6
        $collection = $this->repository
62 6
            ->getCollectionsMap()
63 6
            ->get($cid, false);
64
65 6
        if (false === $collection) {
66 4
            $this->triggerBeforeSelect(
67 2
                $this,
68 4
                $this->getRepository()->getEntityDescriptor()
69 2
            );
70 4
            $data = $this->adapter->query($this, $this->getParameters());
71 4
            $collection = $this->repository->getEntityMapper()
72 4
                ->createFrom($data);
73 4
            $this->triggerAfterSelect(
74 2
                $data,
75
                $collection
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $this->repository->getEn...er()->createFrom($data) on line 71 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...
76 2
            );
77 4
            $collection->setId($cid);
78 4
            $collection->getEmitter()
0 ignored issues
show
Bug introduced by
The method getEmitter 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...
79 4
                ->addListener(
80 4
                    EntityAdded::ACTION_ADD,
81 4
                    [$this, 'updateCollection']
82 2
                );
83 4
            $collection->getEmitter()
84 4
                ->addListener(
85 4
                    EntityRemoved::ACTION_REMOVE,
86 4
                    [$this, 'updateCollection']
87 2
                );
88 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 71 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\...ityCollectionInterface>, 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...
89 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 71 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...
90 2
        }
91
92 6
        return $collection;
93
    }
94
95
    /**
96
     * Handles collection add event and updates the cache
97
     * 
98
     * @param EntityAdded $event
99
     */
100
    public function updateCollection(EntityAdded $event)
101
    {
102
        $collection = $event->getCollection();
103
        if ($collection->getId()) {
104
            $this->repository->getCollectionsMap()
105
                ->set($collection->getId(), $collection);
106
        }
107
    }
108
109
    /**
110
     * Retrieve first record matching this select query
111
     *
112
     * @return EntityInterface|null
113
     */
114 2
    public function first()
115
    {
116 2
        $sql = clone($this);
117 2
        $sql->limit(1);
118 2
        $cid = $this->getId($sql);
119
120 2
        $collection = $this->repository
121 2
            ->getCollectionsMap()
122 2
            ->get($cid, false);
123
124 2
        if (false === $collection) {
125 2
            $this->triggerBeforeSelect(
126 1
                $sql,
127 2
                $this->getRepository()->getEntityDescriptor()
128 1
            );
129 2
            $data = $this->adapter->query($sql, $sql->getParameters());
130 2
            $collection = $this->repository->getEntityMapper()
131 2
                ->createFrom($data);
132 2
            $this->triggerAfterSelect(
133 1
                $data,
134
                $collection
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $this->repository->getEn...er()->createFrom($data) on line 130 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...
135 1
            );
136 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 130 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\...ityCollectionInterface>, 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...
137 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 130 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...
138 1
        }
139
140 2
        return $collection->isEmpty() ? null : $collection[0];
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...
141
    }
142
143
    /**
144
     * Gets the id for this query
145
     *
146
     * @param QueryObjectInterface $query
147
     *
148
     * @return string
149
     */
150 8
    protected function getId(QueryObjectInterface $query)
151
    {
152 8
        $str = $query->getQueryString();
153 8
        $search = array_keys($query->getParameters());
154 8
        $values = array_values($query->getParameters());
155 8
        return str_replace($search, $values, $str);
156
    }
157
158
    /**
159
     * Registers every entity in collection to the repository identity map
160
     *
161
     * @param EntityCollection $collection
162
     *
163
     * @return self
164
     */
165 6
    protected function updateIdentityMap(EntityCollection $collection)
166
    {
167 6
        if ($collection->isEmpty()) {
168 2
            return $this;
169
        }
170
171 4
        foreach ($collection as $entity)
172
        {
173 4
            $this->repository->getIdentityMap()->set($entity);
174 2
        }
175 4
        return $this;
176
    }
177
178
    /**
179
     * Returns the repository that is using this query object
180
     *
181
     * @return RepositoryInterface
182
     */
183 10
    public function getRepository()
184
    {
185 10
        return $this->repository;
186
    }
187
188
    /**
189
     * Gets current entity class name
190
     *
191
     * @return string
192
     */
193 6
    public function getEntityClassName()
194
    {
195 6
        return $this->repository->getEntityDescriptor()->className();
196
    }
197
}