diff --git a/packages/PhpDocParser/PhpDocParser/ValueObject/Ast/PhpDoc/SimplePhpDocNode.php b/packages/PhpDocParser/PhpDocParser/ValueObject/Ast/PhpDoc/SimplePhpDocNode.php deleted file mode 100644 index e1879d97de4..00000000000 --- a/packages/PhpDocParser/PhpDocParser/ValueObject/Ast/PhpDoc/SimplePhpDocNode.php +++ /dev/null @@ -1,40 +0,0 @@ -getParamTagValues() as $paramTagValueNode) { - if ($paramTagValueNode->parameterName !== $desiredParamNameWithDollar) { - continue; - } - - return $paramTagValueNode; - } - - return null; - } - - public function getParamType(string $desiredParamName): ?TypeNode - { - $paramTagValueNode = $this->getParam($desiredParamName); - if (! $paramTagValueNode instanceof ParamTagValueNode) { - return null; - } - - return $paramTagValueNode->type; - } -} diff --git a/rules/Naming/Contract/Guard/ConflictingNameGuardInterface.php b/rules/Naming/Contract/Guard/ConflictingNameGuardInterface.php deleted file mode 100644 index 52aa4b39efe..00000000000 --- a/rules/Naming/Contract/Guard/ConflictingNameGuardInterface.php +++ /dev/null @@ -1,18 +0,0 @@ - - */ -final class DateTimeAtNamingConventionGuard implements ConflictingNameGuardInterface +final class DateTimeAtNamingConventionGuard { public function __construct( private readonly NodeTypeResolver $nodeTypeResolver, @@ -24,15 +19,7 @@ public function __construct( ) { } - /** - * @param PropertyRename $renameValueObject - */ - public function isConflicting(RenameValueObjectInterface $renameValueObject): bool - { - return $this->isDateTimeAtNamingConvention($renameValueObject); - } - - private function isDateTimeAtNamingConvention(PropertyRename $propertyRename): bool + public function isConflicting(PropertyRename $propertyRename): bool { $type = $this->nodeTypeResolver->getType($propertyRename->getProperty()); $type = $this->typeUnwrapper->unwrapFirstObjectTypeFromUnionType($type); diff --git a/rules/Naming/Guard/HasMagicGetSetGuard.php b/rules/Naming/Guard/HasMagicGetSetGuard.php index ac4271822af..5bba018d68f 100644 --- a/rules/Naming/Guard/HasMagicGetSetGuard.php +++ b/rules/Naming/Guard/HasMagicGetSetGuard.php @@ -5,30 +5,22 @@ namespace Rector\Naming\Guard; use PHPStan\Reflection\ReflectionProvider; -use Rector\Naming\Contract\Guard\ConflictingNameGuardInterface; -use Rector\Naming\Contract\RenameValueObjectInterface; use Rector\Naming\ValueObject\PropertyRename; -/** - * @implements ConflictingNameGuardInterface - */ -final class HasMagicGetSetGuard implements ConflictingNameGuardInterface +final class HasMagicGetSetGuard { public function __construct( private readonly ReflectionProvider $reflectionProvider ) { } - /** - * @param PropertyRename $renameValueObject - */ - public function isConflicting(RenameValueObjectInterface $renameValueObject): bool + public function isConflicting(PropertyRename $propertyRename): bool { - if (! $this->reflectionProvider->hasClass($renameValueObject->getClassLikeName())) { + if (! $this->reflectionProvider->hasClass($propertyRename->getClassLikeName())) { return false; } - $classReflection = $this->reflectionProvider->getClass($renameValueObject->getClassLikeName()); + $classReflection = $this->reflectionProvider->getClass($propertyRename->getClassLikeName()); if ($classReflection->hasMethod('__set')) { return true; } diff --git a/rules/Naming/Guard/NotPrivatePropertyGuard.php b/rules/Naming/Guard/NotPrivatePropertyGuard.php deleted file mode 100644 index 05cb97333c4..00000000000 --- a/rules/Naming/Guard/NotPrivatePropertyGuard.php +++ /dev/null @@ -1,23 +0,0 @@ - - */ -final class NotPrivatePropertyGuard implements ConflictingNameGuardInterface -{ - /** - * @param PropertyRename $renameValueObject - */ - public function isConflicting(RenameValueObjectInterface $renameValueObject): bool - { - return ! $renameValueObject->isPrivateProperty(); - } -} diff --git a/rules/Naming/Guard/RamseyUuidInterfaceGuard.php b/rules/Naming/Guard/RamseyUuidInterfaceGuard.php deleted file mode 100644 index fe6f686d7d9..00000000000 --- a/rules/Naming/Guard/RamseyUuidInterfaceGuard.php +++ /dev/null @@ -1,33 +0,0 @@ - - */ -final class RamseyUuidInterfaceGuard implements ConflictingNameGuardInterface -{ - public function __construct( - private readonly NodeTypeResolver $nodeTypeResolver - ) { - } - - /** - * @param PropertyRename $renameValueObject - */ - public function isConflicting(RenameValueObjectInterface $renameValueObject): bool - { - return $this->nodeTypeResolver->isObjectType( - $renameValueObject->getProperty(), - new ObjectType('Ramsey\Uuid\UuidInterface') - ); - } -} diff --git a/rules/Naming/RenameGuard/PropertyRenameGuard.php b/rules/Naming/RenameGuard/PropertyRenameGuard.php index 33c18393426..c1976cdd29c 100644 --- a/rules/Naming/RenameGuard/PropertyRenameGuard.php +++ b/rules/Naming/RenameGuard/PropertyRenameGuard.php @@ -4,27 +4,38 @@ namespace Rector\Naming\RenameGuard; -use Rector\Naming\Contract\Guard\ConflictingNameGuardInterface; +use PHPStan\Type\ObjectType; +use Rector\Naming\Guard\DateTimeAtNamingConventionGuard; +use Rector\Naming\Guard\HasMagicGetSetGuard; use Rector\Naming\ValueObject\PropertyRename; +use Rector\NodeTypeResolver\NodeTypeResolver; final class PropertyRenameGuard { - /** - * @param ConflictingNameGuardInterface[] $conflictingNameGuards - */ public function __construct( - private readonly array $conflictingNameGuards + private readonly NodeTypeResolver $nodeTypeResolver, + private readonly DateTimeAtNamingConventionGuard $dateTimeAtNamingConventionGuard, + private readonly HasMagicGetSetGuard $hasMagicGetSetGuard, ) { } public function shouldSkip(PropertyRename $propertyRename): bool { - foreach ($this->conflictingNameGuards as $conflictingNameGuard) { - if ($conflictingNameGuard->isConflicting($propertyRename)) { - return true; - } + if (! $propertyRename->isPrivateProperty()) { + return true; } - return false; + if ($this->nodeTypeResolver->isObjectType( + $propertyRename->getProperty(), + new ObjectType('Ramsey\Uuid\UuidInterface') + )) { + return true; + } + + if ($this->dateTimeAtNamingConventionGuard->isConflicting($propertyRename)) { + return true; + } + + return $this->hasMagicGetSetGuard->isConflicting($propertyRename); } } diff --git a/src/DependencyInjection/CompilerPass/AutowireArrayParameterCompilerPass.php b/src/DependencyInjection/CompilerPass/AutowireArrayParameterCompilerPass.php index 9fdbfeb1223..6720c9a899e 100644 --- a/src/DependencyInjection/CompilerPass/AutowireArrayParameterCompilerPass.php +++ b/src/DependencyInjection/CompilerPass/AutowireArrayParameterCompilerPass.php @@ -4,14 +4,12 @@ namespace Rector\Core\DependencyInjection\CompilerPass; -use Nette\Utils\Strings; use Rector\Core\DependencyInjection\DefinitionFinder; use Rector\Core\DependencyInjection\DocBlock\ParamTypeDocBlockResolver; use Rector\Core\DependencyInjection\Skipper\ParameterSkipper; use Rector\Core\DependencyInjection\TypeResolver\ParameterTypeResolver; use ReflectionClass; use ReflectionMethod; -use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; @@ -25,47 +23,20 @@ */ final class AutowireArrayParameterCompilerPass implements CompilerPassInterface { - /** - * These namespaces are already configured by their bundles/extensions. - * - * @var string[] - */ - private const EXCLUDED_NAMESPACES = ['Doctrine', 'JMS', 'Symfony', 'Sensio', 'Knp', 'EasyCorp', 'Sonata', 'Twig']; - - /** - * Classes that create circular dependencies - * - * @var class-string[]|string[] - */ - private const EXCLUDED_FATAL_CLASSES = [ - 'Symfony\Component\Form\FormExtensionInterface', - 'Symfony\Component\Asset\PackageInterface', - 'Symfony\Component\Config\Loader\LoaderInterface', - 'Symfony\Component\VarDumper\Dumper\ContextProvider\ContextProviderInterface', - 'EasyCorp\Bundle\EasyAdminBundle\Form\Type\Configurator\TypeConfiguratorInterface', - 'Sonata\CoreBundle\Model\Adapter\AdapterInterface', - 'Sonata\Doctrine\Adapter\AdapterChain', - 'Sonata\Twig\Extension\TemplateExtension', - 'Symfony\Component\HttpKernel\KernelInterface', - ]; - private readonly DefinitionFinder $definitionFinder; private readonly ParameterTypeResolver $parameterTypeResolver; private readonly ParameterSkipper $parameterSkipper; - /** - * @param string[] $excludedFatalClasses - */ - public function __construct(array $excludedFatalClasses = []) + public function __construct() { $this->definitionFinder = new DefinitionFinder(); $paramTypeDocBlockResolver = new ParamTypeDocBlockResolver(); $this->parameterTypeResolver = new ParameterTypeResolver($paramTypeDocBlockResolver); - $this->parameterSkipper = new ParameterSkipper($this->parameterTypeResolver, $excludedFatalClasses); + $this->parameterSkipper = new ParameterSkipper($this->parameterTypeResolver); } public function process(ContainerBuilder $containerBuilder): void @@ -97,22 +68,6 @@ private function shouldSkipDefinition(ContainerBuilder $containerBuilder, Defini return true; } - // here class name can be "%parameter.class%" - $parameterBag = $containerBuilder->getParameterBag(); - $resolvedClassName = $parameterBag->resolveValue($definition->getClass()); - - // skip 3rd party classes, they're autowired by own config - $excludedNamespacePattern = '#^(' . implode('|', self::EXCLUDED_NAMESPACES) . ')\\\\#'; - $excludedNamespaceMatch = Strings::match($resolvedClassName, $excludedNamespacePattern); - - if ($excludedNamespaceMatch !== null) { - return true; - } - - if (in_array($resolvedClassName, self::EXCLUDED_FATAL_CLASSES, true)) { - return true; - } - if ($definition->getFactory()) { return true; } diff --git a/src/DependencyInjection/Skipper/ParameterSkipper.php b/src/DependencyInjection/Skipper/ParameterSkipper.php index 18a482d910b..65485d96e8a 100644 --- a/src/DependencyInjection/Skipper/ParameterSkipper.php +++ b/src/DependencyInjection/Skipper/ParameterSkipper.php @@ -9,40 +9,13 @@ use ReflectionNamedType; use ReflectionParameter; use ReflectionType; -use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\DependencyInjection\Definition; final class ParameterSkipper { - /** - * Classes that create circular dependencies - * - * @var class-string[]|string[] - */ - private const DEFAULT_EXCLUDED_FATAL_CLASSES = [ - 'Symfony\Component\Form\FormExtensionInterface', - 'Symfony\Component\Asset\PackageInterface', - 'Symfony\Component\Config\Loader\LoaderInterface', - 'Symfony\Component\VarDumper\Dumper\ContextProvider\ContextProviderInterface', - 'EasyCorp\Bundle\EasyAdminBundle\Form\Type\Configurator\TypeConfiguratorInterface', - 'Sonata\CoreBundle\Model\Adapter\AdapterInterface', - 'Sonata\Doctrine\Adapter\AdapterChain', - 'Sonata\Twig\Extension\TemplateExtension', - ]; - - /** - * @var string[] - */ - private array $excludedFatalClasses = []; - - /** - * @param string[] $excludedFatalClasses - */ public function __construct( private readonly ParameterTypeResolver $parameterTypeResolver, - array $excludedFatalClasses = [] ) { - $this->excludedFatalClasses = array_merge(self::DEFAULT_EXCLUDED_FATAL_CLASSES, $excludedFatalClasses); } public function shouldSkipParameter( @@ -69,11 +42,8 @@ public function shouldSkipParameter( return true; } - if (in_array($parameterType, $this->excludedFatalClasses, true)) { - return true; - } - - if (! class_exists($parameterType) && ! interface_exists($parameterType)) { + // autowire only rector classes + if (! str_starts_with($parameterType, 'Rector\\')) { return true; } diff --git a/src/Kernel/RectorKernel.php b/src/Kernel/RectorKernel.php index 936c48dbccd..bfecd1185a8 100644 --- a/src/Kernel/RectorKernel.php +++ b/src/Kernel/RectorKernel.php @@ -17,7 +17,7 @@ final class RectorKernel /** * @var string */ - private const CACHE_KEY = 'v74'; + private const CACHE_KEY = 'v75'; private ContainerInterface|null $container = null;