Skip to content

Commit

Permalink
Add missing null check to ControllerReflector::getReflectionMethod (#…
Browse files Browse the repository at this point in the history
…1918)

* add null check

#1909

* less code is better

* add tests for ControllerReflection::getReflectionMethod()

* lint fix

* style_ci fixes

Co-authored-by: Alexey <alshenestky@icloud.com>
  • Loading branch information
alshenetsky and Alexey committed Dec 12, 2021
1 parent babf788 commit 7de49bb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
29 changes: 29 additions & 0 deletions Tests/Util/ControllerReflectorTest.php
@@ -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));
}
}
11 changes: 6 additions & 5 deletions Util/ControllerReflector.php
Expand Up @@ -38,15 +38,16 @@ public function __construct(ContainerInterface $container)
/**
* 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);
Expand Down Expand Up @@ -122,7 +123,7 @@ private function getClassAndMethod(string $controller)
if (!isset($class) || !isset($method)) {
$this->controllers[$controller] = null;

return;
return null;
}

return $this->controllers[$controller] = [$class, $method];
Expand Down

0 comments on commit 7de49bb

Please sign in to comment.