Skip to content

Commit 819e3d3

Browse files
Smaller test
1 parent 20b2c0f commit 819e3d3

1 file changed

Lines changed: 6 additions & 52 deletions

File tree

tests/PHPStan/Analyser/nsrt/shopware-connection-profiler.php

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,21 @@
44

55
use function PHPStan\Testing\assertType;
66

7-
class Data {}
87
class ParameterType {}
98

109
/**
11-
* @phpstan-type Backtrace list<array{function: string, line?: int, file?: string, class?: class-string, type?: '->'|'::', args?: list<mixed>, object?: object}>
12-
* @phpstan-type QueryInfo array{
13-
* sql: string,
14-
* executionMS: float,
15-
* types: array<int|string, int>,
16-
* params: array<mixed>,
17-
* backtrace?: Backtrace
18-
* }
19-
* @phpstan-type SanitizedQueryInfo array{sql: string, executionMS: float, types: array<(int | string), ParameterType|int>, params: Data, runnable: bool, explainable: bool, backtrace?: Backtrace}
20-
* @phpstan-type SanitizedQueryInfoGroup array{sql: string, executionMS: float, types: array<(int | string), ParameterType|int>, params: Data, runnable: bool, explainable: bool, backtrace?: Backtrace, count: int, index: int, executionPercent?: float}
10+
* @phpstan-type SanitizedQueryInfo array{sql: string, executionMS: float, types: array<(int | string), ParameterType|int>}
2111
*/
2212
abstract class ConnectionProfiler
2313
{
24-
/**
25-
* @var ?array<string, array<int, SanitizedQueryInfoGroup>>
26-
*/
27-
public ?array $groupedQueries = null;
28-
2914
/**
3015
* @return array<string, array<int, SanitizedQueryInfo>>
3116
*/
3217
abstract public function getQueries(): array;
3318

34-
/**
35-
* @return array<string, array<int, SanitizedQueryInfoGroup>>
36-
*/
37-
public function getGroupedQueries(): array
19+
public function getGroupedQueries(): void
3820
{
39-
if ($this->groupedQueries !== null) {
40-
return $this->groupedQueries;
41-
}
42-
43-
$this->groupedQueries = [];
44-
$totalExecutionMS = 0;
45-
foreach ($this->getQueries() as $connection => $queries) {
21+
foreach ($this->getQueries() as $queries) {
4622
$connectionGroupedQueries = [];
4723
foreach ($queries as $i => $query) {
4824
$key = $query['sql'];
@@ -53,37 +29,15 @@ public function getGroupedQueries(): array
5329
$connectionGroupedQueries[$key]['index'] = $i; // "Explain query" relies on query index in 'queries'.
5430
}
5531

56-
assertType("array<string, array{sql: string, executionMS: 0, types: array<int|string, int|Shopware\Core\Profiling\Doctrine\ParameterType>, params: Shopware\Core\Profiling\Doctrine\Data, runnable: bool, explainable: bool, backtrace?: list<array{function: string, line?: int, file?: string, class?: class-string, type?: '->'|'::', args?: list<mixed>, object?: object}>, count: 0, index: int}|array{sql: string, executionMS: float, types: array<int|string, int|Shopware\Core\Profiling\Doctrine\ParameterType>, params: Shopware\Core\Profiling\Doctrine\Data, runnable: bool, explainable: bool, backtrace?: list<array{function: string, line?: int, file?: string, class?: class-string, type?: '->'|'::', args?: list<mixed>, object?: object}>, count: int<1, max>, index: int}>", $connectionGroupedQueries);
32+
assertType("array<string, array{sql: string, executionMS: 0, types: array<int|string, int|Shopware\Core\Profiling\Doctrine\ParameterType>, count: 0, index: int}|array{sql: string, executionMS: float, types: array<int|string, int|Shopware\Core\Profiling\Doctrine\ParameterType>, count: int<1, max>, index: int}>", $connectionGroupedQueries);
5733
$connectionGroupedQueries[$key]['executionMS'] += $query['executionMS'];
58-
assertType("non-empty-array<string, array{sql: string, executionMS: float, types: array<int|string, int|Shopware\Core\Profiling\Doctrine\ParameterType>, params: Shopware\Core\Profiling\Doctrine\Data, runnable: bool, explainable: bool, backtrace?: list<array{function: string, line?: int, file?: string, class?: class-string, type?: '->'|'::', args?: list<mixed>, object?: object}>, count: int<0, max>, index: int}>", $connectionGroupedQueries);
34+
assertType("non-empty-array<string, array{sql: string, executionMS: float, types: array<int|string, int|Shopware\Core\Profiling\Doctrine\ParameterType>, count: int<0, max>, index: int}>", $connectionGroupedQueries);
5935
++$connectionGroupedQueries[$key]['count'];
60-
$totalExecutionMS += $query['executionMS'];
6136
}
6237

63-
assertType("array<string, array{sql: string, executionMS: float, types: array<int|string, int|Shopware\Core\Profiling\Doctrine\ParameterType>, params: Shopware\Core\Profiling\Doctrine\Data, runnable: bool, explainable: bool, backtrace?: list<array{function: string, line?: int, file?: string, class?: class-string, type?: '->'|'::', args?: list<mixed>, object?: object}>, count: int<1, max>, index: int}>", $connectionGroupedQueries);
38+
assertType("array<string, array{sql: string, executionMS: float, types: array<int|string, int|Shopware\Core\Profiling\Doctrine\ParameterType>, count: int<1, max>, index: int}>", $connectionGroupedQueries);
6439
usort($connectionGroupedQueries, static fn (array $a, array $b): int => $b['executionMS'] <=> $a['executionMS']);
65-
$this->groupedQueries[$connection] = $connectionGroupedQueries;
66-
}
67-
68-
foreach ($this->groupedQueries as &$queries) {
69-
foreach ($queries as &$query) {
70-
$query['executionPercent'] = $this->executionTimePercentage($query['executionMS'], $totalExecutionMS);
71-
}
72-
unset($query);
7340
}
74-
unset($queries);
75-
76-
assertType("list<array{sql: string, executionMS: float, types: array<int|string, int|Shopware\Core\Profiling\Doctrine\ParameterType>, params: Shopware\Core\Profiling\Doctrine\Data, runnable: bool, explainable: bool, backtrace?: list<array{function: string, line?: int, file?: string, class?: class-string, type?: '->'|'::', args?: list<mixed>, object?: object}>, count: int<1, max>, index: int}>", $connectionGroupedQueries);
77-
78-
return $this->groupedQueries;
7941
}
8042

81-
private function executionTimePercentage(float $executionTimeMS, float $totalExecutionTimeMS): float
82-
{
83-
if (!$totalExecutionTimeMS) {
84-
return 0;
85-
}
86-
87-
return $executionTimeMS / $totalExecutionTimeMS * 100;
88-
}
8943
}

0 commit comments

Comments
 (0)