Skip to content

Commit

Permalink
Revert Fix $this/self docblock compare removed on on-final class, as …
Browse files Browse the repository at this point in the history
…it can refer to child this type (#1246) (#1669)

* Revert Fix $this/self docblock compare removed on on-final class, as it can refer to child this type (#1246)

This reverts commit a2c2979

* Fix handling of $this return type in UnionTypes rule

`$this` refers to the exact object identity, not just the same type. Therefore, its valid and should not be removed
@see https://wiki.php.net/rfc/this_return_type for more context
  • Loading branch information
simPod committed Jan 17, 2022
1 parent 65c97a9 commit 7d7edc4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 30 deletions.
21 changes: 7 additions & 14 deletions packages/NodeTypeResolver/TypeComparator/TypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
namespace Rector\NodeTypeResolver\TypeComparator;

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;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
Expand All @@ -23,7 +21,6 @@
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;
Expand Down Expand Up @@ -272,17 +269,13 @@ private function areTypesSameWithLiteralTypeInPhpDoc(

private function isThisTypeInFinalClass(Type $phpStanDocType, Type $phpParserNodeType, Node $node): bool
{
// 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);
if ($scope instanceof Scope) {
$classReflection = $scope->getClassReflection();
if ($classReflection instanceof ClassReflection) {
return $classReflection->isFinal();
}
}
}

return true;
/**
* Special case for $this/(self|static) compare
*
* $this refers to the exact object identity, not just the same type. Therefore, it's valid and should not be removed
* @see https://wiki.php.net/rfc/this_return_type for more context
*/
return ! ($phpStanDocType instanceof ThisType && $phpParserNodeType instanceof StaticType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,3 @@ final class SkipUnionSelfThisFinalClass
return $this;
}
}

?>
-----
<?php

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

final class SkipUnionSelfThisFinalClass
{
public function withFromId(int $fromId): self
{
return $this;
}
}

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

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

class SkipUnionStaticThisClass
{
/**
* @return $this
*/
public function withFromId(int $fromId): static
{
return $this;
}
}

0 comments on commit 7d7edc4

Please sign in to comment.