Skip to content

Commit

Permalink
[TypeDeclaration] Handle regression multiple params no longer working…
Browse files Browse the repository at this point in the history
… on AddMethodCallBasedStrictParamTypeRector (#3681)

* [TypeDeclaration] Handle regression multiple params no longer working on AddMethodCallBasedStrictParamTypeRector

* rollback tweak

* try fixing

* try with check has bool type

* skip narrow bool|false|<OtherTypeHere>
  • Loading branch information
samsonasik committed Apr 24, 2023
1 parent 52e213f commit 2efca73
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,6 @@ private function narrowBoolType(
return $phpParserUnionType;
}

return $this->phpStanStaticTypeMapper->mapToPhpParserNode($unionType, $typeKind);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

class NarrowBoolFalse2
class SkipNarrowBoolFalseAnotherType
{
/**
* @param bool|int|false $param
Expand All @@ -17,23 +17,3 @@ class NarrowBoolFalse2
return 1;
}
}

?>
-----
<?php

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

class NarrowBoolFalse2
{
public function go(bool|int $param): bool|int
{
if (rand(0, 1)) {
return rand(0, 1) ? true : false;
}

return 1;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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\LNumber;
use PhpParser\Node\Scalar\String_;

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

private function someExpr($expr, $staticCall)
{
}
}

?>
-----
<?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\LNumber;
use PhpParser\Node\Scalar\String_;

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

private function someExpr(\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Scalar\String_ $expr, \PhpParser\Node\Expr\StaticCall $staticCall)
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,8 @@ public function __construct(
public function complete(ClassMethod $classMethod, array $classParameterTypes, int $maxUnionTypes): ?ClassMethod
{
$hasChanged = false;
$totalTypes = count($classParameterTypes);

foreach ($classParameterTypes as $position => $argumentStaticType) {
if ($totalTypes > 1 && $argumentStaticType instanceof UnionType) {
return null;
}

/** @var Type $argumentStaticType */
if ($this->shouldSkipArgumentStaticType($classMethod, $argumentStaticType, $position, $maxUnionTypes)) {
continue;
Expand Down

0 comments on commit 2efca73

Please sign in to comment.