[TypeDeclaration] Remove CompleteVarDocTypePropertyRector, is handled by split rules (#1500)

This commit is contained in:
Tomas Votruba 2021-12-15 01:30:14 +01:00 committed by GitHub
parent bfe8d995b7
commit 3417ff434b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 21 additions and 1002 deletions

View File

@ -11693,29 +11693,6 @@ return static function (ContainerConfigurator $containerConfigurator): void {
<br>
### CompleteVarDocTypePropertyRector
Complete property `@var` annotations or correct the old ones
- class: [`Rector\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector`](../rules/TypeDeclaration/Rector/Property/CompleteVarDocTypePropertyRector.php)
```diff
final class SomeClass
{
+ /**
+ * @var EventDispatcher
+ */
private $eventDispatcher;
public function __construct(EventDispatcher $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
}
```
<br>
### FormerNullableArgumentToScalarTypedRector
Change null in argument, that is now not nullable anymore
@ -11984,6 +11961,27 @@ Add return method return type based on strict typed property
<br>
### TypedPropertyFromAssignsRector
Add typed property from assigned types
- class: [`Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector`](../rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php)
```diff
final class SomeClass
{
- private $name;
+ private string|null $name = null;
public function run()
{
$this->name = 'string';
}
}
```
<br>
### TypedPropertyFromStrictConstructorRector
Add typed properties based only on strict constructor types

View File

@ -121,7 +121,6 @@
"stubs/Doctrine/Common/Persistence/ObjectManager.php",
"rules-tests/Transform/Rector/FuncCall/FuncCallToMethodCallRector/Source/some_view_function.php",
"rules-tests/TypeDeclaration/Rector/FunctionLike/ReturnTypeDeclarationRector/Source/MyBar.php",
"rules-tests/TypeDeclaration/Rector/Property/CompleteVarDocTypePropertyRector/Source/EventDispatcher.php",
"rules-tests/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector/Source/FunctionTyped.php"
]
},

View File

@ -1,36 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
/**
* @requires PHP 8.0
*/
final class AutoImportTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureAutoImport');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/auto_import.php';
}
}

View File

@ -1,36 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
/**
* @requires PHP 8.0
*/
final class CompleteVarDocTypePropertyRectorTest 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/configured_rule.php';
}
}

View File

@ -1,54 +0,0 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
class Robocop
{
}
final class AssignConflict
{
private $eventDispatcher;
public function __construct(\EventDispatcher $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
public function run(Robocop $stdClass)
{
$this->eventDispatcher = $stdClass;
}
}
?>
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
class Robocop
{
}
final class AssignConflict
{
/**
* @var \EventDispatcher|\Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture\Robocop
*/
private $eventDispatcher;
public function __construct(\EventDispatcher $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
public function run(Robocop $stdClass)
{
$this->eventDispatcher = $stdClass;
}
}
?>

View File

@ -1,45 +0,0 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
final class DefaultValue
{
private $number = 5;
private $maybe = false;
private $name = 'John';
private $longName = 'Elton' . 'John';
}
?>
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
final class DefaultValue
{
/**
* @var int
*/
private $number = 5;
/**
* @var bool
*/
private $maybe = false;
/**
* @var string
*/
private $name = 'John';
/**
* @var string
*/
private $longName = 'Elton' . 'John';
}
?>

View File

@ -1,24 +0,0 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
final class DefaultValueArrayMixed
{
private $dreams = [];
}
?>
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
final class DefaultValueArrayMixed
{
/**
* @var mixed[]
*/
private $dreams = [];
}
?>

View File

@ -1,43 +0,0 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
class ImproveTypeAndKeepComment
{
/**
* @var string e.g. 123
*/
private $ids;
public function setIds()
{
$this->ids[] = 'hello';
if (random_int(1, 100)) {
$this->ids = 'hey';
}
}
}
?>
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
class ImproveTypeAndKeepComment
{
/**
* @var string[]|string|null e.g. 123
*/
private $ids;
public function setIds()
{
$this->ids[] = 'hello';
if (random_int(1, 100)) {
$this->ids = 'hey';
}
}
}
?>

View File

@ -1,37 +0,0 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
class ObjectCastToStdClass
{
/**
* @var object[]
*/
private static $services = [];
public static function register(array $service)
{
self::$services[] = (object) $service;
}
}
?>
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
class ObjectCastToStdClass
{
/**
* @var \stdClass[]
*/
private static $services = [];
public static function register(array $service)
{
self::$services[] = (object) $service;
}
}
?>

View File

@ -1,24 +0,0 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
class PrivateVarNullUnused
{
private $config = null;
}
?>
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
class PrivateVarNullUnused
{
/**
* @var null
*/
private $config = null;
}
?>

View File

@ -1,34 +0,0 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
final class PropertyAssign
{
private $eventDispatcher;
public function __construct(\EventDispatcher $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
}
?>
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
final class PropertyAssign
{
/**
* @var \EventDispatcher
*/
private $eventDispatcher;
public function __construct(\EventDispatcher $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
}
?>

View File

@ -1,38 +0,0 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
use stdClass;
class PublicVarNullUsed
{
public $config = null;
public function run()
{
$this->config = new stdClass;
}
}
?>
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
use stdClass;
class PublicVarNullUsed
{
/**
* @var stdClass|null
*/
public $config = null;
public function run()
{
$this->config = new stdClass;
}
}
?>

View File

@ -1,60 +0,0 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
class InvokableA {}
class InvokableB {}
class InvokableC {}
class FactoryA {}
class FactoryB {}
class FactoryC {}
final class RecursiveMultipleClassStringArray
{
public $services = [
'invokables' => [
InvokableA::class,
InvokableB::class,
InvokableC::class,
],
'factories' => [
FactoryA::class,
FactoryB::class,
FactoryC::class,
],
];
}
?>
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
class InvokableA {}
class InvokableB {}
class InvokableC {}
class FactoryA {}
class FactoryB {}
class FactoryC {}
final class RecursiveMultipleClassStringArray
{
/**
* @var array<string, mixed[]>
*/
public $services = [
'invokables' => [
InvokableA::class,
InvokableB::class,
InvokableC::class,
],
'factories' => [
FactoryA::class,
FactoryB::class,
FactoryC::class,
],
];
}
?>

View File

@ -1,18 +0,0 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
use Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Source\SomeService as SignalSlotDispatcher;
class SlotReplacement
{
/**
* @var SignalSlotDispatcher
*/
protected $signalSlotDispatcher;
public function __construct(SignalSlotDispatcher $signalSlotDispatcher)
{
$this->signalSlotDispatcher = $signalSlotDispatcher;
}
}

View File

@ -1,29 +0,0 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
use Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Source\SomeService;
class SkipMoreSpecific
{
/**
* @var SomeService[]
*/
private static $registry = [];
/**
* @param SomeService $service
*/
public static function register(SomeService $service)
{
self::$registry[$service->getLabel()] = $service;
}
/**
* @return SomeService[]
*/
public static function getRegisteredSomeServices()
{
return self::$registry;
}
}

View File

@ -1,21 +0,0 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
class A {}
class B {}
class C {}
final class SkipMultipleClassStringArray
{
/**
* @var class-string[]
*/
public $classes = [
A::class,
B::class,
C::class,
];
}
?>

View File

@ -1,8 +0,0 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
class SkipPublicVarNull
{
public $config = null;
}

View File

@ -1,52 +0,0 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
final class SomeCallableType
{
private $code;
/**
* @param callable $code A callable(InputInterface $input, OutputInterface $output)
*/
public function setCode(callable $code)
{
if ($code instanceof \Closure) {
$r = new \ReflectionFunction($code);
if (null === $r->getClosureThis()) {
$code = \Closure::bind($code, $this);
}
}
$this->code = $code;
return $this;
}
}
?>
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
final class SomeCallableType
{
/**
* @var callable|null
*/
private $code;
/**
* @param callable $code A callable(InputInterface $input, OutputInterface $output)
*/
public function setCode(callable $code)
{
if ($code instanceof \Closure) {
$r = new \ReflectionFunction($code);
if (null === $r->getClosureThis()) {
$code = \Closure::bind($code, $this);
}
}
$this->code = $code;
return $this;
}
}
?>

View File

@ -1,62 +0,0 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
use Symfony\Component\Console\Input\InputDefinition;
final class CommandDefinedInConstructor
{
private $definition;
public function __construct()
{
$this->definition = new InputDefinition();
}
/**
* @param array|InputDefinition $definition An array of argument and option instances or a definition instance
*/
public function setDefinition($definition)
{
if ($definition instanceof InputDefinition) {
$this->definition = $definition;
} else {
$this->definition->setDefinition($definition);
}
}
}
?>
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
use Symfony\Component\Console\Input\InputDefinition;
final class CommandDefinedInConstructor
{
/**
* @var \Symfony\Component\Console\Input\InputDefinition
*/
private $definition;
public function __construct()
{
$this->definition = new InputDefinition();
}
/**
* @param array|InputDefinition $definition An array of argument and option instances or a definition instance
*/
public function setDefinition($definition)
{
if ($definition instanceof InputDefinition) {
$this->definition = $definition;
} else {
$this->definition->setDefinition($definition);
}
}
}
?>

View File

@ -1,76 +0,0 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\HelperSet;
class SymfonyConsoleHelperSet
{
private $helperSet;
public function setApplication(Application $application = null)
{
$this->application = $application;
if ($application) {
$this->setHelperSet($application->getHelperSet());
} else {
$this->helperSet = null;
}
}
public function setHelperSet(HelperSet $helperSet)
{
$this->helperSet = $helperSet;
}
/**
* @return HelperSet
*/
public function getHelperSet()
{
return $this->helperSet;
}
}
?>
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\HelperSet;
class SymfonyConsoleHelperSet
{
/**
* @var \Symfony\Component\Console\Helper\HelperSet|null
*/
private $helperSet;
public function setApplication(Application $application = null)
{
$this->application = $application;
if ($application) {
$this->setHelperSet($application->getHelperSet());
} else {
$this->helperSet = null;
}
}
public function setHelperSet(HelperSet $helperSet)
{
$this->helperSet = $helperSet;
}
/**
* @return HelperSet
*/
public function getHelperSet()
{
return $this->helperSet;
}
}
?>

View File

@ -1,40 +0,0 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
class TypedArray
{
private $items;
/**
* @param string[] $items
*/
public function setItems(array $items)
{
$this->items = $items;
}
}
?>
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
class TypedArray
{
/**
* @var string[]|null
*/
private $items;
/**
* @param string[] $items
*/
public function setItems(array $items)
{
$this->items = $items;
}
}
?>

View File

@ -1,40 +0,0 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
class TypedArrayNested
{
private $itemsNested;
/**
* @param int[][]|bool[] $itemsNested
*/
public function setItemsNested(array $itemsNested)
{
$this->itemsNested = $itemsNested;
}
}
?>
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
class TypedArrayNested
{
/**
* @var int[][]|bool[]|null
*/
private $itemsNested;
/**
* @param int[][]|bool[] $itemsNested
*/
public function setItemsNested(array $itemsNested)
{
$this->itemsNested = $itemsNested;
}
}
?>

View File

@ -1,47 +0,0 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
final class WithNumericCheck
{
/**
* @var int|string
*/
public $value;
public function __construct($value = '')
{
if (is_numeric($value)) {
$this->value = $value;
return;
}
$this->value = 100;
}
}
?>
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
final class WithNumericCheck
{
/**
* @var float|int|string
*/
public $value;
public function __construct($value = '')
{
if (is_numeric($value)) {
$this->value = $value;
return;
}
$this->value = 100;
}
}
?>

View File

@ -1,18 +0,0 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\FixtureAutoImport;
use stdClass;
final class KeepSimpleClass
{
/**
* @var stdClass|null
*/
public $obj;
public function run()
{
$this->obj = new stdClass();
}
}

View File

@ -1,8 +0,0 @@
<?php
declare(strict_types=1);
final class EventDispatcher
{
}

View File

@ -1,10 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Source;
final class SomeService
{
}

View File

@ -1,15 +0,0 @@
<?php
declare(strict_types=1);
use Rector\Core\Configuration\Option;
use Rector\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::AUTO_IMPORT_NAMES, true);
$services = $containerConfigurator->services();
$services->set(CompleteVarDocTypePropertyRector::class);
};

View File

@ -1,11 +0,0 @@
<?php
declare(strict_types=1);
use Rector\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(CompleteVarDocTypePropertyRector::class);
};

View File

@ -1,92 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\TypeDeclaration\Rector\Property;
use PhpParser\Node;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\Rector\AbstractRector;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\CompleteVarDocTypePropertyRectorTest
*/
final class CompleteVarDocTypePropertyRector extends AbstractRector
{
public function __construct(
private readonly PropertyTypeInferer $propertyTypeInferer,
private readonly PhpDocTypeChanger $phpDocTypeChanger
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Complete property `@var` annotations or correct the old ones',
[
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
private $eventDispatcher;
public function __construct(EventDispatcher $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeClass
{
/**
* @var EventDispatcher
*/
private $eventDispatcher;
public function __construct(EventDispatcher $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Property::class];
}
/**
* @param Property $node
*/
public function refactor(Node $node): ?Node
{
$propertyType = $this->propertyTypeInferer->inferProperty($node);
if ($propertyType instanceof MixedType) {
return null;
}
if (! $node->isPrivate() && $propertyType instanceof NullType) {
return null;
}
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$this->phpDocTypeChanger->changeVarType($phpDocInfo, $propertyType);
return $node;
}
}