Skip to content

Commit ed1727c

Browse files
committed
The XmlContainer must exists. Throw exception, if container not exists. + test
1 parent 8e8fa73 commit ed1727c

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Lookyman\PHPStan\Symfony\Exception;
6+
7+
final class XmlContainerNotExistsException extends \InvalidArgumentException
8+
{
9+
10+
}

src/ServiceMap.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Lookyman\PHPStan\Symfony;
66

7+
use Lookyman\PHPStan\Symfony\Exception\XmlContainerNotExistsException;
78
use PhpParser\Node;
89
use PhpParser\Node\Expr\ClassConstFetch;
910
use PhpParser\Node\Name;
@@ -21,7 +22,11 @@ public function __construct(string $containerXml)
2122
{
2223
$this->services = $aliases = [];
2324
/** @var \SimpleXMLElement $def */
24-
foreach (\simplexml_load_file($containerXml)->services->service as $def) {
25+
$xml = @\simplexml_load_file($containerXml);
26+
if ($xml === false) {
27+
throw new XmlContainerNotExistsException(\sprintf('Container %s not exists', $containerXml));
28+
}
29+
foreach ($xml->services->service as $def) {
2530
$attrs = $def->attributes();
2631
if (!isset($attrs->id)) {
2732
continue;

tests/ServiceMapTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ public function testGetServiceFromNode(array $service)
2424
self::assertEquals($service, $serviceMap->getServiceFromNode(new String_($service['id'])));
2525
}
2626

27+
/**
28+
* @expectedException \Lookyman\PHPStan\Symfony\Exception\XmlContainerNotExistsException
29+
*/
30+
public function testFileNotExists()
31+
{
32+
new ServiceMap(__DIR__ . '/foo.xml');
33+
}
34+
2735
public function getServiceFromNodeProvider(): array
2836
{
2937
return [

0 commit comments

Comments
 (0)