Completed
Push — master ( 0330ad...fd62a8 )
by Filipe
03:14
created

QueryObject::first()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 30
ccs 24
cts 24
cp 1
rs 8.8571
cc 3
eloc 22
nc 4
nop 0
crap 3
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 6
        );
48 12
    }
49
50
    /**
51
     * Retrieve all records matching this select query
52
     *
53
     * @return \Slick\Database\RecordList
54
     */
55 6
    public function all()
56
    {
57 6
        $cid = $this->getId($this);
58 6
        $collection = $this->repository
59 6
            ->getCollectionsMap()
60 6
            ->get($cid, false);
61
62 6
        if (false === $collection) {
63 4
            $data = $this->adapter->query($this, $this->getParameters());
64 4
            $collection = $this->repository->getEntityMapper()
65 4
                ->createFrom($data);
66 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 64 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...
67 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 64 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...
68 2
        }
69
70 6
        return $collection;
71
    }
72
73
    /**
74
     * Retrieve first record matching this select query
75
     *
76
     * @return EntityInterface|null
77
     */
78 2
    public function first()
79
    {
80 2
        $sql = clone($this);
81 2
        $sql->limit(1);
82 2
        $cid = $this->getId($sql);
83
84 2
        $collection = $this->repository
85 2
            ->getCollectionsMap()
86 2
            ->get($cid, false);
87
88 2
        if (false === $collection) {
89 2
            $this->triggerBeforeSelect(
90 1
                $sql,
91 2
                $this->getRepository()->getEntityDescriptor()
92 1
            );
93 2
            $data = $this->adapter->query($sql, $sql->getParameters());
94 2
            $collection = $this->repository->getEntityMapper()
95 2
                ->createFrom($data);
96 2
            $this->triggerAfterSelect(
97 1
                $data,
98
                $collection
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $this->repository->getEn...er()->createFrom($data) on line 94 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...
99 1
            );
100 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 94 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...
101 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 94 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...
102 1
        }
103
104 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...
105 1
            ? null
106 2
            : $collection[0];
107
    }
108
109
    /**
110
     * Gets the id for this query
111
     *
112
     * @param QueryObjectInterface $query
113
     *
114
     * @return string
115
     */
116 8
    protected function getId(QueryObjectInterface $query)
117
    {
118 8
        $str = $query->getQueryString();
119 8
        $search = array_keys($query->getParameters());
120 8
        $values = array_values($query->getParameters());
121 8
        return str_replace($search, $values, $str);
122
    }
123
124
    /**
125
     * Registers every entity in collection to the repository identity map
126
     *
127
     * @param EntityCollection $collection
128
     *
129
     * @return self
130
     */
131 6
    protected function updateIdentityMap(EntityCollection $collection)
132
    {
133 6
        if ($collection->isEmpty()) {
134 2
            return $this;
135
        }
136
137 4
        foreach ($collection as $entity)
138
        {
139 4
            $this->repository->getIdentityMap()->set($entity);
140 2
        }
141 4
        return $this;
142
    }
143
144
    /**
145
     * Returns the repository that is using this query object
146
     *
147
     * @return RepositoryInterface
148
     */
149 10
    public function getRepository()
150
    {
151 10
        return $this->repository;
152
    }
153
154
    /**
155
     * Gets current entity class name
156
     *
157
     * @return string
158
     */
159 2
    public function getEntityClassName()
160
    {
161 2
        return $this->repository->getEntityDescriptor()->className();
162
    }
163
}