Skip to content
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
20 changes: 6 additions & 14 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
'*/Source/*',
'*/Source*/*',
'*/tests/*/Fixture*/Expected/*',
StringClassNameToClassConstantRector::class => [__DIR__ . '/config', __DIR__ . '/src/Enum'],
StringClassNameToClassConstantRector::class => [
__DIR__ . '/config', __DIR__ . '/src/Enum',
__DIR__ . '/rules/CodeQuality/Enum/',
],
UseClassKeywordForClassNameResolutionRector::class => [__DIR__ . '/config'],

RenameForeachValueVariableToMatchMethodCallReturnTypeRector::class => [
Expand All @@ -34,19 +37,8 @@
// marked as skipped
ReturnNeverTypeRector::class => ['*/tests/*'],
])
->withConfiguredRule(StringClassNameToClassConstantRector::class, [
'Error',
'Exception',
'Symfony\*',
'Twig_*',
'Twig*',
'Swift_*',
'Doctrine\*',
// loaded from project itself
'Psr\Container\ContainerInterface',
'Symfony\Component\Routing\RouterInterface',
'Symfony\Component\DependencyInjection\Container',
])
// @todo cleanup rest and move to enum classes ass single place for class names
->withConfiguredRule(StringClassNameToClassConstantRector::class, ['Symfony\*', 'Twig_*', 'Twig*'])
->withPhpSets()
->withPreparedSets(
deadCode: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use Twig\Extension\AbstractExtension;

final class SomeGetFilter
{
#[\Twig\Attribute\AsTwigFilter('some_filter')]
#[\Twig\Attribute\AsTwigFilter(name: 'some_filter')]
public function someFilter($value)
{
return $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ use Twig\Extension\AbstractExtension;

final class WithFirstClassCallableFilter
{
#[\Twig\Attribute\AsTwigFilter('some_filter')]
#[\Twig\Attribute\AsTwigFilter(name: 'some_filter')]
public function someFilter($value)
{
return $value;
}

#[\Twig\Attribute\AsTwigFilter('another_filter')]
#[\Twig\Attribute\AsTwigFilter(name: 'another_filter')]
public function anotherFilter($value)
{
return $value;
}

#[\Twig\Attribute\AsTwigFilter('third_filter')]
#[\Twig\Attribute\AsTwigFilter(name: 'third_filter')]
public function thirdFilter($value)
{
return $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,49 +82,49 @@ use Twig\Extension\AbstractExtension;

final class WithOptionsParameter
{
#[\Twig\Attribute\AsTwigFilter('with_environment', needsEnvironment: true)]
#[\Twig\Attribute\AsTwigFilter(name: 'with_environment', needsEnvironment: true)]
public function withEnvironment(Environment $env, $value)
{
return $value;
}

#[\Twig\Attribute\AsTwigFilter('with_context', needsContext: true)]
#[\Twig\Attribute\AsTwigFilter(name: 'with_context', needsContext: true)]
public function withContext(array $context, $value)
{
return $value;
}

#[\Twig\Attribute\AsTwigFilter('with_charset', needsCharset: true)]
#[\Twig\Attribute\AsTwigFilter(name: 'with_charset', needsCharset: true)]
public function withCharset(string $charset, $value)
{
return $value;
}

#[\Twig\Attribute\AsTwigFilter('with_pre_escape', preEscape: 'html')]
#[\Twig\Attribute\AsTwigFilter(name: 'with_pre_escape', preEscape: 'html')]
public function withPreEscape($value)
{
return $value;
}

#[\Twig\Attribute\AsTwigFilter('with_preserves_safety', preservesSafety: ['html'])]
#[\Twig\Attribute\AsTwigFilter(name: 'with_preserves_safety', preservesSafety: ['html'])]
public function withPreservesSafety($value)
{
return $value;
}

#[\Twig\Attribute\AsTwigFilter('with_safe_callback', isSafeCallback: [self::class, 'checkSafeCallback'])]
#[\Twig\Attribute\AsTwigFilter(name: 'with_safe_callback', isSafeCallback: [self::class, 'checkSafeCallback'])]
public function withSafeCallback($value)
{
return $value;
}

#[\Twig\Attribute\AsTwigFilter('with_deprecation_info', deprecationInfo: new DeprecatedCallableInfo('package', 'version'))]
#[\Twig\Attribute\AsTwigFilter(name: 'with_deprecation_info', deprecationInfo: new DeprecatedCallableInfo('package', 'version'))]
public function withDeprecationInfo($value)
{
return $value;
}

#[\Twig\Attribute\AsTwigFilter('with_everything', isSafe: ['html'], needsContext: true, needsCharset: true, needsEnvironment: true, preEscape: 'html', preservesSafety: ['html'])]
#[\Twig\Attribute\AsTwigFilter(name: 'with_everything', isSafe: ['html'], needsContext: true, needsCharset: true, needsEnvironment: true, preEscape: 'html', preservesSafety: ['html'])]
public function withEverything(string $charset, Environment $env, array $context, $value)
{
return $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ use Twig\Extension\AbstractExtension;

final class SomeGetFunctions
{
#[\Twig\Attribute\AsTwigFunction('some_function')]
#[\Twig\Attribute\AsTwigFunction(name: 'some_function')]
public function someFunction($value)
{
return $value;
}

#[\Twig\Attribute\AsTwigFunction('another_function')]
#[\Twig\Attribute\AsTwigFunction(name: 'another_function')]
public function anotherFunction($value)
{
return $value;
}

#[\Twig\Attribute\AsTwigFunction('third_function')]
#[\Twig\Attribute\AsTwigFunction(name: 'third_function')]
public function thirdFunction($value)
{
return $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,37 +70,37 @@ use Twig\Extension\AbstractExtension;

final class WithOptionsParameter
{
#[\Twig\Attribute\AsTwigFunction('with_environment', needsEnvironment: true)]
#[\Twig\Attribute\AsTwigFunction(name: 'with_environment', needsEnvironment: true)]
public function withEnvironment(Environment $env, $value)
{
return $value;
}

#[\Twig\Attribute\AsTwigFunction('with_context', needsContext: true)]
#[\Twig\Attribute\AsTwigFunction(name: 'with_context', needsContext: true)]
public function withContext(array $context, $value)
{
return $value;
}

#[\Twig\Attribute\AsTwigFunction('with_charset', needsCharset: true)]
#[\Twig\Attribute\AsTwigFunction(name: 'with_charset', needsCharset: true)]
public function withCharset(string $charset, $value)
{
return $value;
}

#[\Twig\Attribute\AsTwigFunction('with_safe_callback', isSafeCallback: [self::class, 'checkSafeCallback'])]
#[\Twig\Attribute\AsTwigFunction(name: 'with_safe_callback', isSafeCallback: [self::class, 'checkSafeCallback'])]
public function withSafeCallback($value)
{
return $value;
}

#[\Twig\Attribute\AsTwigFunction('with_deprecation_info', deprecationInfo: new DeprecatedCallableInfo('package', 'version'))]
#[\Twig\Attribute\AsTwigFunction(name: 'with_deprecation_info', deprecationInfo: new DeprecatedCallableInfo('package', 'version'))]
public function withDeprecationInfo($value)
{
return $value;
}

#[\Twig\Attribute\AsTwigFunction('with_everything', isSafe: ['html'], needsContext: true, needsCharset: true, needsEnvironment: true)]
#[\Twig\Attribute\AsTwigFunction(name: 'with_everything', isSafe: ['html'], needsContext: true, needsCharset: true, needsEnvironment: true)]
public function withEverything(string $charset, Environment $env, array $context, $value)
{
return $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\Symfony\TypeAnalyzer\ControllerAnalyzer;
use Symplify\PHPStanRules\Enum\SymfonyClass;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand Down Expand Up @@ -110,7 +111,7 @@ public function refactor(Node $node): ?Node
continue;
}

if (! $this->isObjectType($param->type, new ObjectType('Symfony\Component\HttpFoundation\Request'))) {
if (! $this->isObjectType($param->type, new ObjectType(SymfonyClass::REQUEST))) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Rector\Symfony\CodeQuality\Enum\ResponseClass;
use Rector\Symfony\Enum\SensioAttribute;
use Rector\Symfony\Enum\SymfonyAnnotation;
use Rector\Symfony\Enum\SymfonyClass;
use Rector\Symfony\TypeAnalyzer\ControllerAnalyzer;
use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer;
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
Expand Down Expand Up @@ -125,7 +126,8 @@ public function refactor(Node $node): ?Node

$returnType = $this->returnTypeInferer->inferFunctionLike($node);
$types = $returnType instanceof UnionType ? $returnType->getTypes() : [$returnType];
$objectType = new ObjectType('Symfony\Component\HttpFoundation\Response');

$objectType = new ObjectType(SymfonyClass::RESPONSE);

foreach ($types as $type) {
if ($type instanceof ObjectType && $objectType->isSuperTypeOf($type)->yes()) {
Expand Down
8 changes: 2 additions & 6 deletions rules/Symfony30/Rector/ClassMethod/GetRequestRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Rector\Symfony\Bridge\NodeAnalyzer\ControllerMethodAnalyzer;
use Rector\Symfony\Enum\SymfonyClass;
use Rector\Symfony\TypeAnalyzer\ControllerAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -25,11 +26,6 @@
*/
final class GetRequestRector extends AbstractRector
{
/**
* @var string
*/
private const REQUEST_CLASS = 'Symfony\Component\HttpFoundation\Request';

private ?string $requestVariableAndParamName = null;

public function __construct(
Expand Down Expand Up @@ -230,7 +226,7 @@ private function refactorClassMethod(ClassMethod $classMethod): null|ClassMethod
return null;
}

$fullyQualified = new FullyQualified(self::REQUEST_CLASS);
$fullyQualified = new FullyQualified(SymfonyClass::REQUEST);
$classMethod->params[] = new Param(new Variable(
$this->getRequestVariableAndParamName()
), null, $fullyQualified);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\Symfony\Enum\SymfonyClass;
use Rector\Symfony\ValueObject\ConstantMap\SymfonyCommandConstantMap;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -80,7 +81,7 @@ public function refactor(Node $node): ?Node
return null;
}

if (! $classReflection->is('Symfony\Component\Console\Command\Command')) {
if (! $classReflection->is(SymfonyClass::COMMAND)) {
return null;
}

Expand Down Expand Up @@ -121,7 +122,7 @@ private function convertNumberToConstant(Int_ $int): ?ClassConstFetch
}

return $this->nodeFactory->createClassConstFetch(
'Symfony\Component\Console\Command\Command',
SymfonyClass::COMMAND,
SymfonyCommandConstantMap::RETURN_TO_CONST[$int->value]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Rector\NodeCollector\ValueObject\ArrayCallable;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Rector\Symfony\Enum\TwigClass;
use Rector\ValueObject\PhpVersion;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -100,7 +101,7 @@ public function refactor(Node $node): ?Node
{
if (! $this->nodeTypeResolver->isObjectTypes($node, [
new ObjectType('Twig_ExtensionInterface'),
new ObjectType('Twig\Extension\ExtensionInterface'),
new ObjectType(TwigClass::EXTENSION_INTERFACE),
])) {
return null;
}
Expand Down
2 changes: 2 additions & 0 deletions rules/Symfony73/GetMethodToAsTwigAttributeTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ public function transformClassGetMethodToAttributeMarker(
// remove old new function instance
unset($returnArray->items[$key]);

$nameArg->name = new Identifier('name');

$hasChanged = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ public function localMethod(Environment $env, $value)
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Twig\Extension\AbstractExtension;
use Twig\Attribute\AsTwigFunction;
use Twig\Environment;

class SomeClass extends AbstractExtension
class SomeClass
{
#[AsTwigFunction('function_name', needsEnvironment: true)]
#[AsTwigFunction(name: 'function_name', needsEnvironment: true)]
public function localMethod(Environment $env, $value)
{
return $value;
Expand Down
3 changes: 2 additions & 1 deletion src/Bridge/Symfony/ContainerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Exception\ShouldNotHappenException;
use Rector\Symfony\Enum\SymfonyClass;
use Symfony\Component\DependencyInjection\Container;
use Webmozart\Assert\Assert;

Expand Down Expand Up @@ -37,7 +38,7 @@ private function getSymfonyContainer(): object
$container = require $symfonyContainerPhp;

// this allows older Symfony versions, e.g. 2.8 did not have the PSR yet
Assert::isInstanceOf($container, 'Symfony\Component\DependencyInjection\Container');
Assert::isInstanceOf($container, SymfonyClass::CONTAINER);
$this->container = $container;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Enum/SymfonyClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,6 @@ final class SymfonyClass
public const ROUTING_CONFIGURATOR = 'Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator';

public const KERNEL = 'Symfony\Component\HttpKernel\Kernel';

public const CONTAINER = 'Symfony\Component\DependencyInjection\Container';
}
5 changes: 5 additions & 0 deletions src/Enum/TwigClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ final class TwigClass
* @var string
*/
public const AS_TWIG_FUNCTION_ATTRIBUTE = 'Twig\Attribute\AsTwigFunction';

/**
* @var string
*/
public const EXTENSION_INTERFACE = 'Twig\Extension\ExtensionInterface';
}