mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-03 18:00:50 +00:00
[TypeDeclaration] Remove CompleteVarDocTypePropertyRector, is handled by split rules (#1500)
This commit is contained in:
parent
bfe8d995b7
commit
3417ff434b
|
@ -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
|
||||
|
|
|
@ -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"
|
||||
]
|
||||
},
|
||||
|
|
|
@ -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';
|
||||
}
|
||||
}
|
|
@ -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';
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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';
|
||||
}
|
||||
|
||||
?>
|
|
@ -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 = [];
|
||||
}
|
||||
|
||||
?>
|
|
@ -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';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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,
|
||||
];
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,8 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
|
||||
|
||||
class SkipPublicVarNull
|
||||
{
|
||||
public $config = null;
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
final class EventDispatcher
|
||||
{
|
||||
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Source;
|
||||
|
||||
final class SomeService
|
||||
{
|
||||
|
||||
}
|
|
@ -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);
|
||||
};
|
|
@ -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);
|
||||
};
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user