mirror of
https://github.com/rectorphp/rector.git
synced 2025-03-13 20:12:07 +01:00
[Symfony] Add RemoveDefaultGetBlockPrefixRector
This commit is contained in:
parent
965cef98e2
commit
86caa737f9
@ -12,6 +12,7 @@ services:
|
||||
Rector\Symfony\Rector\MethodCall\FormTypeInstanceToClassConstRector: null
|
||||
Rector\Symfony\Rector\Form\StringFormTypeToClassRector: null
|
||||
Rector\Symfony\Rector\MethodCall\CascadeValidationFormBuilderRector: null
|
||||
Rector\Symfony\Rector\ClassMethod\RemoveDefaultGetBlockPrefixRector: null
|
||||
|
||||
# forms - collection
|
||||
Rector\Symfony\Rector\MethodCall\ChangeCollectionTypeOptionTypeFromStringToClassReferenceRector: null
|
||||
|
@ -1,4 +1,4 @@
|
||||
# All 523 Rectors Overview
|
||||
# All 524 Rectors Overview
|
||||
|
||||
- [Projects](#projects)
|
||||
- [General](#general)
|
||||
@ -65,7 +65,7 @@
|
||||
- [SOLID](#solid) (12)
|
||||
- [Sensio](#sensio) (3)
|
||||
- [StrictCodeQuality](#strictcodequality) (1)
|
||||
- [Symfony](#symfony) (32)
|
||||
- [Symfony](#symfony) (33)
|
||||
- [SymfonyCodeQuality](#symfonycodequality) (1)
|
||||
- [SymfonyPHPUnit](#symfonyphpunit) (1)
|
||||
- [Twig](#twig) (1)
|
||||
@ -11148,6 +11148,27 @@ Turns redirect to route to short helper method in Controller in Symfony
|
||||
|
||||
<br><br>
|
||||
|
||||
### `RemoveDefaultGetBlockPrefixRector`
|
||||
|
||||
- class: [`Rector\Symfony\Rector\ClassMethod\RemoveDefaultGetBlockPrefixRector`](/../master/rules/symfony/src/Rector/ClassMethod/RemoveDefaultGetBlockPrefixRector.php)
|
||||
- [test fixtures](/../master/rules/symfony/tests/Rector/ClassMethod/RemoveDefaultGetBlockPrefixRector/Fixture)
|
||||
|
||||
Rename `getBlockPrefix()` if it returns the default value - class to underscore, e.g. UserFormType = user_form
|
||||
|
||||
```diff
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
|
||||
class TaskType extends AbstractType
|
||||
{
|
||||
- public function getBlockPrefix()
|
||||
- {
|
||||
- return 'task';
|
||||
- }
|
||||
}
|
||||
```
|
||||
|
||||
<br><br>
|
||||
|
||||
### `ResponseStatusCodeRector`
|
||||
|
||||
- class: [`Rector\Symfony\Rector\BinaryOp\ResponseStatusCodeRector`](/../master/rules/symfony/src/Rector/BinaryOp/ResponseStatusCodeRector.php)
|
||||
|
@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Symfony\Rector\ClassMethod;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\RectorDefinition\CodeSample;
|
||||
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||
use Rector\Core\Util\StaticRectorStrings;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
|
||||
/**
|
||||
* @see https://github.com/symfony/symfony/blob/3.4/UPGRADE-3.0.md#form
|
||||
*
|
||||
* @see \Rector\Symfony\Tests\Rector\ClassMethod\RemoveDefaultGetBlockPrefixRector\RemoveDefaultGetBlockPrefixRectorTest
|
||||
*/
|
||||
final class RemoveDefaultGetBlockPrefixRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition(
|
||||
'Rename `getBlockPrefix()` if it returns the default value - class to underscore, e.g. UserFormType = user_form',
|
||||
[
|
||||
new CodeSample(
|
||||
<<<'PHP'
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
|
||||
class TaskType extends AbstractType
|
||||
{
|
||||
public function getBlockPrefix()
|
||||
{
|
||||
return 'task';
|
||||
}
|
||||
}
|
||||
PHP
|
||||
,
|
||||
<<<'PHP'
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
|
||||
class TaskType extends AbstractType
|
||||
{
|
||||
}
|
||||
PHP
|
||||
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [ClassMethod::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $this->isObjectMethodNameMatch($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$returnedExpr = $this->resolveOnlyStmtReturnExpr($node);
|
||||
if ($returnedExpr === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$returnedValue = $this->getValue($returnedExpr);
|
||||
|
||||
$classShortName = $node->getAttribute(AttributeKey::CLASS_SHORT_NAME);
|
||||
if (Strings::endsWith($classShortName, 'Type')) {
|
||||
$classShortName = Strings::before($classShortName, 'Type');
|
||||
}
|
||||
|
||||
$underscoredClassShortName = StaticRectorStrings::camelCaseToUnderscore($classShortName);
|
||||
if ($underscoredClassShortName !== $returnedValue) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->removeNode($node);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* return <$thisValue>;
|
||||
*/
|
||||
private function resolveOnlyStmtReturnExpr(ClassMethod $classMethod): ?Expr
|
||||
{
|
||||
if (count((array) $classMethod->stmts) !== 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$onlyStmt = $classMethod->stmts[0];
|
||||
if (! $onlyStmt instanceof Return_) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $onlyStmt->expr;
|
||||
}
|
||||
|
||||
private function isObjectMethodNameMatch(ClassMethod $classMethod): bool
|
||||
{
|
||||
if (! $this->isInObjectType($classMethod, 'Symfony\Component\Form\AbstractType')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->isName($classMethod->name, 'getBlockPrefix');
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Symfony\Tests\Rector\ClassMethod\RemoveDefaultGetBlockPrefixRector\Fixture;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
|
||||
class DoubleNameType extends AbstractType
|
||||
{
|
||||
public function getBlockPrefix()
|
||||
{
|
||||
return 'double_name';
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Symfony\Tests\Rector\ClassMethod\RemoveDefaultGetBlockPrefixRector\Fixture;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
|
||||
class DoubleNameType extends AbstractType
|
||||
{
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Symfony\Tests\Rector\ClassMethod\RemoveDefaultGetBlockPrefixRector\Fixture;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
|
||||
class TaskType extends AbstractType
|
||||
{
|
||||
public function getBlockPrefix()
|
||||
{
|
||||
return 'task';
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Symfony\Tests\Rector\ClassMethod\RemoveDefaultGetBlockPrefixRector\Fixture;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
|
||||
class TaskType extends AbstractType
|
||||
{
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Symfony\Tests\Rector\ClassMethod\RemoveDefaultGetBlockPrefixRector\Fixture;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
|
||||
class SkipDifferentNameType extends AbstractType
|
||||
{
|
||||
public function getBlockPrefix()
|
||||
{
|
||||
return 'unique';
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Symfony\Tests\Rector\ClassMethod\RemoveDefaultGetBlockPrefixRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Rector\Symfony\Rector\ClassMethod\RemoveDefaultGetBlockPrefixRector;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class RemoveDefaultGetBlockPrefixRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return RemoveDefaultGetBlockPrefixRector::class;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user