[Nette] FormSetRequiredRector init

This commit is contained in:
TomasVotruba 2017-09-05 21:07:02 +02:00
parent eaf264ab23
commit b230f5201f
5 changed files with 135 additions and 0 deletions

View File

@ -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
*/

View 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.
}
}

View File

@ -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;
}
}

View 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];
}
}

View File

@ -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;
}
}