10000 [Symfony] Add RemoveDefaultGetBlockPrefixRector by TomasVotruba · Pull Request #3705 · rectorphp/rector · GitHub
[go: up one dir, main page]

Skip to content

[Symfony] Add RemoveDefaultGetBlockPrefixRector #3705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 13, 2020
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
1 change: 1 addition & 0 deletions config/set/symfony/symfony30.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 23 additions & 2 deletions docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# All 523 Rectors Overview
# All 524 Rectors Overview

- [Projects](#projects)
- [General](#general)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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');
}
}
Original file line number Diff line number Diff line change
@@ -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
{
}

?>
Original file line number Diff line number Diff line change
@@ -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
{
}

?>
Original file line number Diff line number Diff line change
@@ -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';
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
0