2021-01-18 12:22:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-01-20 15:24:57 +00:00
|
|
|
namespace Rector\CodeQuality\Rector\Identical;
|
2021-01-18 12:22:26 +00:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Identical;
|
|
|
|
use PhpParser\Node\Expr\BooleanNot;
|
|
|
|
use PhpParser\Node\Expr\Instanceof_;
|
|
|
|
use PhpParser\Node\Name\FullyQualified;
|
|
|
|
use PhpParser\Node\Stmt\Expression;
|
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
|
2021-01-19 11:24:48 +00:00
|
|
|
use PHPStan\Type\NullType;
|
2021-01-20 11:41:35 +00:00
|
|
|
use PHPStan\Type\ObjectType;
|
2021-01-19 11:24:48 +00:00
|
|
|
use PHPStan\Type\Type;
|
|
|
|
use PHPStan\Type\UnionType;
|
2021-01-18 12:22:26 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
2021-01-19 19:45:30 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
|
2021-01-18 12:22:26 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2021-01-19 11:24:48 +00:00
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
|
2021-01-18 12:22:26 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
|
|
|
|
|
|
/**
|
2021-01-20 15:24:57 +00:00
|
|
|
* @see \Rector\CodeQuality\Tests\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector\FlipTypeControlToUseExclusiveTypeRectorTest
|
2021-01-18 12:22:26 +00:00
|
|
|
*/
|
|
|
|
final class FlipTypeControlToUseExclusiveTypeRector extends AbstractRector
|
|
|
|
{
|
2021-01-19 19:45:30 +00:00
|
|
|
/**
|
|
|
|
* @var PhpDocTagRemover
|
|
|
|
*/
|
|
|
|
private $phpDocTagRemover;
|
|
|
|
|
|
|
|
public function __construct(PhpDocTagRemover $phpDocTagRemover)
|
|
|
|
{
|
|
|
|
$this->phpDocTagRemover = $phpDocTagRemover;
|
|
|
|
}
|
|
|
|
|
2021-01-18 12:22:26 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
|
|
|
{
|
|
|
|
return new RuleDefinition(
|
|
|
|
'Flip type control to use exclusive type',
|
|
|
|
[
|
|
|
|
new CodeSample(
|
|
|
|
<<<'CODE_SAMPLE'
|
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function __construct(array $values)
|
|
|
|
{
|
|
|
|
/** @var PhpDocInfo|null $phpDocInfo */
|
|
|
|
$phpDocInfo = $functionLike->getAttribute(AttributeKey::PHP_DOC_INFO);
|
|
|
|
if ($phpDocInfo === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
,
|
|
|
|
<<<'CODE_SAMPLE'
|
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function __construct(array $values)
|
|
|
|
{
|
|
|
|
$phpDocInfo = $functionLike->getAttribute(AttributeKey::PHP_DOC_INFO);
|
|
|
|
if (! $phpDocInfo instanceof PhpDocInfo) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
),
|
|
|
|
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [Identical::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Identical $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
|
|
|
if (! $this->isNull($node->left) && ! $this->isNull($node->right)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$variable = $this->isNull($node->left)
|
|
|
|
? $node->right
|
|
|
|
: $node->left;
|
|
|
|
|
|
|
|
$assign = $this->getVariableAssign($node, $variable);
|
|
|
|
if (! $assign instanceof Assign) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$expression = $assign->getAttribute(AttributeKey::PARENT_NODE);
|
|
|
|
if (! $expression instanceof Expression) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-01-18 21:45:13 +00:00
|
|
|
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($expression);
|
2021-01-19 11:24:48 +00:00
|
|
|
$type = $phpDocInfo->getVarType();
|
2021-01-18 12:22:26 +00:00
|
|
|
|
2021-01-20 11:41:35 +00:00
|
|
|
if (! $type instanceof UnionType) {
|
|
|
|
$type = $this->getObjectType($assign->expr);
|
|
|
|
}
|
|
|
|
|
2021-01-19 11:24:48 +00:00
|
|
|
if (! $type instanceof UnionType) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var Type[] $types */
|
|
|
|
$types = $this->getTypes($type);
|
2021-01-20 15:24:57 +00:00
|
|
|
if ($this->isNotNullOneOf($types)) {
|
2021-01-18 12:22:26 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->processConvertToExclusiveType($types, $variable, $phpDocInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-19 11:24:48 +00:00
|
|
|
* @param Type[] $types
|
2021-01-18 12:22:26 +00:00
|
|
|
*/
|
|
|
|
private function processConvertToExclusiveType(array $types, Expr $expr, PhpDocInfo $phpDocInfo): ?BooleanNot
|
|
|
|
{
|
2021-01-19 11:24:48 +00:00
|
|
|
$type = $types[0] instanceof NullType
|
|
|
|
? $types[1]
|
|
|
|
: $types[0];
|
2021-01-18 12:22:26 +00:00
|
|
|
|
2021-01-20 11:41:35 +00:00
|
|
|
if (! $type instanceof FullyQualifiedObjectType && ! $type instanceof ObjectType) {
|
2021-01-18 12:22:26 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$tagValueNode = $phpDocInfo->getVarTagValueNode();
|
2021-01-20 11:41:35 +00:00
|
|
|
if ($tagValueNode instanceof VarTagValueNode) {
|
|
|
|
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $tagValueNode);
|
|
|
|
}
|
2021-01-19 19:45:30 +00:00
|
|
|
|
2021-01-19 11:24:48 +00:00
|
|
|
return new BooleanNot(new Instanceof_($expr, new FullyQualified($type->getClassName())));
|
2021-01-18 12:22:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-19 11:24:48 +00:00
|
|
|
* @return Type[]
|
2021-01-18 12:22:26 +00:00
|
|
|
*/
|
2021-01-19 11:24:48 +00:00
|
|
|
private function getTypes(UnionType $unionType): array
|
2021-01-18 12:22:26 +00:00
|
|
|
{
|
2021-01-19 11:24:48 +00:00
|
|
|
$types = $unionType->getTypes();
|
|
|
|
if (count($types) > 2) {
|
2021-01-18 12:22:26 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2021-01-19 11:24:48 +00:00
|
|
|
return $types;
|
2021-01-18 12:22:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getVariableAssign(Identical $identical, Expr $expr): ?Node
|
|
|
|
{
|
|
|
|
return $this->betterNodeFinder->findFirstPrevious($identical, function (Node $node) use ($expr): bool {
|
|
|
|
if (! $node instanceof Assign) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->areNodesEqual($node->var, $expr);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-19 11:24:48 +00:00
|
|
|
* @param Type[] $types
|
2021-01-18 12:22:26 +00:00
|
|
|
*/
|
2021-01-20 15:24:57 +00:00
|
|
|
private function isNotNullOneOf(array $types): bool
|
2021-01-18 12:22:26 +00:00
|
|
|
{
|
|
|
|
if ($types === []) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-19 11:24:48 +00:00
|
|
|
if ($types[0] === $types[1]) {
|
2021-01-18 12:22:26 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-01-19 11:24:48 +00:00
|
|
|
if ($types[0] instanceof NullType) {
|
2021-01-18 12:22:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-01-20 15:24:57 +00:00
|
|
|
|
2021-01-19 11:24:48 +00:00
|
|
|
return ! $types[1] instanceof NullType;
|
2021-01-18 12:22:26 +00:00
|
|
|
}
|
|
|
|
}
|