diff --git a/phpstan.neon b/phpstan.neon index abef462..13b0b4a 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,8 +1,8 @@ includes: - - phar://phpstan.phar/conf/config.levelmax.neon - vendor/phpstan/phpstan-phpunit/extension.neon parameters: + level: max paths: - src/ - tests/ diff --git a/src/Type/Laminas/ServiceManagerGetDynamicReturnTypeExtension.php b/src/Type/Laminas/ServiceManagerGetDynamicReturnTypeExtension.php index c87d890..6215260 100644 --- a/src/Type/Laminas/ServiceManagerGetDynamicReturnTypeExtension.php +++ b/src/Type/Laminas/ServiceManagerGetDynamicReturnTypeExtension.php @@ -82,6 +82,10 @@ public function getTypeFromMethodCall( return new NeverType(); } + if (\class_exists($serviceName) || \interface_exists($serviceName)) { + return new ObjectServiceManagerType($serviceName, $serviceName); + } + $service = $serviceManager->get($serviceName); if (\is_object($service)) { return new ObjectServiceManagerType(\get_class($service), $serviceName); diff --git a/tests/LaminasIntegration/data/serviceManagerDynamicReturn-0.json b/tests/LaminasIntegration/data/serviceManagerDynamicReturn-0.json index fa0ccdc..1d579e6 100644 --- a/tests/LaminasIntegration/data/serviceManagerDynamicReturn-0.json +++ b/tests/LaminasIntegration/data/serviceManagerDynamicReturn-0.json @@ -1,12 +1,12 @@ [ { "message": "The service \"bar\" was not configured in Laminas\\ServiceManager\\ServiceLocatorInterface.", - "line": 23, + "line": 24, "ignorable": true }, { "message": "The service \"foobar\" was not configured in ViewHelperManager nor the class \"foobar\" exists.", - "line": 31, + "line": 32, "ignorable": true } ] \ No newline at end of file diff --git a/tests/LaminasIntegration/data/serviceManagerDynamicReturn-2.json b/tests/LaminasIntegration/data/serviceManagerDynamicReturn-2.json index 3210832..c546e5f 100644 --- a/tests/LaminasIntegration/data/serviceManagerDynamicReturn-2.json +++ b/tests/LaminasIntegration/data/serviceManagerDynamicReturn-2.json @@ -1,12 +1,17 @@ [ { "message": "Call to an undefined method LaminasPhpStan\\TestAsset\\CssService::isCssWut().", - "line": 29, + "line": 30, + "ignorable": true + }, + { + "message": "Call to an undefined method LaminasPhpStan\\TestAsset\\HeavyService::bar().", + "line": 36, "ignorable": true }, { "message": "Cannot access property $foo on array.", - "line": 39, + "line": 44, "ignorable": true } ] \ No newline at end of file diff --git a/tests/LaminasIntegration/data/serviceManagerDynamicReturn-3.json b/tests/LaminasIntegration/data/serviceManagerDynamicReturn-3.json index 3256f3a..f9f5abd 100644 --- a/tests/LaminasIntegration/data/serviceManagerDynamicReturn-3.json +++ b/tests/LaminasIntegration/data/serviceManagerDynamicReturn-3.json @@ -1,7 +1,7 @@ [ { "message": "Offset 'xyz' does not exist on array('foo' => 'bar').", - "line": 38, + "line": 43, "ignorable": true } ] \ No newline at end of file diff --git a/tests/LaminasIntegration/data/serviceManagerDynamicReturn.php b/tests/LaminasIntegration/data/serviceManagerDynamicReturn.php index 6cf4484..2f254d4 100644 --- a/tests/LaminasIntegration/data/serviceManagerDynamicReturn.php +++ b/tests/LaminasIntegration/data/serviceManagerDynamicReturn.php @@ -5,6 +5,7 @@ namespace LaminasPhpStan\Tests\LaminasIntegration\data; use Laminas\ServiceManager\ServiceLocatorInterface; +use LaminasPhpStan\TestAsset\HeavyService; final class serviceManagerDynamicReturn { @@ -29,6 +30,10 @@ public function getDynamicType(): void $viewHelperManager->get('css')->isCssWut(); $viewHelperManager->get('foobar')->isCss(); + + $heavyService = $this->serviceManager->get(HeavyService::class); + $heavyService->foo(); + $heavyService->bar(); } public function nonObjectServices(): void diff --git a/tests/LaminasIntegration/servicemanagerloader.php b/tests/LaminasIntegration/servicemanagerloader.php index 84e20a2..8fbce97 100644 --- a/tests/LaminasIntegration/servicemanagerloader.php +++ b/tests/LaminasIntegration/servicemanagerloader.php @@ -3,9 +3,11 @@ declare(strict_types=1); use Laminas\ModuleManager\Feature\ConfigProviderInterface; +use Laminas\ServiceManager\Factory\InvokableFactory; use LaminasPhpStan\TestAsset\BarService; use LaminasPhpStan\TestAsset\CssService; use LaminasPhpStan\TestAsset\FooService; +use LaminasPhpStan\TestAsset\HeavyService; use LaminasPhpStan\TestAsset\Route66; use LaminasPhpStan\TestAsset\XyzController; @@ -28,6 +30,9 @@ public function getConfig() 'foo' => 'bar', ], ], + 'factories' => [ + HeavyService::class => InvokableFactory::class, + ], ], 'controllers' => [ 'invokables' => [ diff --git a/tests/TestAsset/HeavyService.php b/tests/TestAsset/HeavyService.php new file mode 100644 index 0000000..22117c4 --- /dev/null +++ b/tests/TestAsset/HeavyService.php @@ -0,0 +1,16 @@ +