mirror of
https://github.com/rectorphp/rector.git
synced 2024-08-12 19:40:23 +00:00
[CodingStyle] Add ArrayPropertyDefaultValueRector
This commit is contained in:
parent
7cb8a6916b
commit
0174637de9
@ -20,3 +20,4 @@ services:
|
||||
Rector\CodingStyle\Rector\FuncCall\ConsistentPregDelimiterRector: ~
|
||||
Rector\CodingStyle\Rector\Include_\FollowRequireByDirRector: ~
|
||||
Rector\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector: ~
|
||||
Rector\CodingStyle\Rector\Property\ArrayPropertyDefaultValueRector: ~
|
||||
|
@ -0,0 +1,97 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Rector\Property;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\PropertyProperty;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
final class ArrayPropertyDefaultValueRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var DocBlockManipulator
|
||||
*/
|
||||
private $docBlockManipulator;
|
||||
|
||||
public function __construct(DocBlockManipulator $docBlockManipulator)
|
||||
{
|
||||
$this->docBlockManipulator = $docBlockManipulator;
|
||||
}
|
||||
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Array property should have default value, to prevent undefined array issues', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
/**
|
||||
* @var int[]
|
||||
*/
|
||||
private $items;
|
||||
|
||||
public function run()
|
||||
{
|
||||
foreach ($items as $item) {
|
||||
}
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
/**
|
||||
* @var int[]
|
||||
*/
|
||||
private $items = [];
|
||||
|
||||
public function run()
|
||||
{
|
||||
foreach ($items as $item) {
|
||||
}
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [PropertyProperty::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PropertyProperty $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($node->default) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var Node\Stmt\Property $parentNode */
|
||||
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
|
||||
$varTypeInfo = $this->docBlockManipulator->getVarTypeInfo($parentNode);
|
||||
if ($varTypeInfo === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $varTypeInfo->isIterable()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$node->default = new Node\Expr\Array_();
|
||||
|
||||
return $node;
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Property\ArrayPropertyDefaultValueRector;
|
||||
|
||||
use Rector\CodingStyle\Rector\Property\ArrayPropertyDefaultValueRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class ArrayPropertyDefaultValueRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles([
|
||||
__DIR__ . '/Fixture/fixture.php.inc',
|
||||
__DIR__ . '/Fixture/skip.php.inc',
|
||||
__DIR__ . '/Fixture/static_property.php.inc',
|
||||
]);
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return ArrayPropertyDefaultValueRector::class;
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Property\ArrayPropertyDefaultValueRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
/**
|
||||
* @var int[]
|
||||
*/
|
||||
private $items;
|
||||
|
||||
/**
|
||||
* @var array|SomeEntity[]
|
||||
*/
|
||||
public $entities;
|
||||
|
||||
/**
|
||||
* @var Bar[]|Foo[]
|
||||
*/
|
||||
private $combined;
|
||||
|
||||
public function run()
|
||||
{
|
||||
foreach ($items as $item) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Property\ArrayPropertyDefaultValueRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
/**
|
||||
* @var int[]
|
||||
*/
|
||||
private $items = [];
|
||||
|
||||
/**
|
||||
* @var array|SomeEntity[]
|
||||
*/
|
||||
public $entities = [];
|
||||
|
||||
/**
|
||||
* @var Bar[]|Foo[]
|
||||
*/
|
||||
private $combined = [];
|
||||
|
||||
public function run()
|
||||
{
|
||||
foreach ($items as $item) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Property\ArrayPropertyDefaultValueRector\Fixture;
|
||||
|
||||
class Skip
|
||||
{
|
||||
/**
|
||||
* @var Collection|Foo[]
|
||||
*/
|
||||
private $foo;
|
||||
|
||||
/**
|
||||
* @var ArrayObject|int[]
|
||||
*/
|
||||
public $property;
|
||||
|
||||
/**
|
||||
* @var SplObjectStorage|int[]
|
||||
*/
|
||||
public $anotherProperty;
|
||||
|
||||
/**
|
||||
* @var \ArrayObject|int[]
|
||||
*/
|
||||
public $propertyWithPreslash;
|
||||
|
||||
/**
|
||||
* @var \SplObjectStorage|int[]
|
||||
*/
|
||||
public $anotherPropertyWithPreslash;
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Property\ArrayPropertyDefaultValueRector\Fixture;
|
||||
|
||||
class StaticProperty
|
||||
{
|
||||
/** @var int[] */
|
||||
public static $property;
|
||||
|
||||
/** @var int[][] */
|
||||
public $doubleArray;
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Property\ArrayPropertyDefaultValueRector\Fixture;
|
||||
|
||||
class StaticProperty
|
||||
{
|
||||
/** @var int[] */
|
||||
public static $property = [];
|
||||
|
||||
/** @var int[][] */
|
||||
public $doubleArray = [];
|
||||
}
|
||||
|
||||
?>
|
@ -48,4 +48,19 @@ final class VarTypeInfo extends AbstractTypeInfo
|
||||
{
|
||||
return $this->fqnTypes[0] ?? null;
|
||||
}
|
||||
|
||||
public function isIterable(): bool
|
||||
{
|
||||
if ($this->types === []) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($this->types as $type) {
|
||||
if ($type !== 'array') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user