-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MaxLengthSymfonyFormOptionToAttrRector
- Loading branch information
1 parent
4e464b4
commit 5bc75ab
Showing
10 changed files
with
330 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
src/Rector/MethodCall/MaxLengthSymfonyFormOptionToAttrRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Symfony\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr\Array_; | ||
use PhpParser\Node\Expr\ArrayItem; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Scalar\String_; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Core\Exception\ShouldNotHappenException; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @changelog https://github.com/symfony/symfony/commit/feea36df99205fd12be52515edeb10fc18523232 | ||
* @changelog https://github.com/symfony/symfony-docs/pull/3461 | ||
* @changelog https://github.com/symfony/symfony/issues/7148 | ||
* | ||
* @see \Rector\Symfony\Tests\Rector\Array_\MaxLengthSymfonyFormOptionToAttrRector\MaxLengthSymfonyFormOptionToAttrRectorTest | ||
*/ | ||
final class MaxLengthSymfonyFormOptionToAttrRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Change form option "max_length" to a form "attr" > "max_length"', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
$formBuilder = new Symfony\Component\Form\FormBuilder(); | ||
$form = $formBuilder->create('name', 'text', [ | ||
'max_length' => 123, | ||
]); | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
$formBuilder = new Symfony\Component\Form\FormBuilder(); | ||
$form = $formBuilder->create('name', 'text', [ | ||
'attr' => ['maxlength' => 123], | ||
]); | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class]; | ||
} | ||
|
||
/** | ||
* @param MethodCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->isFormCreateMethodCallMatch($node)) { | ||
return null; | ||
} | ||
|
||
$optionsArg = $node->getArgs()[2] ?? null; | ||
if (! $optionsArg instanceof Arg) { | ||
return null; | ||
} | ||
|
||
if (! $optionsArg->value instanceof Array_) { | ||
return null; | ||
} | ||
|
||
$optionsArray = $optionsArg->value; | ||
|
||
$itemToAddToAttrs = null; | ||
|
||
foreach ($optionsArray->items as $arrayKey => $arrayItem) { | ||
if (! $arrayItem instanceof ArrayItem) { | ||
continue; | ||
} | ||
|
||
if (! $arrayItem->key instanceof String_) { | ||
continue; | ||
} | ||
|
||
if (! $this->valueResolver->isValue($arrayItem->key, 'max_length')) { | ||
continue; | ||
} | ||
|
||
unset($optionsArray->items[$arrayKey]); | ||
|
||
$itemToAddToAttrs = $arrayItem; | ||
break; | ||
} | ||
|
||
if (! $itemToAddToAttrs instanceof ArrayItem) { | ||
return null; | ||
} | ||
|
||
$this->addArrayItemToAttrsItemOrCreateOne($optionsArray, $itemToAddToAttrs); | ||
return $node; | ||
} | ||
|
||
private function matchAttrArrayItem(Array_ $array): ?ArrayItem | ||
{ | ||
foreach ($array->items as $arrayItem) { | ||
if (! $arrayItem instanceof ArrayItem) { | ||
continue; | ||
} | ||
|
||
if (! $arrayItem->key instanceof String_) { | ||
continue; | ||
} | ||
|
||
if (! $this->valueResolver->isValue($arrayItem->key, 'attrs')) { | ||
continue; | ||
} | ||
|
||
return $arrayItem; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private function addArrayItemToAttrsItemOrCreateOne(Array_ $array, ArrayItem $arrayItem): Array_ | ||
{ | ||
// rename | ||
$arrayItem->key = new String_('maxlength'); | ||
|
||
$attrArrayItem = $this->matchAttrArrayItem($array); | ||
|
||
if ($attrArrayItem instanceof ArrayItem) { | ||
if (! $attrArrayItem->value instanceof Array_) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
|
||
$attrArrayItem->value->items[] = $arrayItem; | ||
return $array; | ||
} | ||
|
||
$array->items[] = new ArrayItem(new Array_([$arrayItem]), new String_('attr')); | ||
return $array; | ||
} | ||
|
||
private function isFormCreateMethodCallMatch(MethodCall $methodCall): bool | ||
{ | ||
if ( | ||
! $this->isObjectType($methodCall->var, new ObjectType('Symfony\Component\Form\FormFactoryInterface')) | ||
&& ! $this->isObjectType($methodCall->var, new ObjectType('Symfony\Component\Form\FormBuilderInterface')) | ||
) { | ||
return false; | ||
} | ||
|
||
return $this->isNames($methodCall->name, ['create', 'add']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,7 @@ | |
|
||
class FormBuilder implements FormBuilderInterface | ||
{ | ||
|
||
public function add() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symfony\Component\Form; | ||
|
||
if (class_exists('Symfony\Component\Form\FormFactory')) { | ||
return; | ||
} | ||
|
||
class FormFactory implements FormFactoryInterface | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Symfony\Component\Form; | ||
|
||
if (interface_exists('Symfony\Component\Form\FormFactoryInterface')) { | ||
return; | ||
} | ||
|
||
interface FormFactoryInterface | ||
{ | ||
} |
27 changes: 27 additions & 0 deletions
27
tests/Rector/Array_/MaxLengthSymfonyFormOptionToAttrRector/Fixture/some_class.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Rector\Symfony\Tests\Rector\Array_\MaxLengthSymfonyFormOptionToAttrRector\Fixture; | ||
|
||
use Symfony\Component\Form\FormBuilder; | ||
|
||
$formBuilder = new FormBuilder(); | ||
|
||
$form = $formBuilder->add('name', 'text', [ | ||
'max_length' => 123, | ||
]); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Symfony\Tests\Rector\Array_\MaxLengthSymfonyFormOptionToAttrRector\Fixture; | ||
|
||
use Symfony\Component\Form\FormBuilder; | ||
|
||
$formBuilder = new FormBuilder(); | ||
|
||
$form = $formBuilder->add('name', 'text', [ | ||
'attr' => ['maxlength' => 123], | ||
]); | ||
|
||
?> |
23 changes: 23 additions & 0 deletions
23
tests/Rector/Array_/MaxLengthSymfonyFormOptionToAttrRector/Fixture/some_form_factory.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Rector\Symfony\Tests\Rector\Array_\MaxLengthSymfonyFormOptionToAttrRector\Fixture; | ||
|
||
$formBuilder = new \Symfony\Component\Form\FormFactory(); | ||
|
||
$form = $formBuilder->create('name', 'text', [ | ||
'max_length' => 123, | ||
]); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Symfony\Tests\Rector\Array_\MaxLengthSymfonyFormOptionToAttrRector\Fixture; | ||
|
||
$formBuilder = new \Symfony\Component\Form\FormFactory(); | ||
|
||
$form = $formBuilder->create('name', 'text', [ | ||
'attr' => ['maxlength' => 123], | ||
]); | ||
|
||
?> |
32 changes: 32 additions & 0 deletions
32
...ay_/MaxLengthSymfonyFormOptionToAttrRector/MaxLengthSymfonyFormOptionToAttrRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Symfony\Tests\Rector\Array_\MaxLengthSymfonyFormOptionToAttrRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class MaxLengthSymfonyFormOptionToAttrRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(string $file): void | ||
{ | ||
$this->doTestFile($file); | ||
} | ||
|
||
/** | ||
* @return Iterator<string[]> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/Rector/Array_/MaxLengthSymfonyFormOptionToAttrRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Symfony\Rector\MethodCall\MaxLengthSymfonyFormOptionToAttrRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php'); | ||
|
||
$rectorConfig->rule(MaxLengthSymfonyFormOptionToAttrRector::class); | ||
}; |