From f610206257decf344d7dd43889517315fdb6e470 Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Sun, 5 Jan 2020 19:33:35 +0100 Subject: [PATCH] Generate no return when empty return statement --- ...llableThisArrayToAnonymousFunctionRector.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/CodeQuality/src/Rector/Array_/CallableThisArrayToAnonymousFunctionRector.php b/packages/CodeQuality/src/Rector/Array_/CallableThisArrayToAnonymousFunctionRector.php index b48878f3a3a..f2a356905a9 100644 --- a/packages/CodeQuality/src/Rector/Array_/CallableThisArrayToAnonymousFunctionRector.php +++ b/packages/CodeQuality/src/Rector/Array_/CallableThisArrayToAnonymousFunctionRector.php @@ -188,7 +188,7 @@ PHP */ private function createAnonymousFunction(ClassMethod $classMethod, Node $node): Closure { - $hasClassMethodReturn = $this->betterNodeFinder->findInstanceOf((array) $classMethod->stmts, Return_::class); + $classMethodReturns = $this->betterNodeFinder->findInstanceOf((array) $classMethod->stmts, Return_::class); $anonymousFunction = new Closure(); $newParams = $this->copyParams($classMethod->params); @@ -205,7 +205,7 @@ PHP } // does method return something? - if ($hasClassMethodReturn !== []) { + if ($this->hasClassMethodReturn($classMethodReturns)) { $anonymousFunction->stmts[] = new Return_($innerMethodCall); } else { $anonymousFunction->stmts[] = new Expression($innerMethodCall); @@ -220,6 +220,19 @@ PHP return $anonymousFunction; } + /** + * @param Return_[] $nodes + */ + private function hasClassMethodReturn(array $nodes): bool + { + foreach($nodes as $node) { + if ($node->expr !== null) { + return true; + } + } + return false; + } + /** * @param Param[] $params * @return Arg[]