Completed
Push — master ( 20f332...6fd3d5 )
by Lukas Kahwe
07:41
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
    public function __construct($defaultMaxCount, Reader $annotationReader)
18
    {
19
        $this->defaultMaxCount = $defaultMaxCount;
20
        $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
    }
22
23
    public function checkQueryCount($actualQueryCount)
24
    {
25
        $maxQueryCount = $this->getMaxQueryCount();
26
27
        if (null === $maxQueryCount) {
28
            return;
29
        }
30
31
        if ($actualQueryCount > $maxQueryCount) {
32
            throw new AllowedQueriesExceededException(
33
                "Allowed amount of queries ($maxQueryCount) exceeded (actual: $actualQueryCount)."
34
            );
35
        }
36
    }
37
38
    private function getMaxQueryCount()
39
    {
40
        $maxQueryCount = $this->getMaxQueryAnnotation();
41
42
        if (false !== $maxQueryCount) {
43
            return $maxQueryCount;
44
        }
45
46
        return $this->defaultMaxCount;
47
    }
48
49
    private function getMaxQueryAnnotation()
50
    {
51
        foreach (debug_backtrace() as $step) {
52
            if ('test' === substr($step['function'], 0, 4)) { //TODO: handle tests with the @test annotation
53
                $annotations = $this->annotationReader->getMethodAnnotations(
54
                    new \ReflectionMethod($step['class'], $step['function'])
55
                );
56
57
                foreach ($annotations as $annotationClass) {
58
                    if ($annotationClass instanceof QueryCount && isset($annotationClass->maxQueries)) {
59
                        /* @var $annotations \Liip\FunctionalTestBundle\Annotations\QueryCount */
60
61
                        return $annotationClass->maxQueries;
62
                    }
63
                }
64
            }
65
        }
66
67
        return false;
68
    }
69
}
70