[TypeDeclaration] Skip nullable self referencing param and return on AddArrayParamDocTypeRector and ReturnTypeDeclarationRector (#1183)

* Adding tests based on examples

Demo examples:
- https://getrector.org/demo/1ec3fe09-f75d-6f66-8b0b-73ca8e804aa7
- https://getrector.org/demo/1ec3fe0a-4d37-63f6-a486-899a21bb210b

* handle MixedType

* phpstan

* fix subtype in union

* fix FuncCall node name

* fix serliaze on MixedType hashing

Co-authored-by: Marco Lipparini <developer@liarco.net>
Co-authored-by: Tomas Votruba <tomas.vot@gmail.com>
This commit is contained in:
Abdul Malik Ikhsan 2021-11-08 19:26:59 +07:00 committed by GitHub
parent 127428ac8a
commit e899eaaabe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 3 deletions

View File

@ -15,6 +15,7 @@ use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar;
use PhpParser\Node\Stmt\Class_;
@ -139,6 +140,13 @@ final class NodeTypeResolver
public function getType(Node $node): Type
{
if ($node instanceof NullableType) {
$type = $this->getType($node->type);
if (! $type instanceof MixedType) {
return new UnionType([$type, new NullType()]);
}
}
if ($node instanceof Ternary) {
$ternaryType = $this->resolveTernaryType($node);
if (! $ternaryType instanceof MixedType) {

View File

@ -30,7 +30,7 @@ final class TypeHasher
public function createTypeHash(Type $type): string
{
if ($type instanceof MixedType) {
return serialize($type) . $type->isExplicitMixed();
return $type->describe(VerbosityLevel::precise()) . $type->isExplicitMixed();
}
if ($type instanceof ArrayType) {

View File

@ -0,0 +1,16 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddArrayParamDocTypeRector\Fixture;
class SkipNullableSelfReferencingPropertySetter
{
private ?self $parent = null;
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
}
?>

View File

@ -0,0 +1,14 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture;
class SkipNullableSelfReferencingPropertyGetter
{
private ?self $parent = null;
public function getParent(): ?self
{
return $this->parent;
}
}
?>

View File

@ -39,8 +39,8 @@ final class DowngradeExponentialAssignmentOperatorRector extends AbstractRector
*/
public function refactor(Node $node): Assign
{
$powCall = $this->nodeFactory->createFuncCall('pow', [$node->var, $node->expr]);
$powFuncCall = $this->nodeFactory->createFuncCall('pow', [$node->var, $node->expr]);
return new Assign($node->var, $powCall);
return new Assign($node->var, $powFuncCall);
}
}

View File

@ -220,6 +220,12 @@ CODE_SAMPLE
return false;
}
// probably more/less strict union type on purpose
if ($currentType->isSubTypeOf($inferedType)
->yes()) {
return true;
}
return $inferedType->isSubTypeOf($currentType)
->yes();
}