Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DowngradePHP74] Fix parent removal regression #1081

Merged
merged 3 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions build/config/config-downgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@
PhpDocNodeVisitorInterface::class,
Node::class,
NodeNameResolverInterface::class,
// symfony
\Symfony\Component\String\AbstractString::class,
// phpstan
SourceLocator::class,
\PHPStan\PhpDocParser\Ast\Node::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
use PHPStan\PhpDocParser\Ast\Type\ThisTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\StaticType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\ValueObject\TypeKind;
use Rector\StaticTypeMapper\ValueObject\Type\SelfStaticType;

/**
* @see \Rector\Tests\NodeTypeResolver\StaticTypeMapper\StaticTypeMapperTest
Expand Down Expand Up @@ -50,7 +50,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type, TypeKind $typeKind): Type
*/
public function mapToPhpParserNode(Type $type, TypeKind $typeKind): ?Node
{
if ($type instanceof ThisType) {
if ($type instanceof SelfStaticType) {
return new Name(ObjectReference::SELF()->getValue());
}

Expand Down
9 changes: 8 additions & 1 deletion packages/StaticTypeMapper/PhpParser/NameNodeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Name;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
Expand All @@ -19,6 +20,7 @@
use PHPStan\Type\Type;
use Rector\Core\Configuration\RenamedClassesDataCollector;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\StaticTypeMapper\Contract\PhpParser\PhpParserNodeMapperInterface;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
Expand Down Expand Up @@ -83,7 +85,12 @@ private function createClassReferenceType(Name $name, string $reference): MixedT
}

if ($reference === ObjectReference::PARENT()->getValue()) {
return new ParentStaticType($classReflection);
$parentClassReflection = $classReflection->getParentClass();
if (! $parentClassReflection instanceof ClassReflection) {
throw new ShouldNotHappenException();
}

return new ParentStaticType($parentClassReflection);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samsonasik Here was the bug probably. The parent did not refere to parent class, but self class.

-return new ParentStaticType($classReflection);
+return new ParentStaticType($parentClassReflection);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomasVotruba thank you 👍

}

return new ThisType($classReflection);
Expand Down
11 changes: 11 additions & 0 deletions packages/StaticTypeMapper/ValueObject/Type/SelfStaticType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Rector\StaticTypeMapper\ValueObject\Type;

use PHPStan\Type\ThisType;

final class SelfStaticType extends ThisType
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Rector\Tests\DowngradePhp74\Rector\ClassMethod\DowngradeCovariantReturnTypeRector\Fixture;

use Rector\Tests\DowngradePhp74\Rector\ClassMethod\DowngradeCovariantReturnTypeRector\Source\ParentSymfonyString;

final class SkipParent extends ParentSymfonyString
{
protected $string = '';

public function upper(): parent
{
$str = clone $this;
$str->string = strtoupper($str->string);

return $str;
}

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

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp74\Rector\ClassMethod\DowngradeCovariantReturnTypeRector\Source;

abstract class ParentSymfonyString
{
/**
* @return static
*/
abstract public function upper(): self;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,3 @@ final class StaticObject
return $this;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\FixtureForPhp80;

final class StaticObject
{
/**
* @var object
*/
protected $obj;

public function get($obj = null): object|self
{
if (func_num_args() === 0) {
return $this->obj;
}

$this->obj = $obj;
return $this;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class StaticDocBlock
/**
* @return static
*/
public function getSelf(): self
public function getSelf(): static
{
return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\BetterPhpDocParser\PhpDocParser\PhpDocFromTypeDeclarationDecorator;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\StaticTypeMapper\ValueObject\Type\ParentStaticType;
Expand Down Expand Up @@ -93,8 +94,12 @@ public function refactor(Node $node): ?Node
return null;
}

$parentStaticType = new ParentStaticType($classReflection);
$parentClassReflection = $classReflection->getParentClass();
if (! $parentClassReflection instanceof ClassReflection) {
throw new ShouldNotHappenException();
}

$parentStaticType = new ParentStaticType($parentClassReflection);
if (! $this->phpDocFromTypeDeclarationDecorator->decorateReturnWithSpecificType($node, $parentStaticType)) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Rector\DeadCode\PhpDoc\TagRemover\ReturnTagRemover;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PHPStanStaticTypeMapper\ValueObject\TypeKind;
use Rector\StaticTypeMapper\ValueObject\Type\ParentStaticType;
use Symplify\PackageBuilder\Reflection\PrivatesCaller;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -174,13 +175,42 @@ private function resolveDifferentAncestorReturnType(
}

$bareReturnType = $returnTypeNode instanceof NullableType ? $returnTypeNode->type : $returnTypeNode;

$returnType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($bareReturnType);

$methodName = $this->getName($classMethod);

/** @var ClassReflection[] $parentClassesAndInterfaces */
$parentClassesAndInterfaces = array_merge($classReflection->getParents(), $classReflection->getInterfaces());

return $this->resolveMatchingReturnType($parentClassesAndInterfaces, $methodName, $classMethod, $returnType);
}

private function addDocBlockReturn(ClassMethod $classMethod): void
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);

// keep return type if already set one
if (! $phpDocInfo->getReturnType() instanceof MixedType) {
return;
}

/** @var Node $returnType */
$returnType = $classMethod->returnType;
$type = $this->staticTypeMapper->mapPhpParserNodePHPStanType($returnType);

$this->phpDocTypeChanger->changeReturnType($phpDocInfo, $type);
$this->returnTagRemover->removeReturnTagIfUseless($phpDocInfo, $classMethod);
}

/**
* @param ClassReflection[] $parentClassesAndInterfaces
*/
private function resolveMatchingReturnType(
array $parentClassesAndInterfaces,
string $methodName,
ClassMethod $classMethod,
\PHPStan\Type\Type $returnType
): Type {
foreach ($parentClassesAndInterfaces as $parentClassAndInterface) {
$parentClassAndInterfaceHasMethod = $parentClassAndInterface->hasMethod($methodName);
if (! $parentClassAndInterfaceHasMethod) {
Expand All @@ -201,6 +231,13 @@ private function resolveDifferentAncestorReturnType(
[]
);

// skip "parent" reference if correct
if ($returnType instanceof ParentStaticType) {
if ($parentReturnType->accepts($returnType, true)->yes()) {
continue;
}
}

if ($parentReturnType->equals($returnType)) {
continue;
}
Expand All @@ -211,21 +248,4 @@ private function resolveDifferentAncestorReturnType(

return new MixedType();
}

private function addDocBlockReturn(ClassMethod $classMethod): void
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);

// keep return type if already set one
if (! $phpDocInfo->getReturnType() instanceof MixedType) {
return;
}

/** @var Node $returnType */
$returnType = $classMethod->returnType;
$type = $this->staticTypeMapper->mapPhpParserNodePHPStanType($returnType);

$this->phpDocTypeChanger->changeReturnType($phpDocInfo, $type);
$this->returnTagRemover->removeReturnTagIfUseless($phpDocInfo, $classMethod);
}
}
5 changes: 3 additions & 2 deletions rules/TypeDeclaration/PHPStan/Type/ObjectTypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\NonExistingObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\ParentStaticType;
use Rector\StaticTypeMapper\ValueObject\Type\SelfObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\ShortenedGenericObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType;
Expand Down Expand Up @@ -250,7 +251,7 @@ private function matchClassWithLastUseImportPart(ObjectType $objectType, UseUse
private function resolveObjectReferenceType(
Scope $scope,
string $classReferenceValue
): StaticType|FullyQualifiedObjectType|SelfObjectType {
): StaticType|SelfObjectType {
$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
throw new ShouldNotHappenException();
Expand All @@ -270,7 +271,7 @@ private function resolveObjectReferenceType(
throw new ShouldNotHappenException();
}

return new FullyQualifiedObjectType($parentClassReflection->getName(), null, $parentClassReflection);
return new ParentStaticType($parentClassReflection);
}

throw new ShouldNotHappenException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StaticType;
use PHPStan\Type\ThisType;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\PHPStanStaticTypeMapper\ValueObject\TypeKind;
use Rector\StaticTypeMapper\ValueObject\Type\SelfStaticType;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -139,7 +139,7 @@ private function createObjectTypeFromNew(New_ $new): ObjectType|StaticType
}

if ($className === ObjectReference::SELF()->getValue()) {
return new ThisType($classReflection);
return new SelfStaticType($classReflection);
}

return new StaticType($classReflection);
Expand Down