diff --git a/conf/config.neon b/conf/config.neon index 5b98342605..89d9b5a5b2 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -628,11 +628,6 @@ services: arguments: exceptionTypeResolver: @exceptionTypeResolver - - - class: PHPStan\Rules\Exceptions\TooWideThrowTypeCheck - arguments: - implicitThrows: %exceptions.implicitThrows% - - class: PHPStan\Rules\FunctionCallParametersCheck arguments: @@ -641,12 +636,6 @@ services: checkExtraArguments: %checkExtraArguments% checkMissingTypehints: %checkMissingTypehints% - - - class: PHPStan\Rules\FunctionDefinitionCheck - arguments: - checkClassCaseSensitivity: %checkClassCaseSensitivity% - checkThisOnly: %checkThisOnly% - - class: PHPStan\Rules\Generics\GenericAncestorsCheck arguments: @@ -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: diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 2f815c0233..a90aa2621c 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -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 diff --git a/src/Command/ErrorFormatter/TableErrorFormatter.php b/src/Command/ErrorFormatter/TableErrorFormatter.php index b195e03a34..0c101407d4 100644 --- a/src/Command/ErrorFormatter/TableErrorFormatter.php +++ b/src/Command/ErrorFormatter/TableErrorFormatter.php @@ -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; @@ -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, ) { diff --git a/src/DependencyInjection/AutowiredAttributeServicesExtension.php b/src/DependencyInjection/AutowiredAttributeServicesExtension.php index b09bb8b112..afed36c04f 100644 --- a/src/DependencyInjection/AutowiredAttributeServicesExtension.php +++ b/src/DependencyInjection/AutowiredAttributeServicesExtension.php @@ -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 { @@ -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; @@ -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; diff --git a/src/DependencyInjection/AutowiredParameter.php b/src/DependencyInjection/AutowiredParameter.php new file mode 100644 index 0000000000..1f3853d3d4 --- /dev/null +++ b/src/DependencyInjection/AutowiredParameter.php @@ -0,0 +1,23 @@ +