Skip to content

Commit f5c0da2

Browse files
authored
Do not load service names which are class or interfaces themselves (#23)
1 parent 6903299 commit f5c0da2

File tree

8 files changed

+41
-6
lines changed

8 files changed

+41
-6
lines changed

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
includes:
2-
- phar://phpstan.phar/conf/config.levelmax.neon
32
- vendor/phpstan/phpstan-phpunit/extension.neon
43

54
parameters:
5+
level: max
66
paths:
77
- src/
88
- tests/

src/Type/Laminas/ServiceManagerGetDynamicReturnTypeExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ public function getTypeFromMethodCall(
8282
return new NeverType();
8383
}
8484

85+
if (\class_exists($serviceName) || \interface_exists($serviceName)) {
86+
return new ObjectServiceManagerType($serviceName, $serviceName);
87+
}
88+
8589
$service = $serviceManager->get($serviceName);
8690
if (\is_object($service)) {
8791
return new ObjectServiceManagerType(\get_class($service), $serviceName);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[
22
{
33
"message": "The service \"bar\" was not configured in Laminas\\ServiceManager\\ServiceLocatorInterface.",
4-
"line": 23,
4+
"line": 24,
55
"ignorable": true
66
},
77
{
88
"message": "The service \"foobar\" was not configured in ViewHelperManager nor the class \"foobar\" exists.",
9-
"line": 31,
9+
"line": 32,
1010
"ignorable": true
1111
}
1212
]
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
[
22
{
33
"message": "Call to an undefined method LaminasPhpStan\\TestAsset\\CssService::isCssWut().",
4-
"line": 29,
4+
"line": 30,
5+
"ignorable": true
6+
},
7+
{
8+
"message": "Call to an undefined method LaminasPhpStan\\TestAsset\\HeavyService::bar().",
9+
"line": 36,
510
"ignorable": true
611
},
712
{
813
"message": "Cannot access property $foo on array<string, string>.",
9-
"line": 39,
14+
"line": 44,
1015
"ignorable": true
1116
}
1217
]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"message": "Offset 'xyz' does not exist on array('foo' => 'bar').",
4-
"line": 38,
4+
"line": 43,
55
"ignorable": true
66
}
77
]

tests/LaminasIntegration/data/serviceManagerDynamicReturn.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace LaminasPhpStan\Tests\LaminasIntegration\data;
66

77
use Laminas\ServiceManager\ServiceLocatorInterface;
8+
use LaminasPhpStan\TestAsset\HeavyService;
89

910
final class serviceManagerDynamicReturn
1011
{
@@ -29,6 +30,10 @@ public function getDynamicType(): void
2930
$viewHelperManager->get('css')->isCssWut();
3031

3132
$viewHelperManager->get('foobar')->isCss();
33+
34+
$heavyService = $this->serviceManager->get(HeavyService::class);
35+
$heavyService->foo();
36+
$heavyService->bar();
3237
}
3338

3439
public function nonObjectServices(): void

tests/LaminasIntegration/servicemanagerloader.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
declare(strict_types=1);
44

55
use Laminas\ModuleManager\Feature\ConfigProviderInterface;
6+
use Laminas\ServiceManager\Factory\InvokableFactory;
67
use LaminasPhpStan\TestAsset\BarService;
78
use LaminasPhpStan\TestAsset\CssService;
89
use LaminasPhpStan\TestAsset\FooService;
10+
use LaminasPhpStan\TestAsset\HeavyService;
911
use LaminasPhpStan\TestAsset\Route66;
1012
use LaminasPhpStan\TestAsset\XyzController;
1113

@@ -28,6 +30,9 @@ public function getConfig()
2830
'foo' => 'bar',
2931
],
3032
],
33+
'factories' => [
34+
HeavyService::class => InvokableFactory::class,
35+
],
3136
],
3237
'controllers' => [
3338
'invokables' => [

tests/TestAsset/HeavyService.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace LaminasPhpStan\TestAsset;
4+
5+
final class HeavyService
6+
{
7+
public function __construct()
8+
{
9+
throw new \RuntimeException('Too heavy to load for Static Analysis');
10+
}
11+
12+
public function foo(): bool
13+
{
14+
return true;
15+
}
16+
}

0 commit comments

Comments
 (0)