Skip to content

Commit a531474

Browse files
committed
AutowiredAttributeServicesExtension - add service tags based on implemented interfaces
1 parent 5d756a0 commit a531474

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

conf/config.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,11 +1150,6 @@ services:
11501150
-
11511151
class: PHPStan\Type\BitwiseFlagHelper
11521152

1153-
-
1154-
class: PHPStan\Type\Php\AbsFunctionDynamicReturnTypeExtension
1155-
tags:
1156-
- phpstan.broker.dynamicFunctionReturnTypeExtension
1157-
11581153
-
11591154
class: PHPStan\Type\Php\ArgumentBasedFunctionReturnTypeExtension
11601155
tags:

src/DependencyInjection/AutowiredAttributeServicesExtension.php

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

55
use Nette\DI\CompilerExtension;
66
use olvlvl\ComposerAttributeCollector\Attributes;
7+
use PHPStan\Broker\BrokerFactory;
8+
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
9+
use ReflectionClass;
710

811
final class AutowiredAttributeServicesExtension extends CompilerExtension
912
{
@@ -14,10 +17,24 @@ public function loadConfiguration(): void
1417
$autowiredServiceClasses = Attributes::findTargetClasses(AutowiredService::class);
1518
$builder = $this->getContainerBuilder();
1619

20+
$interfaceToTag = [
21+
DynamicFunctionReturnTypeExtension::class => BrokerFactory::DYNAMIC_FUNCTION_RETURN_TYPE_EXTENSION_TAG,
22+
];
23+
1724
foreach ($autowiredServiceClasses as $class) {
18-
$builder->addDefinition(null)
25+
$reflection = new ReflectionClass($class->name);
26+
27+
$definition = $builder->addDefinition(null)
1928
->setType($class->name)
2029
->setAutowired();
30+
31+
foreach ($interfaceToTag as $interface => $tag) {
32+
if (!$reflection->implementsInterface($interface)) {
33+
continue;
34+
}
35+
36+
$definition->addTag($tag);
37+
}
2138
}
2239
}
2340

src/Type/Php/AbsFunctionDynamicReturnTypeExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
use PhpParser\Node\Expr\FuncCall;
66
use PHPStan\Analyser\Scope;
7+
use PHPStan\DependencyInjection\AutowiredService;
78
use PHPStan\Reflection\FunctionReflection;
89
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
910
use PHPStan\Type\ErrorType;
1011
use PHPStan\Type\Type;
1112

13+
#[AutowiredService]
1214
final class AbsFunctionDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
1315
{
1416

0 commit comments

Comments
 (0)