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
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\GetFiltersToAsTwigFilterAttributeRector\Fixture;

use Twig\DeprecatedCallableInfo;
use Twig\Environment;
use Twig\Node\Node;
use Twig\Extension\AbstractExtension;

final class WithOptionsParameter extends AbstractExtension
{
public function getFilters(): array
{
return [
new \Twig\TwigFilter('with_environment', $this->withEnvironment(...), ['needs_environment' => true]),
new \Twig\TwigFilter('with_context', [$this, 'withContext'], ['needs_context' => true]),
new \Twig\TwigFilter('with_charset', [$this, 'withCharset'], ['needs_charset' => true]),
new \Twig\TwigFilter('with_pre_escape', [$this, 'withPreEscape'], ['pre_escape' => 'html']),
new \Twig\TwigFilter('with_preserves_safety', [$this, 'withPreservesSafety'], ['preserves_safety' => ['html']]),
new \Twig\TwigFilter('with_safe_callback', [$this, 'withSafeCallback'], ['is_safe_callback' => [self::class, 'checkSafeCallback']]),
new \Twig\TwigFilter('with_deprecation_info', [$this, 'withDeprecationInfo'], ['deprecation_info' => new DeprecatedCallableInfo('package', 'version')]),
new \Twig\TwigFilter('with_everything', [$this, 'withEverything'], ['is_safe' => ['html'], 'needs_context' => true, 'needs_charset' => true, 'needs_environment' => true, 'pre_escape' => 'html', 'preserves_safety' => ['html']],),
];
}

public function withEnvironment(Environment $env, $value)
{
return $value;
}

public function withContext(array $context, $value)
{
return $value;
}

public function withCharset(string $charset, $value)
{
return $value;
}

public function withPreEscape($value)
{
return $value;
}

public function withPreservesSafety($value)
{
return $value;
}

public function withSafeCallback($value)
{
return $value;
}

public function withDeprecationInfo($value)
{
return $value;
}

public function withEverything(string $charset, Environment $env, array $context, $value)
{
return $value;
}

public function checkSafeCallback(Node $argsNode): array
{
return [];
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\GetFiltersToAsTwigFilterAttributeRector\Fixture;

use Twig\DeprecatedCallableInfo;
use Twig\Environment;
use Twig\Node\Node;
use Twig\Extension\AbstractExtension;

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

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

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

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

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

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

#[\Twig\Attribute\AsTwigFilter('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'])]
public function withEverything(string $charset, Environment $env, array $context, $value)
{
return $value;
}

public function checkSafeCallback(Node $argsNode): array
{
return [];
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\GetFunctionsToAsTwigFunctionAttributeRector\Fixture;

use Twig\DeprecatedCallableInfo;
use Twig\Environment;
use Twig\Node\Node;
use Twig\Extension\AbstractExtension;

final class WithOptionsParameter extends AbstractExtension
{
public function getFunctions(): array
{
return [
new \Twig\TwigFunction('with_environment', $this->withEnvironment(...), ['needs_environment' => true]),
new \Twig\TwigFunction('with_context', [$this, 'withContext'], ['needs_context' => true]),
new \Twig\TwigFunction('with_charset', [$this, 'withCharset'], ['needs_charset' => true]),
new \Twig\TwigFunction('with_safe_callback', [$this, 'withSafeCallback'], ['is_safe_callback' => [self::class, 'checkSafeCallback']]),
new \Twig\TwigFunction('with_deprecation_info', [$this, 'withDeprecationInfo'], ['deprecation_info' => new DeprecatedCallableInfo('package', 'version')]),
new \Twig\TwigFunction('with_everything', [$this, 'withEverything'], ['is_safe' => ['html'], 'needs_context' => true, 'needs_charset' => true, 'needs_environment' => true]),
];
}

public function withEnvironment(Environment $env, $value)
{
return $value;
}

public function withContext(array $context, $value)
{
return $value;
}

public function withCharset(string $charset, $value)
{
return $value;
}

public function withSafeCallback($value)
{
return $value;
}

public function withDeprecationInfo($value)
{
return $value;
}

public function withEverything(string $charset, Environment $env, array $context, $value)
{
return $value;
}

public function checkSafeCallback(Node $argsNode): array
{
return [];
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\GetFunctionsToAsTwigFunctionAttributeRector\Fixture;

use Twig\DeprecatedCallableInfo;
use Twig\Environment;
use Twig\Node\Node;
use Twig\Extension\AbstractExtension;

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

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

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

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

#[\Twig\Attribute\AsTwigFunction('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)]
public function withEverything(string $charset, Environment $env, array $context, $value)
{
return $value;
}

public function checkSafeCallback(Node $argsNode): array
{
return [];
}
}

?>
74 changes: 69 additions & 5 deletions rules/Symfony73/GetMethodToAsTwigAttributeTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
Expand All @@ -28,6 +29,15 @@
*/
final readonly class GetMethodToAsTwigAttributeTransformer
{
private const OPTION_TO_NAMED_ARG = [
'is_safe' => 'isSafe',
'needs_environment' => 'needsEnvironment',
'needs_context' => 'needsContext',
'needs_charset' => 'needsCharset',
'is_safe_callback' => 'isSafeCallback',
'deprecation_info' => 'deprecationInfo',
];

public function __construct(
private LocalArrayMethodCallableMatcher $localArrayMethodCallableMatcher,
private ReturnEmptyArrayMethodRemover $returnEmptyArrayMethodRemover,
Expand All @@ -36,11 +46,15 @@ public function __construct(
) {
}

/**
* @param array<string, string> $additionalOptionMapping
*/
public function transformClassGetMethodToAttributeMarker(
Class_ $class,
string $methodName,
string $attributeClass,
ObjectType $objectType
ObjectType $objectType,
array $additionalOptionMapping = []
): bool {

// check if attribute even exists
Expand Down Expand Up @@ -77,7 +91,8 @@ public function transformClassGetMethodToAttributeMarker(
}

$new = $arrayItem->value;
if (count($new->getArgs()) !== 2) {
$argCount = count($new->getArgs());
if ($argCount > 3 || $argCount < 2) {
continue;
}

Expand All @@ -87,6 +102,7 @@ public function transformClassGetMethodToAttributeMarker(
}

$secondArg = $new->getArgs()[1];
$thirdArg = $new->getArgs()[2] ?? null;

if ($this->isLocalCallable($secondArg->value)) {
$localMethodName = $this->localArrayMethodCallableMatcher->match($secondArg->value, $objectType);
Expand All @@ -100,7 +116,12 @@ public function transformClassGetMethodToAttributeMarker(
continue;
}

$this->decorateMethodWithAttribute($localMethod, $attributeClass, $nameArg);
$optionArguments = $this->getArgumentsFromOptionArray($thirdArg, $additionalOptionMapping);
if ($optionArguments === null) {
continue;
}

$this->decorateMethodWithAttribute($localMethod, $attributeClass, [$nameArg, ...$optionArguments]);
$this->visibilityManipulator->makePublic($localMethod);

// remove old new function instance
Expand All @@ -120,9 +141,12 @@ public function transformClassGetMethodToAttributeMarker(
return $hasChanged;
}

private function decorateMethodWithAttribute(ClassMethod $classMethod, string $attributeClass, Arg $arg): void
/**
* @param Arg[] $args
*/
private function decorateMethodWithAttribute(ClassMethod $classMethod, string $attributeClass, array $args): void
{
$classMethod->attrGroups[] = new AttributeGroup([new Attribute(new FullyQualified($attributeClass), [$arg])]);
$classMethod->attrGroups[] = new AttributeGroup([new Attribute(new FullyQualified($attributeClass), $args)]);
}

private function isLocalCallable(Expr $expr): bool
Expand All @@ -133,4 +157,44 @@ private function isLocalCallable(Expr $expr): bool

return $expr instanceof Array_ && count($expr->items) === 2;
}

/**
* @param array<string, string> $additionalOptionMapping
*
* @return Arg[]|null
*/
private function getArgumentsFromOptionArray(?Arg $optionArgument, array $additionalOptionMapping): ?array
{
if (! $optionArgument?->value instanceof Array_) {
return [];
}

$allOptionMappings = [...self::OPTION_TO_NAMED_ARG, ...$additionalOptionMapping];

$args = [];
foreach ($optionArgument->value->items as $item) {
if (! $item->key instanceof String_) {
continue;
}

$mappedName = $allOptionMappings[$item->key->value] ?? null;
if ($mappedName === null) {
continue;
}

if ($mappedName === 'isSafeCallback') {
if ($item->value instanceof MethodCall && $item->value->isFirstClassCallable()) {
continue;
}
}

$arg = new Arg($item->value);
$arg->name = new Identifier($mappedName);
$args[] = $arg;
}

$totalItems = count($optionArgument->value->items);

return count($args) === $totalItems ? $args : null;
}
}
Loading