Skip to content

Commit

Permalink
Prevent UnionTypes rule from removing @param static in favor fo `se…
Browse files Browse the repository at this point in the history
…lf`-typed $argument (#1667)
  • Loading branch information
simPod committed Jan 12, 2022
1 parent fd34e6c commit a82a18e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/NodeTypeResolver/TypeComparator/TypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ArrayType;
Expand All @@ -15,15 +16,18 @@
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StaticType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\UnionType;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\NodeTypeResolver\PHPStan\TypeHasher;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\SelfStaticType;
use Rector\TypeDeclaration\TypeNormalizer;

final class TypeComparator
Expand Down Expand Up @@ -96,6 +100,10 @@ public function arePhpParserAndPhpStanPhpDocTypesEqual(
return false;
}

if ($this->isTypeSelfAndDocParamTypeStatic($phpStanDocType, $phpParserNodeType, $phpStanDocTypeNode)) {
return false;
}

// special case for non-final $this/self compare; in case of interface/abstract class, it can be another $this
if ($phpStanDocType instanceof ThisType && $phpParserNodeType instanceof ThisType) {
$scope = $node->getAttribute(AttributeKey::SCOPE);
Expand Down Expand Up @@ -247,4 +255,11 @@ private function normalizeConstantBooleanType(Type $type): Type
return $callable($type);
});
}

private function isTypeSelfAndDocParamTypeStatic(Type $phpStanDocType, Type $phpParserNodeType, TypeNode $phpStanDocTypeNode) : bool
{
return $phpStanDocType instanceof StaticType
&& $phpParserNodeType instanceof ThisType
&& $phpStanDocTypeNode->getAttribute(PhpDocAttributeKey::PARENT) instanceof ParamTagValueNode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\Php80\Rector\FunctionLike\UnionTypesRector\Fixture;

class ParamSelfReturnStatic
{
/**
* @param self $arg
* @return static
*/
public function go(self $arg): static
{
return $this;
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\FunctionLike\UnionTypesRector\Fixture;

class ParamSelfReturnStatic
{
public function go(self $arg): static
{
return $this;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Php80\Rector\FunctionLike\UnionTypesRector\Fixture;

class SkipStaticParam
{
/**
* @param static $arg
*/
public function go(self $arg1)
{
}
}

0 comments on commit a82a18e

Please sign in to comment.