From 7de49bb4a8efa15ee5a98aac6237902925bdf56b Mon Sep 17 00:00:00 2001 From: Alexey Alshenetsky Date: Sun, 12 Dec 2021 03:32:51 +0300 Subject: [PATCH] Add missing null check to ControllerReflector::getReflectionMethod (#1918) * add null check https://github.com/nelmio/NelmioApiDocBundle/issues/1909 * less code is better * add tests for ControllerReflection::getReflectionMethod() * lint fix * style_ci fixes Co-authored-by: Alexey --- Tests/Util/ControllerReflectorTest.php | 29 ++++++++++++++++++++++++++ Util/ControllerReflector.php | 11 +++++----- 2 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 Tests/Util/ControllerReflectorTest.php diff --git a/Tests/Util/ControllerReflectorTest.php b/Tests/Util/ControllerReflectorTest.php new file mode 100644 index 0000000..d44b0db --- /dev/null +++ b/Tests/Util/ControllerReflectorTest.php @@ -0,0 +1,29 @@ +assertEquals( + ReflectionMethod::class, + get_class($controllerReflector->getReflectionMethod([BazingaController::class, 'userAction'])) + ); + $this->assertEquals( + ReflectionMethod::class, + get_class($controllerReflector->getReflectionMethod(BazingaController::class.'::userAction')) + ); + $this->assertNull( + $controllerReflector->getReflectionMethod('UnknownController::userAction') + ); + $this->assertNull($controllerReflector->getReflectionMethod(null)); + } +} diff --git a/Util/ControllerReflector.php b/Util/ControllerReflector.php index 49fe9d7..b085658 100644 --- a/Util/ControllerReflector.php +++ b/Util/ControllerReflector.php @@ -38,15 +38,16 @@ class ControllerReflector /** * Returns the ReflectionMethod for the given controller string. * - * @return \ReflectionMethod|null + * @return \ReflectionMethod|null */ public function getReflectionMethod($controller) { if (is_string($controller)) { $controller = $this->getClassAndMethod($controller); - if (null === $controller) { - return null; - } + } + + if (null === $controller) { + return null; } return $this->geReflectionMethodByClassNameAndMethodName(...$controller); @@ -122,7 +123,7 @@ class ControllerReflector if (!isset($class) || !isset($method)) { $this->controllers[$controller] = null; - return; + return null; } return $this->controllers[$controller] = [$class, $method];