Configurable downgrade (#4099)

This commit is contained in:
Leonardo Losoviz 2020-09-02 00:12:01 +08:00 committed by GitHub
parent 020c7b82b0
commit 02e703b9b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 201 additions and 8 deletions

View File

@ -7,6 +7,7 @@ namespace Rector\Downgrade\Rector\Property;
use PhpParser\Node;
use PhpParser\Node\Stmt\Property;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
@ -15,8 +16,18 @@ use Rector\NodeTypeResolver\Node\AttributeKey;
/**
* @see \Rector\Downgrade\Tests\Rector\Property\DowngradeTypedPropertyRector\DowngradeTypedPropertyRectorTest
*/
final class DowngradeTypedPropertyRector extends AbstractRector
final class DowngradeTypedPropertyRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var string
*/
public const ADD_DOC_BLOCK = '$addDocBlock';
/**
* @var bool
*/
private $addDocBlock = true;
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Changes property type definition from type definitions to `@var` annotations.', [
@ -41,6 +52,11 @@ PHP
]);
}
public function configure(array $configuration): void
{
$this->addDocBlock = $configuration[self::ADD_DOC_BLOCK] ?? true;
}
/**
* @return string[]
*/
@ -58,14 +74,16 @@ PHP
return null;
}
/** @var PhpDocInfo|null $phpDocInfo */
$phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
if ($phpDocInfo === null) {
$phpDocInfo = $this->phpDocInfoFactory->createEmpty($node);
}
if ($this->addDocBlock) {
/** @var PhpDocInfo|null $phpDocInfo */
$phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
if ($phpDocInfo === null) {
$phpDocInfo = $this->phpDocInfoFactory->createEmpty($node);
}
$newType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($node->type);
$phpDocInfo->changeVarType($newType);
$newType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($node->type);
$phpDocInfo->changeVarType($newType);
}
$node->type = null;
return $node;

View File

@ -25,6 +25,18 @@ final class DowngradeTypedPropertyRectorTest extends AbstractRectorTestCase
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
/**
* @return array<string, mixed[]>
*/
protected function getRectorsWithConfiguration(): array
{
return [
DowngradeTypedPropertyRector::class => [
DowngradeTypedPropertyRector::ADD_DOC_BLOCK => true,
],
];
}
protected function getRectorClass(): string
{
return DowngradeTypedPropertyRector::class;

View File

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace Rector\Downgrade\Tests\Rector\Property\NoDocBlockDowngradeTypedPropertyRector;
use Iterator;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\Downgrade\Rector\Property\DowngradeTypedPropertyRector;
use Symplify\SmartFileSystem\SmartFileInfo;
final class DowngradeTypedPropertyRectorTest extends AbstractRectorTestCase
{
/**
* @requires PHP >= 7.4
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
/**
* @return array<string, mixed[]>
*/
protected function getRectorsWithConfiguration(): array
{
return [
DowngradeTypedPropertyRector::class => [
DowngradeTypedPropertyRector::ADD_DOC_BLOCK => false,
],
];
}
protected function getRectorClass(): string
{
return DowngradeTypedPropertyRector::class;
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace Rector\Downgrade\Tests\Rector\Property\NoDocBlockDowngradeTypedPropertyRector\Fixture;
use Rector\Downgrade\Tests\Rector\Property\NoDocBlockDowngradeTypedPropertyRector\Source\AnotherClass;
class ClassNameNullableTypeClass
{
private ?AnotherClass $property;
}
?>
-----
<?php
namespace Rector\Downgrade\Tests\Rector\Property\NoDocBlockDowngradeTypedPropertyRector\Fixture;
use Rector\Downgrade\Tests\Rector\Property\NoDocBlockDowngradeTypedPropertyRector\Source\AnotherClass;
class ClassNameNullableTypeClass
{
private $property;
}
?>

View File

@ -0,0 +1,19 @@
<?php
namespace Rector\Downgrade\Tests\Rector\Property\NoDocBlockDowngradeTypedPropertyRector\Fixture;
class ClassNameClass {
private \Rector\Downgrade\Tests\Rector\Property\NoDocBlockDowngradeTypedPropertyRector\Source\AnotherClass $property;
}
?>
-----
<?php
namespace Rector\Downgrade\Tests\Rector\Property\NoDocBlockDowngradeTypedPropertyRector\Fixture;
class ClassNameClass {
private $property;
}
?>

View File

@ -0,0 +1,25 @@
<?php
namespace Rector\Downgrade\Tests\Rector\Property\NoDocBlockDowngradeTypedPropertyRector\Fixture;
class DocBlockExists {
/**
* This property is the best one
*/
private ?string $property;
}
?>
-----
<?php
namespace Rector\Downgrade\Tests\Rector\Property\NoDocBlockDowngradeTypedPropertyRector\Fixture;
class DocBlockExists {
/**
* This property is the best one
*/
private $property;
}
?>

View File

@ -0,0 +1,21 @@
<?php
namespace Rector\Downgrade\Tests\Rector\Property\NoDocBlockDowngradeTypedPropertyRector\Fixture;
class SomeClass
{
private string $property;
}
?>
-----
<?php
namespace Rector\Downgrade\Tests\Rector\Property\NoDocBlockDowngradeTypedPropertyRector\Fixture;
class SomeClass
{
private $property;
}
?>

View File

@ -0,0 +1,19 @@
<?php
namespace Rector\Downgrade\Tests\Rector\Property\NoDocBlockDowngradeTypedPropertyRector\Fixture;
class NullableTypeClass {
private ?string $property;
}
?>
-----
<?php
namespace Rector\Downgrade\Tests\Rector\Property\NoDocBlockDowngradeTypedPropertyRector\Fixture;
class NullableTypeClass {
private $property;
}
?>

View File

@ -0,0 +1,10 @@
<?php
namespace Rector\Downgrade\Tests\Rector\Property\NoDocBlockDowngradeTypedPropertyRector\Source;
class AnotherClass
{
}