Failed Conditions
Push — master ( 6744b4...2b8acb )
by Marco
60:45 queued 60:36
created

lib/Doctrine/ORM/LazyCriteriaCollection.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM;
21
22
use Doctrine\Common\Collections\AbstractLazyCollection;
23
use Doctrine\Common\Collections\ArrayCollection;
24
use Doctrine\Common\Collections\Criteria;
25
use Doctrine\Common\Collections\Selectable;
26
use Doctrine\ORM\Persisters\Entity\BasicEntityPersister;
27
use Doctrine\ORM\Persisters\Entity\EntityPersister;
28
29
/**
30
 * A lazy collection that allow a fast count when using criteria object
31
 * Once count gets executed once without collection being initialized, result
32
 * is cached and returned on subsequent calls until collection gets loaded,
33
 * then returning the number of loaded results.
34
 *
35
 * @since   2.5
36
 * @author  Guilherme Blanco <[email protected]>
37
 * @author  Michaël Gallego <[email protected]>
38
 */
39
class LazyCriteriaCollection extends AbstractLazyCollection implements Selectable
40
{
41
    /**
42
     * @var BasicEntityPersister
43
     */
44
    protected $entityPersister;
45
46
    /**
47
     * @var Criteria
48
     */
49
    protected $criteria;
50
51
    /**
52
     * @var integer|null
53
     */
54
    private $count;
55
56
    /**
57
     * @param EntityPersister $entityPersister
58
     * @param Criteria        $criteria
59
     */
60 35
    public function __construct(EntityPersister $entityPersister, Criteria $criteria)
61
    {
62 35
        $this->entityPersister = $entityPersister;
0 ignored issues
show
Documentation Bug introduced by
$entityPersister is of type object<Doctrine\ORM\Pers...Entity\EntityPersister>, but the property $entityPersister was declared to be of type object<Doctrine\ORM\Pers...y\BasicEntityPersister>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof 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 given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
63 35
        $this->criteria        = $criteria;
64 35
    }
65
66
    /**
67
     * Do an efficient count on the collection
68
     *
69
     * @return integer
70
     */
71 30
    public function count()
72
    {
73 30
        if ($this->isInitialized()) {
74 2
            return $this->collection->count();
75
        }
76
77
        // Return cached result in case count query was already executed
78 29
        if ($this->count !== null) {
79 2
            return $this->count;
80
        }
81
82 29
        return $this->count = $this->entityPersister->count($this->criteria);
0 ignored issues
show
$this->criteria is of type object<Doctrine\Common\Collections\Criteria>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
    }
84
85
    /**
86
     * check if collection is empty without loading it
87
     *
88
     * @return boolean TRUE if the collection is empty, FALSE otherwise.
89
     */
90 3
    public function isEmpty()
91
    {
92 3
        if ($this->isInitialized()) {
93 1
            return $this->collection->isEmpty();
94
        }
95
96 2
        return !$this->count();
97
    }
98
99
    /**
100
     * Do an optimized search of an element
101
     *
102
     * @param object $element
103
     *
104
     * @return bool
105
     */
106 1
    public function contains($element)
107
    {
108 1
        if ($this->isInitialized()) {
109
            return $this->collection->contains($element);
110
        }
111
112 1
        return $this->entityPersister->exists($element, $this->criteria);
113
    }
114
115
    /**
116
     * {@inheritDoc}
117
     */
118 1
    public function matching(Criteria $criteria)
119
    {
120 1
        $this->initialize();
121
122 1
        return $this->collection->matching($criteria);
123
    }
124
125
    /**
126
     * {@inheritDoc}
127
     */
128 9
    protected function doInitialize()
129
    {
130 9
        $elements         = $this->entityPersister->loadCriteria($this->criteria);
131 7
        $this->collection = new ArrayCollection($elements);
132 7
    }
133
}
134