QueryCounter   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 16%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 58
ccs 4
cts 25
cp 0.16
rs 10
c 1
b 0
f 0
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getMaxQueryAnnotation() 0 23 7
A getMaxQueryCount() 0 9 2
A __construct() 0 4 1
A checkQueryCount() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Liip/FunctionalTestBundle
7
 *
8
 * (c) Lukas Kahwe Smith <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
namespace Liip\FunctionalTestBundle;
15
16
use Doctrine\Common\Annotations\Reader;
17
use Liip\FunctionalTestBundle\Annotations\QueryCount;
18
use Liip\FunctionalTestBundle\Exception\AllowedQueriesExceededException;
19
20
final class QueryCounter
21
{
22
    private ?int $defaultMaxCount;
23
    private ?Reader $annotationReader;
24
25
    public function __construct(?int $defaultMaxCount, ?Reader $annotationReader)
26
    {
27
        $this->defaultMaxCount = $defaultMaxCount;
28 5
        $this->annotationReader = $annotationReader;
29
    }
30 5
31 5
    public function checkQueryCount(int $actualQueryCount): void
32 5
    {
33
        $maxQueryCount = $this->getMaxQueryCount();
34
35
        if (null === $maxQueryCount) {
36
            return;
37
        }
38
39
        if ($actualQueryCount > $maxQueryCount) {
40
            throw new AllowedQueriesExceededException("Allowed amount of queries ($maxQueryCount) exceeded (actual: $actualQueryCount).");
41
        }
42
    }
43
44
    private function getMaxQueryCount(): ?int
45
    {
46
        $maxQueryCount = $this->getMaxQueryAnnotation();
47
48
        if (null !== $maxQueryCount) {
49
            return $maxQueryCount;
50
        }
51
52
        return $this->defaultMaxCount;
53
    }
54
55
    private function getMaxQueryAnnotation(): ?int
56
    {
57
        if (null === $this->annotationReader) {
58
            @trigger_error('The annotationReader is not available', \E_USER_ERROR);
59
        }
60
61
        foreach (debug_backtrace() as $step) {
62
            if ('test' === substr($step['function'], 0, 4)) { //TODO: handle tests with the @test annotation
63
                $annotations = $this->annotationReader->getMethodAnnotations(
0 ignored issues
show
Bug introduced by
The method getMethodAnnotations() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
                /** @scrutinizer ignore-call */ 
64
                $annotations = $this->annotationReader->getMethodAnnotations(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
64
                    new \ReflectionMethod($step['class'], $step['function'])
65
                );
66
67
                foreach ($annotations as $annotationClass) {
68
                    if ($annotationClass instanceof QueryCount && isset($annotationClass->maxQueries)) {
69
                        /* @var $annotations \Liip\FunctionalTestBundle\Annotations\QueryCount */
70
71
                        return $annotationClass->maxQueries;
72
                    }
73
                }
74
            }
75
        }
76
77
        return null;
78
    }
79
}
80