Skip to content

Commit 746ac82

Browse files
authored
Merge pull request #2202 from doctrine/3.2.x
2 parents 87072ec + 10b9d81 commit 746ac82

File tree

15 files changed

+174
-50
lines changed

15 files changed

+174
-50
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,15 @@
4545
"require-dev": {
4646
"doctrine/coding-standard": "^14",
4747
"doctrine/orm": "^3.4.4",
48-
"phpstan/phpstan": "2.1.1",
48+
"phpstan/phpstan": "^2.1.13",
4949
"phpstan/phpstan-phpunit": "2.0.3",
5050
"phpstan/phpstan-strict-rules": "^2",
51-
"phpstan/phpstan-symfony": "^2.0",
51+
"phpstan/phpstan-symfony": "^2.0.9",
5252
"phpunit/phpunit": "^12.3.10",
5353
"psr/log": "^3.0",
5454
"symfony/doctrine-messenger": "^6.4 || ^7.0 || ^8.0",
5555
"symfony/expression-language": "^6.4 || ^7.0 || ^8.0",
56+
"symfony/http-kernel": "^6.4 || ^7.0 || ^8.0",
5657
"symfony/messenger": "^6.4 || ^7.0 || ^8.0",
5758
"symfony/property-info": "^6.4 || ^7.0 || ^8.0",
5859
"symfony/security-bundle": "^6.4 || ^7.0 || ^8.0",

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
includes:
22
- vendor/phpstan/phpstan-symfony/extension.neon
33
parameters:
4-
level: 7
4+
level: 8
55
reportUnmatchedIgnoredErrors: true
66
paths:
77
- config

src/Controller/ProfilerController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ public function explainAction(string $token, string $connectionName, int $query)
4040
{
4141
$this->profiler->disable();
4242

43-
$profile = $this->profiler->loadProfile($token);
43+
$profile = $this->profiler->loadProfile($token);
44+
if ($profile === null) {
45+
return new Response('Profile not found.', 404);
46+
}
47+
4448
$collector = $profile->getCollector('db');
4549

4650
assert($collector instanceof DoctrineDataCollector);

src/DataCollector/DoctrineDataCollector.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,18 @@ class DoctrineDataCollector extends BaseCollector
6666

6767
private int|null $managedEntityCount = null;
6868

69-
/**
70-
* @var mixed[][]|null
71-
* @phpstan-var ?GroupedQueriesType
72-
*/
69+
/** @var GroupedQueriesType|null */
7370
private array|null $groupedQueries = null;
7471

7572
public function __construct(
7673
private readonly ManagerRegistry $registry,
7774
private readonly bool $shouldValidateSchema = true,
7875
DebugDataHolder|null $debugDataHolder = null,
7976
) {
77+
if ($debugDataHolder === null) {
78+
$debugDataHolder = new DebugDataHolder();
79+
}
80+
8081
parent::__construct($registry, $debugDataHolder);
8182
}
8283

@@ -307,10 +308,9 @@ public function getGroupedQueries(): array
307308
$this->groupedQueries[$connection] = $connectionGroupedQueries;
308309
}
309310

310-
foreach ($this->groupedQueries as $connection => $queries) {
311-
foreach ($queries as $i => $query) {
312-
$this->groupedQueries[$connection][$i]['executionPercent'] =
313-
$this->executionTimePercentage($query['executionMS'], $totalExecutionMS);
311+
foreach ($this->groupedQueries as &$queries) {
312+
foreach ($queries as &$query) {
313+
$query['executionPercent'] = $this->executionTimePercentage($query['executionMS'], $totalExecutionMS);
314314
}
315315
}
316316

src/DependencyInjection/Compiler/MiddlewaresPass.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ public function process(ContainerBuilder $container): void
6464
);
6565
$middlewareRefs[$id] = [new Reference($childId), ++$i];
6666

67-
if (! is_subclass_of($abstractDef->getClass(), ConnectionNameAwareInterface::class)) {
67+
$class = $abstractDef->getClass();
68+
if ($class === null || ! is_subclass_of($class, ConnectionNameAwareInterface::class)) {
6869
continue;
6970
}
7071

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ private function addDbalSection(ArrayNodeDefinition $node): void
6767
// Key that should not be rewritten to the connection config
6868
$excludedKeys = ['default_connection' => true, 'driver_schemes' => true, 'driver_scheme' => true, 'types' => true, 'type' => true];
6969

70-
/** @phpstan-ignore class.notFound (Phpstan Symfony extension does not know yet how to deal with these) */
7170
$node
7271
->children()
7372
->arrayNode('dbal')
@@ -167,7 +166,6 @@ private function getDbalConnectionsNode(): ArrayNodeDefinition
167166

168167
$this->configureDbalDriverNode($connectionNode);
169168

170-
/** @phpstan-ignore class.notFound (Phpstan Symfony extension does not know yet how to deal with these) */
171169
$connectionNode
172170
->fixXmlConfig('option')
173171
->fixXmlConfig('mapping_type')
@@ -214,7 +212,6 @@ private function getDbalConnectionsNode(): ArrayNodeDefinition
214212
->scalarNode('result_cache')->end()
215213
->end();
216214

217-
/** @phpstan-ignore class.notFound (Phpstan Symfony extension does not know yet how to deal with these) */
218215
$replicaNode = $connectionNode
219216
->children()
220217
->arrayNode('replicas')
@@ -232,7 +229,6 @@ private function getDbalConnectionsNode(): ArrayNodeDefinition
232229
*/
233230
private function configureDbalDriverNode(ArrayNodeDefinition $node): void
234231
{
235-
/** @phpstan-ignore class.notFound (Phpstan Symfony extension does not know yet how to deal with these) */
236232
$node
237233
->validate()
238234
->always(static function (array $values) {
@@ -288,7 +284,7 @@ private function configureDbalDriverNode(ArrayNodeDefinition $node): void
288284
->end()
289285
->scalarNode('default_dbname')
290286
->info(
291-
'Override the default database (postgres) to connect to for PostgreSQL connexion.',
287+
'Override the default database (postgres) to connect to for PostgreSQL connection.',
292288
)
293289
->end()
294290
->scalarNode('sslmode')
@@ -370,7 +366,6 @@ private function addOrmSection(ArrayNodeDefinition $node): void
370366
'controller_resolver' => true,
371367
];
372368

373-
/** @phpstan-ignore class.notFound (Phpstan Symfony extension does not know yet how to deal with these) */
374369
$node
375370
->children()
376371
->arrayNode('orm')
@@ -518,7 +513,6 @@ private function getOrmEntityListenersNode(): NodeDefinition
518513
return ['entities' => $entities];
519514
};
520515

521-
/** @phpstan-ignore class.notFound (Phpstan Symfony extension does not know yet how to deal with these) */
522516
$node
523517
->beforeNormalization()
524518
// Yaml normalization
@@ -564,7 +558,6 @@ private function getOrmEntityManagersNode(): ArrayNodeDefinition
564558
$treeBuilder = new TreeBuilder('entity_managers');
565559
$node = $treeBuilder->getRootNode();
566560

567-
/** @phpstan-ignore class.notFound (Phpstan Symfony extension does not know yet how to deal with these) */
568561
$node
569562
->requiresAtLeastOneElement()
570563
->useAttributeAsKey('name')
@@ -739,7 +732,6 @@ private function getOrmCacheDriverNode(string $name): ArrayNodeDefinition
739732
$treeBuilder = new TreeBuilder($name);
740733
$node = $treeBuilder->getRootNode();
741734

742-
/** @phpstan-ignore class.notFound (Phpstan Symfony extension does not know yet how to deal with these) */
743735
$node
744736
->beforeNormalization()
745737
->ifString()

src/DependencyInjection/DoctrineExtension.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,11 @@ private function loadMappingInformation(array $objectManager, ContainerBuilder $
173173
throw new InvalidArgumentException(sprintf('Bundle "%s" does not exist or it is not enabled.', $mappingName));
174174
}
175175

176-
$mappingConfig = $this->getMappingDriverBundleConfigDefaults($mappingConfig, $bundle, $container, $bundleMetadata['path']);
177-
if (! $mappingConfig) {
178-
continue;
176+
if ($bundleMetadata !== null) {
177+
$mappingConfig = $this->getMappingDriverBundleConfigDefaults($mappingConfig, $bundle, $container, $bundleMetadata['path']);
178+
if (! $mappingConfig) {
179+
continue;
180+
}
179181
}
180182
} elseif (! $mappingConfig['type']) {
181183
$mappingConfig['type'] = 'attribute';

src/DoctrineBundle.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ public function process(ContainerBuilder $container): void
7878

7979
public function shutdown(): void
8080
{
81+
if ($this->container === null) {
82+
return;
83+
}
84+
8185
// Clear all entity managers to clear references to entities for GC
8286
if ($this->container->hasParameter('doctrine.entity_managers')) {
8387
foreach ($this->container->getParameter('doctrine.entity_managers') as $id) {

src/Twig/DoctrineExtension.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\SqlFormatter\HtmlHighlighter;
88
use Doctrine\SqlFormatter\NullHighlighter;
99
use Doctrine\SqlFormatter\SqlFormatter;
10+
use RuntimeException;
1011
use Stringable;
1112
use Symfony\Component\VarDumper\Cloner\Data;
1213
use Twig\Extension\AbstractExtension;
@@ -24,11 +25,15 @@
2425
use function is_array;
2526
use function is_bool;
2627
use function is_string;
28+
use function preg_last_error;
2729
use function preg_match;
2830
use function preg_replace_callback;
31+
use function sprintf;
2932
use function strtoupper;
3033
use function substr;
3134

35+
use const PREG_NO_ERROR;
36+
3237
/**
3338
* This class contains the needed functions in order to do the query highlighting
3439
*
@@ -116,7 +121,7 @@ public function replaceQueryParameters(string $query, array|Data $parameters): s
116121

117122
$i = 0;
118123

119-
return preg_replace_callback(
124+
$result = preg_replace_callback(
120125
'/(?<!\?)\?(?!\?)|(?<!:)(:[a-z0-9_]+)/i',
121126
static function (array $matches) use ($parameters, &$i): string {
122127
$key = substr($matches[0], 1);
@@ -133,6 +138,17 @@ static function (array $matches) use ($parameters, &$i): string {
133138
},
134139
$query,
135140
);
141+
142+
$pregError = preg_last_error();
143+
if ($pregError !== PREG_NO_ERROR) {
144+
throw new RuntimeException(sprintf('Failed to replace query parameters: PCRE error %d', $pregError));
145+
}
146+
147+
if ($result === null) {
148+
throw new RuntimeException('Failed to replace query parameters: unexpected null result');
149+
}
150+
151+
return $result;
136152
}
137153

138154
public function prettifySql(string $sql): string

tests/Command/CreateDatabaseDoctrineTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private function getMockContainer(string $connectionName, array|null $params = n
7777

7878
$mockContainer = $this->createStub(Container::class);
7979

80-
$mockContainer->method('get')->with('doctrine')->willReturn($mockDoctrine);
80+
$mockContainer->method('get')->willReturnMap([['doctrine', $mockDoctrine]]);
8181

8282
return $mockContainer;
8383
}

0 commit comments

Comments
 (0)