Skip to content

Commit abdd72f

Browse files
committed
AutowiredService - support for "as" - limiting the type under which the service is known to autowiring
1 parent fe93616 commit abdd72f

File tree

12 files changed

+34
-59
lines changed

12 files changed

+34
-59
lines changed

conf/config.neon

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,6 @@ conditionalTags:
233233

234234
services:
235235

236-
-
237-
class: PHPStan\Node\Printer\Printer
238-
autowired:
239-
- PHPStan\Node\Printer\Printer
240-
241236
-
242237
class: PHPStan\Php\PhpVersion
243238
factory: @PHPStan\Php\PhpVersionFactory::create
@@ -250,19 +245,6 @@ services:
250245
class: PHPStan\PhpDoc\TypeNodeResolverExtensionRegistryProvider
251246
factory: PHPStan\PhpDoc\LazyTypeNodeResolverExtensionRegistryProvider
252247

253-
-
254-
class: PHPStan\PhpDoc\DefaultStubFilesProvider
255-
arguments:
256-
stubFiles: %stubFiles%
257-
composerAutoloaderProjectPaths: %composerAutoloaderProjectPaths%
258-
autowired:
259-
- PHPStan\PhpDoc\StubFilesProvider
260-
261-
-
262-
class: PHPStan\Analyser\LazyInternalScopeFactory
263-
autowired:
264-
- PHPStan\Analyser\InternalScopeFactory
265-
266248
-
267249
class: PHPStan\Analyser\NodeScopeResolver
268250
arguments:
@@ -309,11 +291,6 @@ services:
309291
arguments:
310292
originalContainer: @PHPStan\DependencyInjection\Nette\NetteContainer
311293

312-
-
313-
class: PHPStan\DependencyInjection\Nette\NetteContainer
314-
autowired:
315-
- PHPStan\DependencyInjection\Nette\NetteContainer
316-
317294
-
318295
class: PHPStan\DependencyInjection\Reflection\ClassReflectionExtensionRegistryProvider
319296
factory: PHPStan\DependencyInjection\Reflection\LazyClassReflectionExtensionRegistryProvider
@@ -365,32 +342,10 @@ services:
365342
class: PHPStan\Reflection\ReflectionProvider\ReflectionProviderProvider
366343
factory: PHPStan\Reflection\ReflectionProvider\LazyReflectionProviderProvider
367344

368-
-
369-
class: PHPStan\Reflection\SignatureMap\FunctionSignatureMapProvider
370-
arguments:
371-
stricterFunctionMap: %featureToggles.stricterFunctionMap%
372-
autowired:
373-
- PHPStan\Reflection\SignatureMap\FunctionSignatureMapProvider
374-
375-
-
376-
class: PHPStan\Reflection\SignatureMap\Php8SignatureMapProvider
377-
autowired:
378-
- PHPStan\Reflection\SignatureMap\Php8SignatureMapProvider
379-
380345
-
381346
class: PHPStan\Reflection\SignatureMap\SignatureMapProvider
382347
factory: @PHPStan\Reflection\SignatureMap\SignatureMapProviderFactory::create()
383348

384-
-
385-
class: PHPStan\Rules\Exceptions\DefaultExceptionTypeResolver
386-
arguments:
387-
uncheckedExceptionRegexes: %exceptions.uncheckedExceptionRegexes%
388-
uncheckedExceptionClasses: %exceptions.uncheckedExceptionClasses%
389-
checkedExceptionRegexes: %exceptions.checkedExceptionRegexes%
390-
checkedExceptionClasses: %exceptions.checkedExceptionClasses%
391-
autowired:
392-
- PHPStan\Rules\Exceptions\DefaultExceptionTypeResolver
393-
394349
-
395350
class: PHPStan\Type\TypeAliasResolver
396351
factory: PHPStan\Type\UsefulTypeAliasResolver
@@ -430,11 +385,6 @@ services:
430385
parentDirectory: %currentWorkingDirectory%
431386
autowired: false
432387

433-
registry:
434-
class: PHPStan\Rules\LazyRegistry
435-
autowired:
436-
- PHPStan\Rules\Registry
437-
438388
# Reflection providers
439389

440390
betterReflectionReflector:
@@ -484,10 +434,3 @@ services:
484434
allConfigFiles: %allConfigFiles%
485435
configPhpVersion: %phpVersion%
486436
autowired: false
487-
488-
# Error formatters
489-
490-
-
491-
class: PHPStan\Command\ErrorFormatter\CiDetectedErrorFormatter
492-
autowired:
493-
- PHPStan\Command\ErrorFormatter\CiDetectedErrorFormatter

src/Analyser/LazyInternalScopeFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Analyser;
44

5+
use PHPStan\DependencyInjection\AutowiredService;
56
use PHPStan\DependencyInjection\Container;
67
use PHPStan\DependencyInjection\Type\DynamicReturnTypeExtensionRegistryProvider;
78
use PHPStan\DependencyInjection\Type\ExpressionTypeResolverExtensionRegistryProvider;
@@ -14,6 +15,7 @@
1415
use PHPStan\Reflection\ReflectionProvider;
1516
use PHPStan\Rules\Properties\PropertyReflectionFinder;
1617

18+
#[AutowiredService(as: InternalScopeFactory::class)]
1719
final class LazyInternalScopeFactory implements InternalScopeFactory
1820
{
1921

src/Command/ErrorFormatter/CiDetectedErrorFormatter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
use OndraM\CiDetector\Exception\CiNotDetectedException;
77
use PHPStan\Command\AnalysisResult;
88
use PHPStan\Command\Output;
9+
use PHPStan\DependencyInjection\AutowiredService;
910

1011
/**
1112
* @api
1213
*/
14+
#[AutowiredService(as: CiDetectedErrorFormatter::class)]
1315
final class CiDetectedErrorFormatter implements ErrorFormatter
1416
{
1517

src/DependencyInjection/AutowiredAttributeServicesExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function loadConfiguration(): void
4141

4242
$definition = $builder->addDefinition($attribute->name)
4343
->setType($class->name)
44-
->setAutowired();
44+
->setAutowired($attribute->as);
4545

4646
$this->processParameters($class->name, $definition, $autowiredParameters);
4747

src/DependencyInjection/AutowiredService.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
final class AutowiredService
1717
{
1818

19-
public function __construct(public ?string $name = null)
19+
/**
20+
* @param string|null $name
21+
* @param true|list<class-string>|class-string $as
22+
*/
23+
public function __construct(public ?string $name = null, public bool|array|string $as = true)
2024
{
2125
}
2226

src/DependencyInjection/Nette/NetteContainer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\DependencyInjection\Nette;
44

5+
use PHPStan\DependencyInjection\AutowiredService;
56
use PHPStan\DependencyInjection\Container;
67
use PHPStan\DependencyInjection\MissingServiceException;
78
use PHPStan\DependencyInjection\ParameterNotFoundException;
@@ -12,6 +13,7 @@
1213
/**
1314
* @internal
1415
*/
16+
#[AutowiredService(as: NetteContainer::class)]
1517
final class NetteContainer implements Container
1618
{
1719

src/Node/Printer/Printer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\Node\Printer;
44

55
use PhpParser\PrettyPrinter\Standard;
6+
use PHPStan\DependencyInjection\AutowiredService;
67
use PHPStan\Node\Expr\AlwaysRememberedExpr;
78
use PHPStan\Node\Expr\ExistingArrayDimFetch;
89
use PHPStan\Node\Expr\GetIterableKeyTypeExpr;
@@ -22,6 +23,7 @@
2223
/**
2324
* @api
2425
*/
26+
#[AutowiredService(as: Printer::class)]
2527
final class Printer extends Standard
2628
{
2729

src/PhpDoc/DefaultStubFilesProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
namespace PHPStan\PhpDoc;
44

5+
use PHPStan\DependencyInjection\AutowiredParameter;
6+
use PHPStan\DependencyInjection\AutowiredService;
57
use PHPStan\DependencyInjection\Container;
68
use PHPStan\Internal\ComposerHelper;
79
use function array_filter;
810
use function array_values;
911
use function str_contains;
1012
use function strtr;
1113

14+
#[AutowiredService(as: StubFilesProvider::class)]
1215
final class DefaultStubFilesProvider implements StubFilesProvider
1316
{
1417

@@ -24,7 +27,9 @@ final class DefaultStubFilesProvider implements StubFilesProvider
2427
*/
2528
public function __construct(
2629
private Container $container,
30+
#[AutowiredParameter]
2731
private array $stubFiles,
32+
#[AutowiredParameter]
2833
private array $composerAutoloaderProjectPaths,
2934
)
3035
{

src/Reflection/SignatureMap/FunctionSignatureMapProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionFunction;
66
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionMethod;
7+
use PHPStan\DependencyInjection\AutowiredParameter;
8+
use PHPStan\DependencyInjection\AutowiredService;
79
use PHPStan\Php\PhpVersion;
810
use PHPStan\Reflection\InitializerExprContext;
911
use PHPStan\Reflection\InitializerExprTypeResolver;
@@ -19,6 +21,7 @@
1921
use function strtolower;
2022
use const CASE_LOWER;
2123

24+
#[AutowiredService(as: FunctionSignatureMapProvider::class)]
2225
final class FunctionSignatureMapProvider implements SignatureMapProvider
2326
{
2427

@@ -32,6 +35,7 @@ public function __construct(
3235
private SignatureMapParser $parser,
3336
private InitializerExprTypeResolver $initializerExprTypeResolver,
3437
private PhpVersion $phpVersion,
38+
#[AutowiredParameter(ref: '%featureToggles.stricterFunctionMap%')]
3539
private bool $stricterFunctionMap,
3640
)
3741
{

src/Reflection/SignatureMap/Php8SignatureMapProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpParser\Node\Stmt\ClassMethod;
1111
use PhpParser\Node\Stmt\Function_;
1212
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionMethod;
13+
use PHPStan\DependencyInjection\AutowiredService;
1314
use PHPStan\Php\PhpVersion;
1415
use PHPStan\Php8StubsMap;
1516
use PHPStan\PhpDoc\Tag\ParamTag;
@@ -34,6 +35,7 @@
3435
use function sprintf;
3536
use function strtolower;
3637

38+
#[AutowiredService(as: Php8SignatureMapProvider::class)]
3739
final class Php8SignatureMapProvider implements SignatureMapProvider
3840
{
3941

src/Rules/Exceptions/DefaultExceptionTypeResolver.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44

55
use Nette\Utils\Strings;
66
use PHPStan\Analyser\Scope;
7+
use PHPStan\DependencyInjection\AutowiredParameter;
8+
use PHPStan\DependencyInjection\AutowiredService;
79
use PHPStan\Reflection\ReflectionProvider;
810
use function count;
911

1012
/**
1113
* @api
1214
*/
15+
#[AutowiredService(as: DefaultExceptionTypeResolver::class)]
1316
final class DefaultExceptionTypeResolver implements ExceptionTypeResolver
1417
{
1518

@@ -21,9 +24,13 @@ final class DefaultExceptionTypeResolver implements ExceptionTypeResolver
2124
*/
2225
public function __construct(
2326
private ReflectionProvider $reflectionProvider,
27+
#[AutowiredParameter(ref: '%exceptions.uncheckedExceptionRegexes%')]
2428
private array $uncheckedExceptionRegexes,
29+
#[AutowiredParameter(ref: '%exceptions.uncheckedExceptionClasses%')]
2530
private array $uncheckedExceptionClasses,
31+
#[AutowiredParameter(ref: '%exceptions.checkedExceptionRegexes%')]
2632
private array $checkedExceptionRegexes,
33+
#[AutowiredParameter(ref: '%exceptions.checkedExceptionClasses%')]
2734
private array $checkedExceptionClasses,
2835
)
2936
{

src/Rules/LazyRegistry.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
namespace PHPStan\Rules;
44

55
use PhpParser\Node;
6+
use PHPStan\DependencyInjection\AutowiredService;
67
use PHPStan\DependencyInjection\Container;
78
use function class_implements;
89
use function class_parents;
910

11+
#[AutowiredService(name: 'registry', as: Registry::class)]
1012
final class LazyRegistry implements Registry
1113
{
1214

0 commit comments

Comments
 (0)