Skip to content

Commit

Permalink
Various cleanups (#4125)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 8, 2023
1 parent e012a3e commit ade49b3
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 231 deletions.

This file was deleted.

18 changes: 0 additions & 18 deletions rules/Naming/Contract/Guard/ConflictingNameGuardInterface.php

This file was deleted.

17 changes: 2 additions & 15 deletions rules/Naming/Guard/DateTimeAtNamingConventionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,19 @@
use DateTimeInterface;
use PHPStan\Type\TypeWithClassName;
use Rector\Core\Util\StringUtils;
use Rector\Naming\Contract\Guard\ConflictingNameGuardInterface;
use Rector\Naming\Contract\RenameValueObjectInterface;
use Rector\Naming\ValueObject\PropertyRename;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PHPStanStaticTypeMapper\Utils\TypeUnwrapper;

/**
* @implements ConflictingNameGuardInterface<PropertyRename>
*/
final class DateTimeAtNamingConventionGuard implements ConflictingNameGuardInterface
final class DateTimeAtNamingConventionGuard
{
public function __construct(
private readonly NodeTypeResolver $nodeTypeResolver,
private readonly TypeUnwrapper $typeUnwrapper
) {
}

/**
* @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);
Expand Down
16 changes: 4 additions & 12 deletions rules/Naming/Guard/HasMagicGetSetGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<PropertyRename>
*/
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;
}
Expand Down
23 changes: 0 additions & 23 deletions rules/Naming/Guard/NotPrivatePropertyGuard.php

This file was deleted.

33 changes: 0 additions & 33 deletions rules/Naming/Guard/RamseyUuidInterfaceGuard.php

This file was deleted.

31 changes: 21 additions & 10 deletions rules/Naming/RenameGuard/PropertyRenameGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<LoaderInterface>[]|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
Expand Down Expand Up @@ -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;
}
Expand Down
34 changes: 2 additions & 32 deletions src/DependencyInjection/Skipper/ParameterSkipper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<LoaderInterface>[]|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(
Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/RectorKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class RectorKernel
/**
* @var string
*/
private const CACHE_KEY = 'v74';
private const CACHE_KEY = 'v75';

private ContainerInterface|null $container = null;

Expand Down

0 comments on commit ade49b3

Please sign in to comment.