Skip to content

Commit b1a2809

Browse files
committed
Fixed handling of self, static, and parent
1 parent 5d0da0e commit b1a2809

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

src/ServiceMap.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ public static function getServiceIdFromNode(Node $node)
7575
return $node->value;
7676
}
7777
if ($node instanceof ClassConstFetch && $node->class instanceof Name) {
78-
return $node->class->toString();
78+
$serviceId = $node->class->toString();
79+
if (!\in_array($serviceId, ['self', 'static', 'parent'], true)) {
80+
return $serviceId;
81+
}
7982
}
8083
return \null;
8184
}

tests/Rules/ContainerInterfacePrivateServiceRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function testGetPrivateService()
2222
$this->analyse([__DIR__ . '/data/ExampleController.php'], [
2323
[
2424
'Service "private" is private.',
25-
14,
25+
15,
2626
],
2727
]);
2828
}

tests/Rules/ContainerInterfaceUnknownServiceRuleTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ public function testGetUnknownService()
2222
$this->analyse([__DIR__ . '/data/ExampleController.php'], [
2323
[
2424
'Service "service.not.found" is not registered in the container.',
25-
20,
25+
21,
26+
],
27+
[
28+
'Service "Lookyman\PHPStan\Symfony\ServiceMap" is not registered in the container.',
29+
27,
2630
],
2731
]);
2832
}

tests/Rules/data/ExampleController.php

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

55
namespace Lookyman\PHPStan\Symfony\Rules\data;
66

7+
use Lookyman\PHPStan\Symfony\ServiceMap;
78
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
89

910
final class ExampleController extends Controller
@@ -21,16 +22,28 @@ public function getUnknownService()
2122
$service->noMethod();
2223
}
2324

25+
public function getUnknownServiceByClass()
26+
{
27+
$service = $this->get(ServiceMap::class);
28+
$service->noMethod();
29+
}
30+
2431
public function getVariableService(string $serviceKey)
2532
{
2633
$service = $this->get($serviceKey);
2734
$service->noMethod();
2835
}
2936

30-
public function getConcatenatedService(string $serviceKey)
37+
public function getConcatenatedService()
3138
{
3239
$service = $this->get('service.' . self::class);
3340
$service->noMethod();
3441
}
3542

43+
public function getSelfService()
44+
{
45+
$service = $this->get(self::class);
46+
$service->noMethod();
47+
}
48+
3649
}

0 commit comments

Comments
 (0)