Browse Source

Avoid suppressing error reported by static analyzers.

ADmad 5 years ago
parent
commit
c4abc5b507
2 changed files with 3 additions and 7 deletions
  1. 0 5
      phpstan-baseline.neon
  2. 3 2
      src/Controller/ControllerFactory.php

+ 0 - 5
phpstan-baseline.neon

@@ -151,11 +151,6 @@ parameters:
 			path: src/Controller/Controller.php
 
 		-
-			message: "#^Call to an undefined method ReflectionType::getName\\(\\)\\.$#"
-			count: 1
-			path: src/Controller/ControllerFactory.php
-
-		-
 			message: "#^Parameter \\#1 \\$request of method Cake\\\\Controller\\\\ControllerFactory\\:\\:getControllerClass\\(\\) expects Cake\\\\Http\\\\ServerRequest, Psr\\\\Http\\\\Message\\\\ServerRequestInterface given\\.$#"
 			count: 1
 			path: src/Controller/ControllerFactory.php

+ 3 - 2
src/Controller/ControllerFactory.php

@@ -27,6 +27,7 @@ use Psr\Http\Message\ResponseInterface;
 use Psr\Http\Message\ServerRequestInterface;
 use ReflectionClass;
 use ReflectionFunction;
+use ReflectionNamedType;
 
 /**
  * Factory method for building controllers for request.
@@ -113,7 +114,7 @@ class ControllerFactory implements ControllerFactoryInterface
                 $args[$position] = array_shift($passed);
                 continue;
             }
-            $typeName = ltrim($type->getName(), '?');
+            $typeName = $type instanceof ReflectionNamedType ? ltrim($type->getName(), '?') : null;
 
             // Primitive types are passed args as they can't be looked up in the container.
             // We only handle strings currently.
@@ -127,7 +128,7 @@ class ControllerFactory implements ControllerFactoryInterface
             }
 
             // Check the container and parameter default value.
-            if ($this->container->has($typeName)) {
+            if ($typeName && $this->container->has($typeName)) {
                 $args[$position] = $this->container->get($typeName);
             } elseif ($parameter->isDefaultValueAvailable()) {
                 $args[$position] = $parameter->getDefaultValue();