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 <alshenestky@icloud.com>
This commit is contained in:
Alexey Alshenetsky 2021-12-12 03:32:51 +03:00 committed by GitHub
parent babf788636
commit 7de49bb4a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 5 deletions

View File

@ -0,0 +1,29 @@
<?php
namespace Nelmio\ApiDocBundle\Tests\Util;
use Nelmio\ApiDocBundle\Tests\Functional\Controller\BazingaController;
use Nelmio\ApiDocBundle\Util\ControllerReflector;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
use Symfony\Component\DependencyInjection\Container;
class ControllerReflectorTest extends TestCase
{
public function testGetReflectionMethod(): void
{
$controllerReflector = new ControllerReflector(new Container());
$this->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));
}
}

View File

@ -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];