Test Setup Failed
Push — master ( 45cac2...1259a9 )
by Lukas Kahwe
01:25 queued 11s
created

QueryCounter.php (1 issue)

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
namespace Liip\FunctionalTestBundle;
4
5
use Doctrine\Common\Annotations\Reader;
6
use Liip\FunctionalTestBundle\Annotations\QueryCount;
7
use Liip\FunctionalTestBundle\Exception\AllowedQueriesExceededException;
8
9
class QueryCounter
10
{
11
    /** @var int */
12
    private $defaultMaxCount;
13
14
    /** @var \Doctrine\Common\Annotations\AnnotationReader */
15
    private $annotationReader;
16
17 16
    public function __construct($defaultMaxCount, Reader $annotationReader)
18
    {
19 16
        $this->defaultMaxCount = $defaultMaxCount;
20 16
        $this->annotationReader = $annotationReader;
0 ignored issues
show
Documentation Bug introduced by
$annotationReader is of type object<Doctrine\Common\Annotations\Reader>, but the property $annotationReader was declared to be of type object<Doctrine\Common\A...tions\AnnotationReader>. 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...
21 16
    }
22
23 5
    public function checkQueryCount($actualQueryCount)
24
    {
25 5
        $maxQueryCount = $this->getMaxQueryCount();
26
27 5
        if (null === $maxQueryCount) {
28
            return;
29
        }
30
31 5
        if ($actualQueryCount > $maxQueryCount) {
32 2
            throw new AllowedQueriesExceededException(
33 2
                "Allowed amount of queries ($maxQueryCount) exceeded (actual: $actualQueryCount)."
34 2
            );
35
        }
36 3
    }
37
38 5
    private function getMaxQueryCount()
39
    {
40 5
        $maxQueryCount = $this->getMaxQueryAnnotation();
41
42 5
        if (false !== $maxQueryCount) {
43 1
            return $maxQueryCount;
44
        }
45
46 4
        return $this->defaultMaxCount;
47
    }
48
49 5
    private function getMaxQueryAnnotation()
50
    {
51 5
        foreach (debug_backtrace() as $step) {
52 5
            if ('test' === substr($step['function'], 0, 4)) { //TODO: handle tests with the @test annotation
53 5
                $annotations = $this->annotationReader->getMethodAnnotations(
54 5
                    new \ReflectionMethod($step['class'], $step['function'])
55 5
                );
56
57 5
                foreach ($annotations as $annotationClass) {
58 1
                    if ($annotationClass instanceof QueryCount && isset($annotationClass->maxQueries)) {
59
                        /* @var $annotations \Liip\FunctionalTestBundle\Annotations\QueryCount */
60
61 1
                        return $annotationClass->maxQueries;
62
                    }
63 4
                }
64 4
            }
65 5
        }
66
67 4
        return false;
68
    }
69
}
70