mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-01 00:40:52 +00:00
[Nette] FormSetRequiredRector init
This commit is contained in:
parent
eaf264ab23
commit
b230f5201f
|
@ -3,10 +3,35 @@
|
|||
namespace Rector\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
|
||||
final class MethodCallAnalyzer
|
||||
{
|
||||
public function isMethodCallTypeAndMethods(Node $node, string $type, array $methodsNames): bool
|
||||
{
|
||||
if (! $this->isMethodCallType($node, $type)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private function isMethodCallType(Node $node, string $type): bool
|
||||
{
|
||||
if (! $node instanceof MethodCall) {
|
||||
return false;
|
||||
}
|
||||
|
||||
dump($node->getAttribute('type'));
|
||||
die;
|
||||
|
||||
if ($node->class->toString() !== $type) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string[] $methodNames
|
||||
*/
|
||||
|
|
55
src/Rector/Contrib/Nette/FormSetRequiredRector.php
Normal file
55
src/Rector/Contrib/Nette/FormSetRequiredRector.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector\Contrib\Nette;
|
||||
|
||||
use PhpParser\Node;
|
||||
use Rector\Deprecation\SetNames;
|
||||
use Rector\NodeAnalyzer\MethodCallAnalyzer;
|
||||
use Rector\Rector\AbstractRector;
|
||||
|
||||
/**
|
||||
* Covers https://forum.nette.org/cs/26672-missing-setrequired-true-false-on-field-abc-in-form
|
||||
*/
|
||||
final class FormSetRequiredRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const FORM_CLASS = 'Nette\Application\UI\Form';
|
||||
|
||||
/**
|
||||
* @var MethodCallAnalyzer
|
||||
*/
|
||||
private $methodCallAnalyzer;
|
||||
|
||||
public function __construct(MethodCallAnalyzer $methodCallAnalyzer)
|
||||
{
|
||||
$this->methodCallAnalyzer = $methodCallAnalyzer;
|
||||
}
|
||||
|
||||
public function getSetName(): string
|
||||
{
|
||||
return SetNames::NETTE;
|
||||
}
|
||||
|
||||
public function sinceVersion(): float
|
||||
{
|
||||
return 2.4;
|
||||
}
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
if (! $this->methodCallAnalyzer->isMethodCallTypeAndMethods($node, self::FORM_CLASS, ['addCondition'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// dump($node);
|
||||
die;
|
||||
}
|
||||
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
// replace ->addCondition($form::FILLED) by ->setRequired(FALSE)
|
||||
// TODO: Implement refactor() method.
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
<?php declare (strict_types=1);
|
||||
|
||||
class SomePresenter
|
||||
{
|
||||
public function createNetteForm()
|
||||
{
|
||||
$form = new \Nette\Application\UI\Form;
|
||||
$form->addText('name')
|
||||
->setRequired(FALSE)
|
||||
->addRule('...')
|
||||
->addRule('...');
|
||||
|
||||
return $form;
|
||||
}
|
||||
}
|
25
tests/Rector/Contrib/Nette/FormSetRequiredRector/Test.php
Normal file
25
tests/Rector/Contrib/Nette/FormSetRequiredRector/Test.php
Normal file
|
@ -0,0 +1,25 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\Contrib\Nette\FormSetRequiredRector;
|
||||
|
||||
use Rector\Rector\Contrib\Nette\FormSetRequiredRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class Test extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFileMatchesExpectedContent(
|
||||
__DIR__ . '/Wrong/wrong.php.inc',
|
||||
__DIR__ . '/Correct/correct.php.inc'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getRectorClasses(): array
|
||||
{
|
||||
return [FormSetRequiredRector::class];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
<?php declare (strict_types=1);
|
||||
|
||||
class SomePresenter
|
||||
{
|
||||
public function createNetteForm()
|
||||
{
|
||||
$form = new \Nette\Application\UI\Form;
|
||||
$form->addText('name')
|
||||
->addCondition($form::FILLED)
|
||||
->addRule('...')
|
||||
->addRule('...');
|
||||
|
||||
return $form;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user