From 9a9adbd263a39052a19761ff2bf28529543af984 Mon Sep 17 00:00:00 2001 From: Nikolaos Dimopoulos Date: Mon, 27 Nov 2023 10:55:32 -0600 Subject: [PATCH] sonarlint suggestions --- src/Class/Method/Method.php | 23 +++++++++++++---------- src/Statements/ReturnStatement.php | 11 +++++++---- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/Class/Method/Method.php b/src/Class/Method/Method.php index fbc9ec23e..1b68b3f96 100644 --- a/src/Class/Method/Method.php +++ b/src/Class/Method/Method.php @@ -1841,16 +1841,19 @@ public function compile(CompilationContext $compilationContext): void */ $lastType = $this->statements->getLastStatementType(); - if ('return' !== $lastType && 'throw' !== $lastType && !$this->hasChildReturnStatementType($statement)) { - /** - * If a method has return-type hints we need to ensure the last statement is a 'return' statement - */ - if ($this->hasReturnTypes()) { - throw new CompilerException( - 'Reached end of the method without returning a valid type specified in the return-type hints', - $this->expression['return-type'] - ); - } + /** + * If a method has return-type hints we need to ensure the last + * statement is a 'return' statement + */ + if ( + 'return' !== $lastType && + 'throw' !== $lastType && !$this->hasChildReturnStatementType($statement) && + $this->hasReturnTypes() + ) { + throw new CompilerException( + 'Reached end of the method without returning a valid type specified in the return-type hints', + $this->expression['return-type'] + ); } } diff --git a/src/Statements/ReturnStatement.php b/src/Statements/ReturnStatement.php index 51bd7960a..75a5567f7 100644 --- a/src/Statements/ReturnStatement.php +++ b/src/Statements/ReturnStatement.php @@ -13,6 +13,7 @@ namespace Zephir\Statements; +use ReflectionException; use Zephir\CompilationContext; use Zephir\Exception; use Zephir\Exception\CompilerException; @@ -26,11 +27,13 @@ */ final class ReturnStatement extends StatementAbstract { + private const RETURN_RETURN = 'return;'; + /** * @param CompilationContext $compilationContext * * @throws Exception - * @throws \ReflectionException + * @throws ReflectionException */ public function compile(CompilationContext $compilationContext): void { @@ -222,7 +225,7 @@ public function compile(CompilationContext $compilationContext): void if ('return_value' != $resolvedExpr->getCode()) { $codePrinter->output('RETURN_CTOR('.$resolvedExpr->getCode().');'); } else { - $codePrinter->output('return;'); + $codePrinter->output(self::RETURN_RETURN); } break; @@ -300,10 +303,10 @@ public function compile(CompilationContext $compilationContext): void $compilationContext->backend->getVariableCode($symbolVariable) ) ); - $codePrinter->output('return;'); + $codePrinter->output(self::RETURN_RETURN); } } else { - $codePrinter->output('return;'); + $codePrinter->output(self::RETURN_RETURN); } } if ($symbolVariable->isTemporal()) {