Merge pull request #17 from TomasVotruba/symfony-contraint-value-url

[Symfony] add ContraintsUrlOptionRector
This commit is contained in:
Tomáš Votruba 2017-09-01 21:29:49 +02:00 committed by GitHub
commit 9ad4960831
4 changed files with 98 additions and 0 deletions

View File

@ -0,0 +1,67 @@
<?php declare(strict_types=1);
namespace Rector\Rector\Contrib\Symfony;
use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use Rector\Deprecation\SetNames;
use Rector\Rector\AbstractRector;
/**
* Ref: https://github.com/symfony/symfony/blob/master/UPGRADE-4.0.md#validator
*
* Before:
* $containt = new Url(['checkDNS' => true]);
*
* After:
* $containt = new Url(['checkDNS' => Url::CHECK_DNS_TYPE_ANY]);
*/
final class ConstraintUrlOptionRector extends AbstractRector
{
/**
* @todo complete FQN
* @var string
*/
private const URL_CONSTRAINT_CLASS = 'Url';
public function getSetName(): string
{
return SetNames::SYMFONY;
}
public function sinceVersion(): float
{
return 4.0;
}
public function isCandidate(Node $node): bool
{
if (! $node instanceof ConstFetch) {
return false;
}
if ($node->name->toString() !== 'true') {
return false;
}
$prevNode = $node->getAttribute('prev');
if (! $prevNode instanceof String_) {
return false;
}
return $prevNode->value === 'checkDNS';
}
/**
* @param ConstFetch $node
*/
public function refactor(Node $node): ?Node
{
$classNameNode = new Name(self::URL_CONSTRAINT_CLASS);
return new ClassConstFetch($classNameNode, 'CHECK_DNS_TYPE_ANY');
}
}

View File

@ -0,0 +1,3 @@
<?php declare (strict_types=1);
$containt = new Url(['checkDNS' => Url::CHECK_DNS_TYPE_ANY]);

View File

@ -0,0 +1,25 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\Contrib\Symfony\ConstraintUrlOptionRector;
use Rector\Rector\Contrib\Symfony\ConstraintUrlOptionRector;
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 [ConstraintUrlOptionRector::class];
}
}

View File

@ -0,0 +1,3 @@
<?php declare (strict_types=1);
$containt = new Url(['checkDNS' => true]);