Skip to content

Commit

Permalink
[TypeDeclaration] Skip once typed and else return array_map() on Retu…
Browse files Browse the repository at this point in the history
…rnTypeFromStrictTypedCallRector (#4312)

* Add missing test case for native call

When using a native call and returning first, here `array_map`, the `ReturnTypeFromStrictTypedCallRector` uses it directly and doesn't check that later `return` might return another type.

* [TypeDeclaration] Skip once typed and else return array_map() on ReturnTypeFromStrictTypedCallRector

---------

Co-authored-by: SelrahcD <[email protected]>
  • Loading branch information
samsonasik and SelrahcD authored Jun 21, 2023
1 parent df459e7 commit 1179698
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector\Fixture;

final class SkipOnceTypeAndNativeCallFirst
{
public function getData()
{
if (false) {
return array_map(function ($i) {
return $i;
}, []);
}

return $this->getString();
}

private function getString()
{
return '...';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector\Fixture;

final class SkipOnceTypeAndNativeCallSecond
{
public function getData()
{
if (false) {
return $this->getString();
}

return array_map(function ($i) {
return $i;
}, []);
}

private function getString()
{
return '...';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ private function findCurrentScopeReturns(ClassMethod|Function_|Closure $node): a
$this->traverseNodesWithCallable($node->stmts, static function (Node $node) use (&$currentScopeReturns): ?int {
// skip scope nesting
if ($node instanceof FunctionLike) {
return NodeTraverser::STOP_TRAVERSAL;
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}

if (! $node instanceof Return_) {
Expand Down

0 comments on commit 1179698

Please sign in to comment.