mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-03 18:00:50 +00:00
96112cb1f0
2da49992cc
[Downgrade] [PHP 7.2] Make DowngradeParameterTypeWideningRector always downgrade to phpdoc type (#390)
99 lines
2.9 KiB
PHP
99 lines
2.9 KiB
PHP
<?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\PropertyTypeDeclarationRector\PropertyTypeDeclarationRectorTest
|
|
*/
|
|
final class PropertyTypeDeclarationRector extends \Rector\Core\Rector\AbstractRector
|
|
{
|
|
/**
|
|
* @var \Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer
|
|
*/
|
|
private $propertyTypeInferer;
|
|
/**
|
|
* @var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger
|
|
*/
|
|
private $phpDocTypeChanger;
|
|
public function __construct(\Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer $propertyTypeInferer, \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger $phpDocTypeChanger)
|
|
{
|
|
$this->propertyTypeInferer = $propertyTypeInferer;
|
|
$this->phpDocTypeChanger = $phpDocTypeChanger;
|
|
}
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
|
{
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Add @var to properties that are missing it', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
|
|
class SomeClass
|
|
{
|
|
private $value;
|
|
|
|
public function run()
|
|
{
|
|
$this->value = 123;
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
class SomeClass
|
|
{
|
|
/**
|
|
* @var int
|
|
*/
|
|
private $value;
|
|
|
|
public function run()
|
|
{
|
|
$this->value = 123;
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
)]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [\PhpParser\Node\Stmt\Property::class];
|
|
}
|
|
/**
|
|
* @param Property $node
|
|
*/
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
|
{
|
|
if (\count($node->props) !== 1) {
|
|
return null;
|
|
}
|
|
if ($node->type !== null) {
|
|
return null;
|
|
}
|
|
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
|
|
// is already set
|
|
foreach (['@var', '@phpstan-var', '@psalm-var'] as $tagName) {
|
|
$varType = $phpDocInfo->getVarType($tagName);
|
|
if (!$varType instanceof \PHPStan\Type\MixedType) {
|
|
return null;
|
|
}
|
|
}
|
|
$type = $this->propertyTypeInferer->inferProperty($node);
|
|
if ($type instanceof \PHPStan\Type\MixedType) {
|
|
return null;
|
|
}
|
|
if (!$node->isPrivate() && $type instanceof \PHPStan\Type\NullType) {
|
|
return null;
|
|
}
|
|
$this->phpDocTypeChanger->changeVarType($phpDocInfo, $type);
|
|
return $node;
|
|
}
|
|
}
|