|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Shopware\Core\Profiling\Doctrine; |
| 4 | + |
| 5 | +use function PHPStan\Testing\assertType; |
| 6 | + |
| 7 | +class ParameterType {} |
| 8 | + |
| 9 | +/** |
| 10 | + * @phpstan-type SanitizedQueryInfo array{sql: string, executionMS: float, types: array<(int | string), ParameterType|int>} |
| 11 | + */ |
| 12 | +abstract class ConnectionProfiler |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @return array<string, array<int, SanitizedQueryInfo>> |
| 16 | + */ |
| 17 | + abstract public function getQueries(): array; |
| 18 | + |
| 19 | + public function getGroupedQueries(): void |
| 20 | + { |
| 21 | + foreach ($this->getQueries() as $queries) { |
| 22 | + $connectionGroupedQueries = []; |
| 23 | + foreach ($queries as $i => $query) { |
| 24 | + $key = $query['sql']; |
| 25 | + if (!isset($connectionGroupedQueries[$key])) { |
| 26 | + $connectionGroupedQueries[$key] = $query; |
| 27 | + $connectionGroupedQueries[$key]['executionMS'] = 0; |
| 28 | + $connectionGroupedQueries[$key]['count'] = 0; |
| 29 | + $connectionGroupedQueries[$key]['index'] = $i; // "Explain query" relies on query index in 'queries'. |
| 30 | + } |
| 31 | + |
| 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); |
| 33 | + $connectionGroupedQueries[$key]['executionMS'] += $query['executionMS']; |
| 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); |
| 35 | + ++$connectionGroupedQueries[$key]['count']; |
| 36 | + } |
| 37 | + |
| 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); |
| 39 | + usort($connectionGroupedQueries, static fn (array $a, array $b): int => $b['executionMS'] <=> $a['executionMS']); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | +} |
0 commit comments