Skip to content

Commit

Permalink
Skip union if parent type in ClassMethodParamTypeCompleter (#1103)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Oct 29, 2021
1 parent b8a1d72 commit 99f0681
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class NarrowUnion
$this->someExpr($string);
}

private function someExpr(Expr $expr)
private function someExpr($expr)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\FixtureUnion;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Scalar\String_;

final class SkipUnionIfParent
{
public function run(MethodCall $methodCall, StaticCall $staticCall, String_ $string)
{
$this->someExpr($methodCall);
$this->someExpr($staticCall);
$this->someExpr($string);
}

private function someExpr(Expr $expr)
{
}
}
2 changes: 1 addition & 1 deletion rules/TypeDeclaration/NodeAnalyzer/CallTypesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(

/**
* @param MethodCall[]|StaticCall[]|ArrayCallable[] $calls
* @return Type[]
* @return array<int, Type>
*/
public function resolveStrictTypesFromCalls(array $calls): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ private function shouldSkipArgumentStaticType(
return true;
}

// current type already accepts the one added
if ($currentParameterStaticType->accepts($argumentStaticType, true)->yes()) {
return true;
}

// avoid overriding more precise type
if ($argumentStaticType->isSuperTypeOf($currentParameterStaticType)->yes()) {
return true;
Expand Down

0 comments on commit 99f0681

Please sign in to comment.