Completed
Push — master ( 4dd8bf...5dcaed )
by Filipe
07:48
created

QueryObject::all()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 7
Ratio 41.18 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 7
loc 17
ccs 13
cts 13
cp 1
rs 9.4285
cc 2
eloc 12
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
     * QueryObject has a repository as a dependency.
33
     *
34
     * @param RepositoryInterface $repository
35
     */
36 10
    public function __construct(RepositoryInterface $repository)
37
    {
38 10
        $this->repository = $repository;
39 10
        $this->adapter = $repository->getAdapter();
40 10
        parent::__construct(
41 10
            $repository->getEntityDescriptor()->getTableName()
42 10
        );
43 10
    }
44
45
    /**
46
     * Retrieve all records matching this select query
47
     *
48
     * @return \Slick\Database\RecordList
49
     */
50 6
    public function all()
51
    {
52 6
        $cid = $this->getId($this);
53 6
        $collection = $this->repository
54 6
            ->getCollectionsMap()
55 6
            ->get($cid, false);
56
57 6 View Code Duplication
        if (false === $collection) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58 4
            $data = $this->adapter->query($this, $this->getParameters());
59 4
            $collection = $this->repository->getEntityMapper()
60 4
                ->createFrom($data);
61 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 59 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...
62 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 59 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...
63 4
        }
64
65 6
        return $collection;
66
    }
67
68
    /**
69
     * Retrieve first record matching this select query
70
     *
71
     * @return EntityInterface|null
72
     */
73 2
    public function first()
74
    {
75 2
        $sql = clone($this);
76 2
        $sql->limit(1);
77 2
        $cid = $this->getId($sql);
78
79 2
        $collection = $this->repository
80 2
            ->getCollectionsMap()
81 2
            ->get($cid, false);
82
83 2 View Code Duplication
        if (false === $collection) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84 2
            $data = $this->adapter->query($sql, $sql->getParameters());
85 2
            $collection = $this->repository->getEntityMapper()
86 2
                ->createFrom($data);
87 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 85 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...
88 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 85 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...
89 2
        }
90
91 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...
92 2
            ? null
93 2
            : $collection[0];
94
    }
95
96
    /**
97
     * Gets the id for this query
98
     *
99
     * @param QueryObjectInterface $query
100
     *
101
     * @return string
102
     */
103 8
    protected function getId(QueryObjectInterface $query)
104
    {
105 8
        $str = $query->getQueryString();
106 8
        $search = array_keys($query->getParameters());
107 8
        $values = array_values($query->getParameters());
108 8
        return str_replace($search, $values, $str);
109
    }
110
111
    /**
112
     * Registers every entity in collection to the repository identity map
113
     *
114
     * @param EntityCollection $collection
115
     *
116
     * @return self
117
     */
118 6
    protected function updateIdentityMap(EntityCollection $collection)
119
    {
120 6
        if ($collection->isEmpty()) {
121 2
            return $this;
122
        }
123
124 4
        foreach ($collection as $entity)
125
        {
126 4
            $this->repository->getIdentityMap()->set($entity);
127 4
        }
128 4
        return $this;
129
    }
130
131
    /**
132
     * Returns the repository that is using this query object
133
     *
134
     * @return RepositoryInterface
135
     */
136 10
    public function getRepository()
137
    {
138 10
        return $this->repository;
139
    }
140
}