mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-05 10:50:48 +00:00
Merge pull request #17 from TomasVotruba/symfony-contraint-value-url
[Symfony] add ContraintsUrlOptionRector
This commit is contained in:
commit
9ad4960831
67
src/Rector/Contrib/Symfony/ConstraintUrlOptionRector.php
Normal file
67
src/Rector/Contrib/Symfony/ConstraintUrlOptionRector.php
Normal 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');
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
<?php declare (strict_types=1);
|
||||
|
||||
$containt = new Url(['checkDNS' => Url::CHECK_DNS_TYPE_ANY]);
|
|
@ -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];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
<?php declare (strict_types=1);
|
||||
|
||||
$containt = new Url(['checkDNS' => true]);
|
Loading…
Reference in New Issue
Block a user