Skip to content

Commit 65f02c7

Browse files
authoredJan 21, 2025
Add result cache meta extension for DI container
1 parent 7417f3a commit 65f02c7

6 files changed

+412
-37
lines changed
 

‎composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"require": {
1616
"php": "^7.4 || ^8.0",
1717
"ext-simplexml": "*",
18-
"phpstan/phpstan": "^2.0"
18+
"phpstan/phpstan": "^2.1.2"
1919
},
2020
"conflict": {
2121
"symfony/framework-bundle": "<3.0"

‎extension.neon

+5
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,8 @@ services:
363363
-
364364
factory: PHPStan\Type\Symfony\ExtensionGetConfigurationReturnTypeExtension
365365
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
366+
367+
-
368+
class: PHPStan\Symfony\SymfonyContainerResultCacheMetaExtension
369+
tags:
370+
- phpstan.resultCacheMetaExtension
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Symfony;
4+
5+
use PHPStan\Analyser\ResultCache\ResultCacheMetaExtension;
6+
use function array_map;
7+
use function hash;
8+
use function ksort;
9+
use function sort;
10+
use function var_export;
11+
12+
final class SymfonyContainerResultCacheMetaExtension implements ResultCacheMetaExtension
13+
{
14+
15+
private ParameterMap $parameterMap;
16+
17+
private ServiceMap $serviceMap;
18+
19+
public function __construct(ParameterMap $parameterMap, ServiceMap $serviceMap)
20+
{
21+
$this->parameterMap = $parameterMap;
22+
$this->serviceMap = $serviceMap;
23+
}
24+
25+
public function getKey(): string
26+
{
27+
return 'symfonyDiContainer';
28+
}
29+
30+
public function getHash(): string
31+
{
32+
$services = $parameters = [];
33+
34+
foreach ($this->parameterMap->getParameters() as $parameter) {
35+
$parameters[$parameter->getKey()] = $parameter->getValue();
36+
}
37+
ksort($parameters);
38+
39+
foreach ($this->serviceMap->getServices() as $service) {
40+
$serviceTags = array_map(
41+
static fn (ServiceTag $tag) => [
42+
'name' => $tag->getName(),
43+
'attributes' => $tag->getAttributes(),
44+
],
45+
$service->getTags(),
46+
);
47+
sort($serviceTags);
48+
49+
$services[$service->getId()] = [
50+
'class' => $service->getClass(),
51+
'public' => $service->isPublic() ? 'yes' : 'no',
52+
'synthetic' => $service->isSynthetic() ? 'yes' : 'no',
53+
'alias' => $service->getAlias(),
54+
'tags' => $serviceTags,
55+
];
56+
}
57+
ksort($services);
58+
59+
return hash('sha256', var_export(['parameters' => $parameters, 'services' => $services], true));
60+
}
61+
62+
}

‎src/Symfony/XmlParameterMapFactory.php

+15-8
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
use PHPStan\ShouldNotHappenException;
77
use SimpleXMLElement;
88
use function base64_decode;
9+
use function count;
910
use function file_get_contents;
1011
use function is_numeric;
12+
use function ksort;
1113
use function simplexml_load_string;
1214
use function sprintf;
1315
use function strpos;
@@ -40,18 +42,23 @@ public function create(): ParameterMap
4042

4143
/** @var Parameter[] $parameters */
4244
$parameters = [];
43-
foreach ($xml->parameters->parameter as $def) {
44-
/** @var SimpleXMLElement $attrs */
45-
$attrs = $def->attributes();
4645

47-
$parameter = new Parameter(
48-
(string) $attrs->key,
49-
$this->getNodeValue($def),
50-
);
46+
if (count($xml->parameters) > 0) {
47+
foreach ($xml->parameters->parameter as $def) {
48+
/** @var SimpleXMLElement $attrs */
49+
$attrs = $def->attributes();
5150

52-
$parameters[$parameter->getKey()] = $parameter;
51+
$parameter = new Parameter(
52+
(string) $attrs->key,
53+
$this->getNodeValue($def),
54+
);
55+
56+
$parameters[$parameter->getKey()] = $parameter;
57+
}
5358
}
5459

60+
ksort($parameters);
61+
5562
return new DefaultParameterMap($parameters);
5663
}
5764

‎src/Symfony/XmlServiceMapFactory.php

+35-28
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace PHPStan\Symfony;
44

55
use SimpleXMLElement;
6+
use function count;
67
use function file_get_contents;
8+
use function ksort;
79
use function simplexml_load_string;
810
use function sprintf;
911
use function strpos;
@@ -39,35 +41,38 @@ public function create(): ServiceMap
3941
$services = [];
4042
/** @var Service[] $aliases */
4143
$aliases = [];
42-
foreach ($xml->services->service as $def) {
43-
/** @var SimpleXMLElement $attrs */
44-
$attrs = $def->attributes();
45-
if (!isset($attrs->id)) {
46-
continue;
47-
}
48-
49-
$serviceTags = [];
50-
foreach ($def->tag as $tag) {
51-
$tagAttrs = ((array) $tag->attributes())['@attributes'] ?? [];
52-
$tagName = $tagAttrs['name'];
53-
unset($tagAttrs['name']);
54-
55-
$serviceTags[] = new ServiceTag($tagName, $tagAttrs);
56-
}
57-
58-
$service = new Service(
59-
$this->cleanServiceId((string) $attrs->id),
60-
isset($attrs->class) ? (string) $attrs->class : null,
61-
isset($attrs->public) && (string) $attrs->public === 'true',
62-
isset($attrs->synthetic) && (string) $attrs->synthetic === 'true',
63-
isset($attrs->alias) ? $this->cleanServiceId((string) $attrs->alias) : null,
64-
$serviceTags,
65-
);
6644

67-
if ($service->getAlias() !== null) {
68-
$aliases[] = $service;
69-
} else {
70-
$services[$service->getId()] = $service;
45+
if (count($xml->services) > 0) {
46+
foreach ($xml->services->service as $def) {
47+
/** @var SimpleXMLElement $attrs */
48+
$attrs = $def->attributes();
49+
if (!isset($attrs->id)) {
50+
continue;
51+
}
52+
53+
$serviceTags = [];
54+
foreach ($def->tag as $tag) {
55+
$tagAttrs = ((array) $tag->attributes())['@attributes'] ?? [];
56+
$tagName = $tagAttrs['name'];
57+
unset($tagAttrs['name']);
58+
59+
$serviceTags[] = new ServiceTag($tagName, $tagAttrs);
60+
}
61+
62+
$service = new Service(
63+
$this->cleanServiceId((string) $attrs->id),
64+
isset($attrs->class) ? (string) $attrs->class : null,
65+
isset($attrs->public) && (string) $attrs->public === 'true',
66+
isset($attrs->synthetic) && (string) $attrs->synthetic === 'true',
67+
isset($attrs->alias) ? $this->cleanServiceId((string) $attrs->alias) : null,
68+
$serviceTags,
69+
);
70+
71+
if ($service->getAlias() !== null) {
72+
$aliases[] = $service;
73+
} else {
74+
$services[$service->getId()] = $service;
75+
}
7176
}
7277
}
7378
foreach ($aliases as $service) {
@@ -85,6 +90,8 @@ public function create(): ServiceMap
8590
);
8691
}
8792

93+
ksort($services);
94+
8895
return new DefaultServiceMap($services);
8996
}
9097

0 commit comments

Comments
 (0)
Failed to load comments.