Skip to content

AutowiredParameter attribute #4042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -628,11 +628,6 @@ services:
arguments:
exceptionTypeResolver: @exceptionTypeResolver

-
class: PHPStan\Rules\Exceptions\TooWideThrowTypeCheck
arguments:
implicitThrows: %exceptions.implicitThrows%

-
class: PHPStan\Rules\FunctionCallParametersCheck
arguments:
Expand All @@ -641,12 +636,6 @@ services:
checkExtraArguments: %checkExtraArguments%
checkMissingTypehints: %checkMissingTypehints%

-
class: PHPStan\Rules\FunctionDefinitionCheck
arguments:
checkClassCaseSensitivity: %checkClassCaseSensitivity%
checkThisOnly: %checkThisOnly%

-
class: PHPStan\Rules\Generics\GenericAncestorsCheck
arguments:
Expand Down Expand Up @@ -980,14 +969,6 @@ services:
autowired:
- PHPStan\Command\ErrorFormatter\CiDetectedErrorFormatter

errorFormatter.table:
class: PHPStan\Command\ErrorFormatter\TableErrorFormatter
arguments:
simpleRelativePathHelper: @simpleRelativePathHelper
showTipsOfTheDay: %tipsOfTheDay%
editorUrl: %editorUrl%
editorUrlTitle: %editorUrlTitle%

errorFormatter.checkstyle:
class: PHPStan\Command\ErrorFormatter\CheckstyleErrorFormatter
arguments:
Expand Down
12 changes: 12 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ parameters:
count: 1
path: src/Command/ErrorsConsoleStyle.php

-
message: '#^Call to static method escape\(\) of internal class Nette\\DI\\Helpers from outside its root namespace Nette\.$#'
identifier: staticMethod.internalClass
count: 1
path: src/DependencyInjection/AutowiredAttributeServicesExtension.php

-
message: '#^Call to static method expand\(\) of internal class Nette\\DI\\Helpers from outside its root namespace Nette\.$#'
identifier: staticMethod.internalClass
count: 2
path: src/DependencyInjection/AutowiredAttributeServicesExtension.php

-
message: '#^Call to static method expand\(\) of internal class Nette\\DI\\Helpers from outside its root namespace Nette\.$#'
identifier: staticMethod.internalClass
Expand Down
7 changes: 7 additions & 0 deletions src/Command/ErrorFormatter/TableErrorFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use PHPStan\Command\AnalyseCommand;
use PHPStan\Command\AnalysisResult;
use PHPStan\Command\Output;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\File\RelativePathHelper;
use PHPStan\File\SimpleRelativePathHelper;
use Symfony\Component\Console\Formatter\OutputFormatter;
Expand All @@ -21,15 +23,20 @@
use function str_contains;
use function str_replace;

#[AutowiredService(name: 'errorFormatter.table')]
final class TableErrorFormatter implements ErrorFormatter
{

public function __construct(
private RelativePathHelper $relativePathHelper,
#[AutowiredParameter(ref: '@simpleRelativePathHelper')]
private SimpleRelativePathHelper $simpleRelativePathHelper,
private CiDetectedErrorFormatter $ciDetectedErrorFormatter,
#[AutowiredParameter(ref: '%tipsOfTheDay%')]
private bool $showTipsOfTheDay,
#[AutowiredParameter]
private ?string $editorUrl,
#[AutowiredParameter]
private ?string $editorUrlTitle,
)
{
Expand Down
31 changes: 31 additions & 0 deletions src/DependencyInjection/AutowiredAttributeServicesExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
namespace PHPStan\DependencyInjection;

use Nette\DI\CompilerExtension;
use Nette\DI\Definitions\Reference;
use Nette\DI\Helpers;
use Nette\Utils\Strings;
use olvlvl\ComposerAttributeCollector\Attributes;
use ReflectionClass;
use function strtolower;
use function substr;

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

$autowiredParameters = Attributes::findTargetMethodParameters(AutowiredParameter::class);

foreach ($autowiredServiceClasses as $class) {
$reflection = new ReflectionClass($class->name);
$attribute = $class->attribute;
Expand All @@ -23,6 +30,30 @@ public function loadConfiguration(): void
->setType($class->name)
->setAutowired();

foreach ($autowiredParameters as $autowiredParameter) {
if (strtolower($autowiredParameter->method) !== '__construct') {
continue;
}
if (strtolower($autowiredParameter->class) !== strtolower($class->name)) {
continue;
}
$ref = $autowiredParameter->attribute->ref;
if ($ref === null) {
$argument = Helpers::expand(
'%' . Helpers::escape($autowiredParameter->name) . '%',
$builder->parameters,
);
} elseif (Strings::match($ref, '#^@[\w\\\\]+$#D') !== null) {
$argument = new Reference(substr($ref, 1));
} else {
$argument = Helpers::expand(
$ref,
$builder->parameters,
);
}
$definition->setArgument($autowiredParameter->name, $argument);
}

foreach (ValidateServiceTagsExtension::INTERFACE_TAG_MAPPING as $interface => $tag) {
if (!$reflection->implementsInterface($interface)) {
continue;
Expand Down
23 changes: 23 additions & 0 deletions src/DependencyInjection/AutowiredParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types = 1);

namespace PHPStan\DependencyInjection;

use Attribute;

/**
* Autowires constructor parameters in service classes using #[AutowiredService] attribute.
*
* If ref is omitted, it looks for parameter of the same name.
*
* Works thanks to https://github.com/ondrejmirtes/composer-attribute-collector
* and AutowiredAttributeServicesExtension.
*/
#[Attribute(flags: Attribute::TARGET_PARAMETER)]
final class AutowiredParameter
{

public function __construct(public ?string $ref = null)
{
}

}
8 changes: 7 additions & 1 deletion src/Rules/Exceptions/TooWideThrowTypeCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@
namespace PHPStan\Rules\Exceptions;

use PHPStan\Analyser\ThrowPoint;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\VerbosityLevel;
use function array_map;

#[AutowiredService]
final class TooWideThrowTypeCheck
{

public function __construct(private bool $implicitThrows)
public function __construct(
#[AutowiredParameter(ref: '%exceptions.implicitThrows%')]
private bool $implicitThrows,
)
{
}

Expand Down
5 changes: 5 additions & 0 deletions src/Rules/FunctionDefinitionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\UnionType;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Node\Printer\NodeTypePrinter;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\ExtendedParameterReflection;
Expand Down Expand Up @@ -44,6 +46,7 @@
use function sprintf;
use function strtolower;

#[AutowiredService]
final class FunctionDefinitionCheck
{

Expand All @@ -52,7 +55,9 @@ public function __construct(
private ClassNameCheck $classCheck,
private UnresolvableTypeHelper $unresolvableTypeHelper,
private PhpVersion $phpVersion,
#[AutowiredParameter]
private bool $checkClassCaseSensitivity,
#[AutowiredParameter]
private bool $checkThisOnly,
)
{
Expand Down
Loading