Completed
Pull Request — master (#236)
by Alexis
33:58 queued 16:53
created

QueryCounter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96.77%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 66
ccs 30
cts 31
cp 0.9677
rs 10
c 3
b 2
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A checkQueryCount() 0 14 3
A getMaxQueryCount() 0 10 2
B getMaxQueryAnnotation() 0 20 6
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
    /**
18
     * "query.max_query_count" is an array, it is only accessible
19
     * through "query" node and getting the "max_query_count" array
20
     * key with PHP.
21
     */
22 13
    public function __construct($query, Reader $annotationReader)
23
    {
24 13
        $this->defaultMaxCount = $query['max_query_count'];
25 13
        $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...
26 13
    }
27
28 7
    public function checkQueryCount($actualQueryCount)
29
    {
30 7
        $maxQueryCount = $this->getMaxQueryCount();
31
32 7
        if (null === $maxQueryCount) {
33
            return;
34
        }
35
36 7
        if ($actualQueryCount > $maxQueryCount) {
37 2
            throw new AllowedQueriesExceededException(
38 2
                "Allowed amount of queries ($maxQueryCount) exceeded (actual: $actualQueryCount)."
39 2
            );
40
        }
41 5
    }
42
43 7
    private function getMaxQueryCount()
44
    {
45 7
        $maxQueryCount = $this->getMaxQueryAnnotation();
46
47 7
        if (false !== $maxQueryCount) {
48 1
            return $maxQueryCount;
49
        }
50
51 6
        return $this->defaultMaxCount;
52
    }
53
54 7
    private function getMaxQueryAnnotation()
55
    {
56 7
        foreach (debug_backtrace() as $step) {
57 7
            if ('test' === substr($step['function'], 0, 4)) { //TODO: handle tests with the @test annotation
58 7
                $annotations = $this->annotationReader->getMethodAnnotations(
59 7
                    new \ReflectionMethod($step['class'], $step['function'])
60 7
                );
61
62 7
                foreach ($annotations as $annotationClass) {
63 1
                    if ($annotationClass instanceof QueryCount && isset($annotationClass->maxQueries)) {
64
                        /* @var $annotations \Liip\FunctionalTestBundle\Annotations\QueryCount */
65
66 1
                        return $annotationClass->maxQueries;
67
                    }
68 6
                }
69 6
            }
70 7
        }
71
72 6
        return false;
73
    }
74
}
75