Skip to content

Commit 7376d54

Browse files
committed
#[GenerateFactory] attribute to substitute implement neon key
1 parent 824b636 commit 7376d54

File tree

10 files changed

+71
-37
lines changed

10 files changed

+71
-37
lines changed

conf/config.neon

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -243,39 +243,3 @@ services:
243243

244244
-
245245
class: PHPStan\Rules\Properties\UninitializedPropertyRule
246-
247-
-
248-
implement: PHPStan\Analyser\ResultCache\ResultCacheManagerFactory
249-
arguments:
250-
scanFileFinder: @fileFinderScan
251-
cacheFilePath: %resultCachePath%
252-
analysedPaths: %analysedPaths%
253-
analysedPathsFromConfig: %analysedPathsFromConfig%
254-
composerAutoloaderProjectPaths: %composerAutoloaderProjectPaths%
255-
usedLevel: %usedLevel%
256-
cliAutoloadFile: %cliAutoloadFile%
257-
bootstrapFiles: %bootstrapFiles%
258-
scanFiles: %scanFiles%
259-
scanDirectories: %scanDirectories%
260-
checkDependenciesOfProjectExtensionFiles: %resultCacheChecksProjectExtensionFilesDependencies%
261-
parametersNotInvalidatingCache: %parametersNotInvalidatingCache%
262-
skipResultCacheIfOlderThanDays: %resultCacheSkipIfOlderThanDays%
263-
264-
-
265-
implement: PHPStan\File\FileExcluderRawFactory
266-
267-
-
268-
implement: PHPStan\Reflection\FunctionReflectionFactory
269-
arguments:
270-
parser: @defaultAnalysisParser
271-
272-
-
273-
implement: PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedPsrAutoloaderLocatorFactory
274-
275-
-
276-
implement: PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedSingleFileSourceLocatorFactory
277-
278-
-
279-
implement: PHPStan\Reflection\Php\PhpMethodReflectionFactory
280-
arguments:
281-
parser: @defaultAnalysisParser

src/Analyser/ResultCache/ResultCacheManager.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
use PHPStan\Dependency\ExportedNode\ExportedTraitNode;
1212
use PHPStan\Dependency\ExportedNodeFetcher;
1313
use PHPStan\Dependency\RootExportedNode;
14+
use PHPStan\DependencyInjection\AutowiredParameter;
1415
use PHPStan\DependencyInjection\Container;
16+
use PHPStan\DependencyInjection\GenerateFactory;
1517
use PHPStan\DependencyInjection\ProjectConfigHelper;
1618
use PHPStan\File\CouldNotReadFileException;
1719
use PHPStan\File\FileFinder;
@@ -53,6 +55,7 @@
5355
* @phpstan-import-type LinesToIgnore from FileAnalyserResult
5456
* @phpstan-import-type CollectorData from CollectedData
5557
*/
58+
#[GenerateFactory(interface: ResultCacheManagerFactory::class)]
5659
final class ResultCacheManager
5760
{
5861

@@ -77,22 +80,35 @@ final class ResultCacheManager
7780
public function __construct(
7881
private Container $container,
7982
private ExportedNodeFetcher $exportedNodeFetcher,
83+
#[AutowiredParameter(ref: '@fileFinderScan')]
8084
private FileFinder $scanFileFinder,
8185
private ReflectionProvider $reflectionProvider,
8286
private StubFilesProvider $stubFilesProvider,
8387
private FileHelper $fileHelper,
88+
#[AutowiredParameter(ref: '%resultCachePath%')]
8489
private string $cacheFilePath,
90+
#[AutowiredParameter]
8591
private array $analysedPaths,
92+
#[AutowiredParameter]
8693
private array $analysedPathsFromConfig,
94+
#[AutowiredParameter]
8795
private array $composerAutoloaderProjectPaths,
96+
#[AutowiredParameter]
8897
private string $usedLevel,
98+
#[AutowiredParameter]
8999
private ?string $cliAutoloadFile,
100+
#[AutowiredParameter]
90101
private array $bootstrapFiles,
102+
#[AutowiredParameter]
91103
private array $scanFiles,
104+
#[AutowiredParameter]
92105
private array $scanDirectories,
93106
private array $fileReplacements,
107+
#[AutowiredParameter(ref: '%resultCacheChecksProjectExtensionFilesDependencies%')]
94108
private bool $checkDependenciesOfProjectExtensionFiles,
109+
#[AutowiredParameter]
95110
private array $parametersNotInvalidatingCache,
111+
#[AutowiredParameter(ref: '%resultCacheSkipIfOlderThanDays%')]
96112
private int $skipResultCacheIfOlderThanDays,
97113
)
98114
{

src/DependencyInjection/AutowiredAttributeServicesExtension.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ public function loadConfiguration(): void
6161
}
6262
}
6363

64+
foreach (Attributes::findTargetClasses(GenerateFactory::class) as $class) {
65+
$attribute = $class->attribute;
66+
$definition = $builder->addFactoryDefinition(null)
67+
->setImplement($attribute->interface);
68+
69+
$resultDefinition = $definition->getResultDefinition();
70+
$this->processParameters($class->name, $resultDefinition, $autowiredParameters);
71+
}
72+
6473
/** @var stdClass&object{level: int|null} $config */
6574
$config = $this->getConfig();
6675
if ($config->level === null) {

src/DependencyInjection/AutowiredParameter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
/**
88
* Autowires constructor parameters in service classes using #[AutowiredService],
9-
* #[RegisteredRule] or #[RegisteredCollector] attributes.
9+
* #[GeneratedFactory], #[RegisteredRule] or #[RegisteredCollector] attributes.
1010
*
1111
* If ref is omitted, it looks for parameter of the same name.
1212
*
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\DependencyInjection;
4+
5+
use Attribute;
6+
7+
/**
8+
* Generates concrete factory implementation based on the passed interface name.
9+
*
10+
* This looks at and compares constructor of the class with the attribute
11+
* against the "create" method of the passed interface.
12+
*
13+
* It subtracts "create" parameters from the constructor parameters.
14+
* And the rest is autowired from the dependency injection container.
15+
*
16+
* Works thanks to https://github.com/ondrejmirtes/composer-attribute-collector
17+
* and AutowiredAttributeServicesExtension.
18+
*/
19+
#[Attribute(flags: Attribute::TARGET_CLASS)]
20+
final class GenerateFactory
21+
{
22+
23+
/**
24+
* @param class-string $interface
25+
*/
26+
public function __construct(public string $interface)
27+
{
28+
}
29+
30+
}

src/File/FileExcluder.php

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

33
namespace PHPStan\File;
44

5+
use PHPStan\DependencyInjection\GenerateFactory;
56
use function fnmatch;
67
use function in_array;
78
use function is_dir;
@@ -14,6 +15,7 @@
1415
use const FNM_CASEFOLD;
1516
use const FNM_NOESCAPE;
1617

18+
#[GenerateFactory(interface: FileExcluderRawFactory::class)]
1719
final class FileExcluder
1820
{
1921

src/Reflection/BetterReflection/SourceLocator/OptimizedPsrAutoloaderLocator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
use PHPStan\BetterReflection\Reflector\Reflector;
99
use PHPStan\BetterReflection\SourceLocator\Type\Composer\Psr\PsrAutoloaderMapping;
1010
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
11+
use PHPStan\DependencyInjection\GenerateFactory;
1112
use function is_file;
1213

14+
#[GenerateFactory(interface: OptimizedPsrAutoloaderLocatorFactory::class)]
1315
final class OptimizedPsrAutoloaderLocator implements SourceLocator
1416
{
1517

src/Reflection/BetterReflection/SourceLocator/OptimizedSingleFileSourceLocator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
use PHPStan\BetterReflection\Reflector\Reflector;
1313
use PHPStan\BetterReflection\SourceLocator\Ast\Strategy\NodeToReflection;
1414
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
15+
use PHPStan\DependencyInjection\GenerateFactory;
1516
use PHPStan\Reflection\ConstantNameHelper;
1617
use PHPStan\ShouldNotHappenException;
1718
use function array_key_exists;
1819
use function array_keys;
1920
use function strtolower;
2021

22+
#[GenerateFactory(interface: OptimizedSingleFileSourceLocatorFactory::class)]
2123
final class OptimizedSingleFileSourceLocator implements SourceLocator
2224
{
2325

src/Reflection/Php/PhpFunctionReflection.php

Lines changed: 5 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\ReflectionParameter;
7+
use PHPStan\DependencyInjection\AutowiredParameter;
8+
use PHPStan\DependencyInjection\GenerateFactory;
79
use PHPStan\Internal\DeprecatedAttributeHelper;
810
use PHPStan\Parser\Parser;
911
use PHPStan\Parser\VariadicFunctionsVisitor;
@@ -14,6 +16,7 @@
1416
use PHPStan\Reflection\ExtendedParameterReflection;
1517
use PHPStan\Reflection\ExtendedParametersAcceptor;
1618
use PHPStan\Reflection\FunctionReflection;
19+
use PHPStan\Reflection\FunctionReflectionFactory;
1720
use PHPStan\Reflection\InitializerExprContext;
1821
use PHPStan\Reflection\InitializerExprTypeResolver;
1922
use PHPStan\TrinaryLogic;
@@ -27,6 +30,7 @@
2730
use function is_array;
2831
use function is_file;
2932

33+
#[GenerateFactory(interface: FunctionReflectionFactory::class)]
3034
final class PhpFunctionReflection implements FunctionReflection
3135
{
3236

@@ -45,6 +49,7 @@ final class PhpFunctionReflection implements FunctionReflection
4549
public function __construct(
4650
private InitializerExprTypeResolver $initializerExprTypeResolver,
4751
private ReflectionFunction $reflection,
52+
#[AutowiredParameter(ref: '@defaultAnalysisParser')]
4853
private Parser $parser,
4954
private AttributeReflectionFactory $attributeReflectionFactory,
5055
private TemplateTypeMap $templateTypeMap,

src/Reflection/Php/PhpMethodReflection.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\ReflectionMethod;
66
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionParameter;
7+
use PHPStan\DependencyInjection\AutowiredParameter;
8+
use PHPStan\DependencyInjection\GenerateFactory;
79
use PHPStan\Internal\DeprecatedAttributeHelper;
810
use PHPStan\Parser\Parser;
911
use PHPStan\Parser\VariadicMethodsVisitor;
@@ -46,6 +48,7 @@
4648
/**
4749
* @api
4850
*/
51+
#[GenerateFactory(interface: PhpMethodReflectionFactory::class)]
4952
final class PhpMethodReflection implements ExtendedMethodReflection
5053
{
5154

@@ -75,6 +78,7 @@ public function __construct(
7578
private ReflectionMethod $reflection,
7679
private ReflectionProvider $reflectionProvider,
7780
private AttributeReflectionFactory $attributeReflectionFactory,
81+
#[AutowiredParameter(ref: '@defaultAnalysisParser')]
7882
private Parser $parser,
7983
private TemplateTypeMap $templateTypeMap,
8084
private array $phpDocParameterTypes,

0 commit comments

Comments
 (0)