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 | /** |
||
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
|
|||
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 |
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.