Skip to content

Commit

Permalink
Do not load service names which are class or interfaces themselves (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Slamdunk authored May 19, 2021
1 parent 6903299 commit f5c0da2
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
includes:
- phar://phpstan.phar/conf/config.levelmax.neon
- vendor/phpstan/phpstan-phpunit/extension.neon

parameters:
level: max
paths:
- src/
- tests/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
]
Original file line number Diff line number Diff line change
@@ -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<string, string>.",
"line": 39,
"line": 44,
"ignorable": true
}
]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"message": "Offset 'xyz' does not exist on array('foo' => 'bar').",
"line": 38,
"line": 43,
"ignorable": true
}
]
5 changes: 5 additions & 0 deletions tests/LaminasIntegration/data/serviceManagerDynamicReturn.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace LaminasPhpStan\Tests\LaminasIntegration\data;

use Laminas\ServiceManager\ServiceLocatorInterface;
use LaminasPhpStan\TestAsset\HeavyService;

final class serviceManagerDynamicReturn
{
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/LaminasIntegration/servicemanagerloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -28,6 +30,9 @@ public function getConfig()
'foo' => 'bar',
],
],
'factories' => [
HeavyService::class => InvokableFactory::class,
],
],
'controllers' => [
'invokables' => [
Expand Down
16 changes: 16 additions & 0 deletions tests/TestAsset/HeavyService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace LaminasPhpStan\TestAsset;

final class HeavyService
{
public function __construct()
{
throw new \RuntimeException('Too heavy to load for Static Analysis');
}

public function foo(): bool
{
return true;
}
}

0 comments on commit f5c0da2

Please sign in to comment.