mirror of
https://github.com/rectorphp/rector.git
synced 2024-07-04 08:33:33 +00:00
[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:
parent
127428ac8a
commit
e899eaaabe
|
@ -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) {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
?>
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user