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,85 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Symfony73\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @deprecated Handling getFilters() alone leaves the sibling getFunctions()/getTests() methods behind and can produce
* a half-converted extension. Use the GetFiltersAndFunctionsToAsTwigAttributeRector rule instead, that
* converts all the get methods at once.
*/
final class GetFiltersToAsTwigFilterAttributeRector extends AbstractRector implements DeprecatedInterface
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Changes getFilters() in TwigExtension to #[AsTwigFilter] marker attribute above local class method',
[
new CodeSample(
<<<'CODE_SAMPLE'
use Twig\Extension\AbstractExtension;
use Twig\Environment;

class SomeClass extends AbstractExtension
{
public function getFilters()
{
return [
new \Twig\TwigFilter('filter_name', [$this, 'localMethod', 'needs_environment' => true]),
];
}

public function localMethod(Environment $env, $value)
{
return $value;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Twig\Extension\AbstractExtension;
use Twig\Attribute\AsTwigFilter;
use Twig\Environment;

class SomeClass extends AbstractExtension
{
#[AsTwigFilter('filter_name', needsEnvironment: true)]
public function localMethod(Environment $env, $value)
{
return $value;
}
}
CODE_SAMPLE
)]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}

/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Class_
{
throw new ShouldNotHappenException(sprintf(
'"%s" is deprecated, as it converts getFilters() only and leaves getFunctions()/getTests() behind. Use "%s" instead.',
self::class,
GetFiltersAndFunctionsToAsTwigAttributeRector::class
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Symfony73\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @deprecated Handling getFunctions() alone leaves the sibling getFilters()/getTests() methods behind and can produce
* a half-converted extension. Use the GetFiltersAndFunctionsToAsTwigAttributeRector rule instead, that
* converts all the get methods at once.
*/
final class GetFunctionsToAsTwigFunctionAttributeRector extends AbstractRector implements DeprecatedInterface
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Changes getFunctions() in TwigExtension to #[AsTwigFunction] marker attribute above local class method',
[
new CodeSample(
<<<'CODE_SAMPLE'
use Twig\Extension\AbstractExtension;
use Twig\Environment;

class SomeClass extends AbstractExtension
{
public function getFunctions()
{
return [
new \Twig\TwigFunction('function_name', [$this, 'localMethod', 'needs_environment' => true]),
];
}

public function localMethod(Environment $env, $value)
{
return $value;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Twig\Extension\AbstractExtension;
use Twig\Attribute\AsTwigFunction;
use Twig\Environment;

class SomeClass extends AbstractExtension
{
#[AsTwigFunction(name: 'function_name', needsEnvironment: true)]
public function localMethod(Environment $env, $value)
{
return $value;
}
}
CODE_SAMPLE
)]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}

/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Class_
{
throw new ShouldNotHappenException(sprintf(
'"%s" is deprecated, as it converts getFunctions() only and leaves getFilters()/getTests() behind. Use "%s" instead.',
self::class,
GetFiltersAndFunctionsToAsTwigAttributeRector::class
));
}
}
Loading