Skip to content

Commit

Permalink
Add edge cases to skip (#2604)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jul 1, 2022
1 parent 89a7a4d commit 12f380b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\Fixture;

function selfsubstr(): string
{
return 'hi';
}

final class SkipEmptyReturn
{
public function run($value)
{
if ($value) {
return true;
}

return selfsubstr('warning', 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\Fixture;

final class SkipEmptyReturn
{
public function run($value)
{
if ($value) {
return;
}

return substr('warning', 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public function refactor(Node $node): ?Node
}

$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($scalarReturnType, TypeKind::RETURN);
if (! $returnTypeNode instanceof Node) {
return null;
}

$node->returnType = $returnTypeNode;
return $node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,8 @@ public function matchStrictScalarExpr(Expr $expr): ?Type
return null;
}

if ($expr instanceof FuncCall && $expr->name instanceof Name) {
$functionReflection = $this->reflectionProvider->getFunction($expr->name, null);
if (! $functionReflection instanceof NativeFunctionReflection) {
return null;
}

$parametersAcceptor = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants());
return $parametersAcceptor->getReturnType();
if ($expr instanceof FuncCall) {
return $this->resolveFuncCallType($expr);
}

return null;
Expand Down Expand Up @@ -87,4 +81,23 @@ private function resolveTypeFromScalar(Scalar $scalar): Type|null

return null;
}

private function resolveFuncCallType(FuncCall $funcCall): ?Type
{
if (! $funcCall->name instanceof Name) {
return null;
}

if (! $this->reflectionProvider->hasFunction($funcCall->name, null)) {
return null;
}

$functionReflection = $this->reflectionProvider->getFunction($funcCall->name, null);
if (! $functionReflection instanceof NativeFunctionReflection) {
return null;
}

$parametersAcceptor = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants());
return $parametersAcceptor->getReturnType();
}
}

0 comments on commit 12f380b

Please sign in to comment.