Skip to content

Commit cb7ee52

Browse files
committed
AutowiredParameter attribute
1 parent 28a5e21 commit cb7ee52

File tree

7 files changed

+85
-20
lines changed

7 files changed

+85
-20
lines changed

conf/config.neon

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -628,11 +628,6 @@ services:
628628
arguments:
629629
exceptionTypeResolver: @exceptionTypeResolver
630630

631-
-
632-
class: PHPStan\Rules\Exceptions\TooWideThrowTypeCheck
633-
arguments:
634-
implicitThrows: %exceptions.implicitThrows%
635-
636631
-
637632
class: PHPStan\Rules\FunctionCallParametersCheck
638633
arguments:
@@ -641,12 +636,6 @@ services:
641636
checkExtraArguments: %checkExtraArguments%
642637
checkMissingTypehints: %checkMissingTypehints%
643638

644-
-
645-
class: PHPStan\Rules\FunctionDefinitionCheck
646-
arguments:
647-
checkClassCaseSensitivity: %checkClassCaseSensitivity%
648-
checkThisOnly: %checkThisOnly%
649-
650639
-
651640
class: PHPStan\Rules\Generics\GenericAncestorsCheck
652641
arguments:
@@ -980,14 +969,6 @@ services:
980969
autowired:
981970
- PHPStan\Command\ErrorFormatter\CiDetectedErrorFormatter
982971

983-
errorFormatter.table:
984-
class: PHPStan\Command\ErrorFormatter\TableErrorFormatter
985-
arguments:
986-
simpleRelativePathHelper: @simpleRelativePathHelper
987-
showTipsOfTheDay: %tipsOfTheDay%
988-
editorUrl: %editorUrl%
989-
editorUrlTitle: %editorUrlTitle%
990-
991972
errorFormatter.checkstyle:
992973
class: PHPStan\Command\ErrorFormatter\CheckstyleErrorFormatter
993974
arguments:

phpstan-baseline.neon

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,18 @@ parameters:
174174
count: 1
175175
path: src/Command/ErrorsConsoleStyle.php
176176

177+
-
178+
message: '#^Call to static method escape\(\) of internal class Nette\\DI\\Helpers from outside its root namespace Nette\.$#'
179+
identifier: staticMethod.internalClass
180+
count: 1
181+
path: src/DependencyInjection/AutowiredAttributeServicesExtension.php
182+
183+
-
184+
message: '#^Call to static method expand\(\) of internal class Nette\\DI\\Helpers from outside its root namespace Nette\.$#'
185+
identifier: staticMethod.internalClass
186+
count: 2
187+
path: src/DependencyInjection/AutowiredAttributeServicesExtension.php
188+
177189
-
178190
message: '#^Call to static method expand\(\) of internal class Nette\\DI\\Helpers from outside its root namespace Nette\.$#'
179191
identifier: staticMethod.internalClass

src/Command/ErrorFormatter/TableErrorFormatter.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use PHPStan\Command\AnalyseCommand;
77
use PHPStan\Command\AnalysisResult;
88
use PHPStan\Command\Output;
9+
use PHPStan\DependencyInjection\AutowiredParameter;
10+
use PHPStan\DependencyInjection\AutowiredService;
911
use PHPStan\File\RelativePathHelper;
1012
use PHPStan\File\SimpleRelativePathHelper;
1113
use Symfony\Component\Console\Formatter\OutputFormatter;
@@ -21,15 +23,20 @@
2123
use function str_contains;
2224
use function str_replace;
2325

26+
#[AutowiredService(name: 'errorFormatter.table')]
2427
final class TableErrorFormatter implements ErrorFormatter
2528
{
2629

2730
public function __construct(
2831
private RelativePathHelper $relativePathHelper,
32+
#[AutowiredParameter(ref: '@simpleRelativePathHelper')]
2933
private SimpleRelativePathHelper $simpleRelativePathHelper,
3034
private CiDetectedErrorFormatter $ciDetectedErrorFormatter,
35+
#[AutowiredParameter(ref: '%tipsOfTheDay%')]
3136
private bool $showTipsOfTheDay,
37+
#[AutowiredParameter]
3238
private ?string $editorUrl,
39+
#[AutowiredParameter]
3340
private ?string $editorUrlTitle,
3441
)
3542
{

src/DependencyInjection/AutowiredAttributeServicesExtension.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
namespace PHPStan\DependencyInjection;
44

55
use Nette\DI\CompilerExtension;
6+
use Nette\DI\Definitions\Reference;
7+
use Nette\DI\Helpers;
8+
use Nette\Utils\Strings;
69
use olvlvl\ComposerAttributeCollector\Attributes;
710
use ReflectionClass;
11+
use function strtolower;
12+
use function substr;
813

914
final class AutowiredAttributeServicesExtension extends CompilerExtension
1015
{
@@ -15,6 +20,8 @@ public function loadConfiguration(): void
1520
$autowiredServiceClasses = Attributes::findTargetClasses(AutowiredService::class);
1621
$builder = $this->getContainerBuilder();
1722

23+
$autowiredParameters = Attributes::findTargetMethodParameters(AutowiredParameter::class);
24+
1825
foreach ($autowiredServiceClasses as $class) {
1926
$reflection = new ReflectionClass($class->name);
2027
$attribute = $class->attribute;
@@ -23,6 +30,30 @@ public function loadConfiguration(): void
2330
->setType($class->name)
2431
->setAutowired();
2532

33+
foreach ($autowiredParameters as $autowiredParameter) {
34+
if (strtolower($autowiredParameter->method) !== '__construct') {
35+
continue;
36+
}
37+
if (strtolower($autowiredParameter->class) !== strtolower($class->name)) {
38+
continue;
39+
}
40+
$ref = $autowiredParameter->attribute->ref;
41+
if ($ref === null) {
42+
$argument = Helpers::expand(
43+
'%' . Helpers::escape($autowiredParameter->name) . '%',
44+
$builder->parameters,
45+
);
46+
} elseif (Strings::match($ref, '#^@[\w\\\\]+$#D') !== null) {
47+
$argument = new Reference(substr($ref, 1));
48+
} else {
49+
$argument = Helpers::expand(
50+
$ref,
51+
$builder->parameters,
52+
);
53+
}
54+
$definition->setArgument($autowiredParameter->name, $argument);
55+
}
56+
2657
foreach (ValidateServiceTagsExtension::INTERFACE_TAG_MAPPING as $interface => $tag) {
2758
if (!$reflection->implementsInterface($interface)) {
2859
continue;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\DependencyInjection;
4+
5+
use Attribute;
6+
7+
/**
8+
* Autowires constructor parameters in service classes using #[AutowiredService] attribute.
9+
*
10+
* If ref is omitted, it looks for parameter of the same name.
11+
*
12+
* Works thanks to https://github.com/ondrejmirtes/composer-attribute-collector
13+
* and AutowiredAttributeServicesExtension.
14+
*/
15+
#[Attribute(flags: Attribute::TARGET_PARAMETER)]
16+
final class AutowiredParameter
17+
{
18+
19+
public function __construct(public ?string $ref = null)
20+
{
21+
}
22+
23+
}

src/Rules/Exceptions/TooWideThrowTypeCheck.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@
33
namespace PHPStan\Rules\Exceptions;
44

55
use PHPStan\Analyser\ThrowPoint;
6+
use PHPStan\DependencyInjection\AutowiredParameter;
7+
use PHPStan\DependencyInjection\AutowiredService;
68
use PHPStan\Type\NeverType;
79
use PHPStan\Type\Type;
810
use PHPStan\Type\TypeCombinator;
911
use PHPStan\Type\TypeUtils;
1012
use PHPStan\Type\VerbosityLevel;
1113
use function array_map;
1214

15+
#[AutowiredService]
1316
final class TooWideThrowTypeCheck
1417
{
1518

16-
public function __construct(private bool $implicitThrows)
19+
public function __construct(
20+
#[AutowiredParameter(ref: '%exceptions.implicitThrows%')]
21+
private bool $implicitThrows,
22+
)
1723
{
1824
}
1925

src/Rules/FunctionDefinitionCheck.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use PhpParser\Node\Stmt\Function_;
1717
use PhpParser\Node\UnionType;
1818
use PHPStan\Analyser\Scope;
19+
use PHPStan\DependencyInjection\AutowiredParameter;
20+
use PHPStan\DependencyInjection\AutowiredService;
1921
use PHPStan\Node\Printer\NodeTypePrinter;
2022
use PHPStan\Php\PhpVersion;
2123
use PHPStan\Reflection\ExtendedParameterReflection;
@@ -44,6 +46,7 @@
4446
use function sprintf;
4547
use function strtolower;
4648

49+
#[AutowiredService]
4750
final class FunctionDefinitionCheck
4851
{
4952

@@ -52,7 +55,9 @@ public function __construct(
5255
private ClassNameCheck $classCheck,
5356
private UnresolvableTypeHelper $unresolvableTypeHelper,
5457
private PhpVersion $phpVersion,
58+
#[AutowiredParameter]
5559
private bool $checkClassCaseSensitivity,
60+
#[AutowiredParameter]
5661
private bool $checkThisOnly,
5762
)
5863
{

0 commit comments

Comments
 (0)