[DowngradePhp70] Add DowngradeStrictTypeDeclarationRector (#6107)

Co-authored-by: kaizen-ci <info@kaizen-ci.org>
This commit is contained in:
Abdul Malik Ikhsan 2021-04-12 22:28:54 +07:00 committed by GitHub
parent ad7af917e2
commit 4d4fb3611f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 151 additions and 0 deletions

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersion;
use Rector\DowngradePhp70\Rector\Declare_\DowngradeStrictTypeDeclarationRector;
use Rector\DowngradePhp70\Rector\FunctionLike\DowngradeTypeDeclarationRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
@ -13,4 +14,5 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradeTypeDeclarationRector::class);
$services->set(DowngradeStrictTypeDeclarationRector::class);
};

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\DowngradePhp70\Rector\Declare_\DowngradeStrictTypeDeclarationRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class DowngradeStrictTypeDeclarationRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/php_70.php';
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace Rector\Tests\DowngradePhp70\Rector\Declare_\DowngradeStrictTypeDeclarationRector\Fixture;
declare(ticks=1);
class SkipDifferentDeclare
{
}
?>

View File

@ -0,0 +1,21 @@
<?php
namespace Rector\Tests\DowngradePhp70\Rector\Declare_\DowngradeStrictTypeDeclarationRector\Fixture;
declare(strict_types=1);
class Fixture
{
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp70\Rector\Declare_\DowngradeStrictTypeDeclarationRector\Fixture;
class Fixture
{
}
?>

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\DowngradePhp70\Rector\Declare_\DowngradeStrictTypeDeclarationRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersionFeature::SCALAR_TYPES - 1);
$services = $containerConfigurator->services();
$services->set(DowngradeStrictTypeDeclarationRector::class);
};

View File

@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp70\Rector\Declare_;
use PhpParser\Node;
use PhpParser\Node\Stmt\Declare_;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DowngradePhp70\Rector\Declare_\DowngradeStrictTypeDeclarationRector\DowngradeStrictTypeDeclarationRectorTest
*/
final class DowngradeStrictTypeDeclarationRector extends AbstractRector
{
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Declare_::class];
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove the declare(strict_types=1)',
[
new CodeSample(
<<<'CODE_SAMPLE'
declare(strict_types=1);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
CODE_SAMPLE
),
]
);
}
/**
* @param Declare_ $node
*/
public function refactor(Node $node): ?Node
{
if ($this->shouldSkip($node)) {
return null;
}
$this->removeNode($node);
return $node;
}
private function shouldSkip(Declare_ $declare): bool
{
$declares = $declare->declares;
foreach ($declares as $declare) {
if ($this->isName($declare->key, 'strict_types')) {
return false;
}
}
return true;
}
}