Skip to content

Commit c9f3147

Browse files
committed
Process parameters/services only if they were found in XML file
Otherwise it may fail with `foreach() argument must be of type array|object, null given`.
1 parent d19172c commit c9f3147

File tree

2 files changed

+44
-36
lines changed

2 files changed

+44
-36
lines changed

Diff for: src/Symfony/XmlParameterMapFactory.php

+12-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
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;
1112
use function ksort;
@@ -41,16 +42,19 @@ public function create(): ParameterMap
4142

4243
/** @var Parameter[] $parameters */
4344
$parameters = [];
44-
foreach ($xml->parameters->parameter as $def) {
45-
/** @var SimpleXMLElement $attrs */
46-
$attrs = $def->attributes();
4745

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

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

5660
ksort($parameters);

Diff for: src/Symfony/XmlServiceMapFactory.php

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

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

68-
if ($service->getAlias() !== null) {
69-
$aliases[] = $service;
70-
} else {
71-
$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+
}
7276
}
7377
}
7478
foreach ($aliases as $service) {

0 commit comments

Comments
 (0)