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