From 1a4780382aabcd4cc0d27d0dc7b8349342026131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Fri, 19 Jul 2019 12:44:35 +0200 Subject: [PATCH] fixed bugs reported by PhpStan (#212)(#211)(#210)(#208)(#207)(#213) --- src/Bridges/DITracy/ContainerPanel.php | 4 ++-- src/DI/Config/Helpers.php | 1 - src/DI/Definitions/Definition.php | 2 +- src/DI/Extensions/DIExtension.php | 2 +- src/DI/Extensions/InjectExtension.php | 15 +++++++-------- src/DI/Helpers.php | 1 + src/DI/PhpGenerator.php | 2 +- src/DI/Resolver.php | 17 +++++++---------- 8 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/Bridges/DITracy/ContainerPanel.php b/src/Bridges/DITracy/ContainerPanel.php index 8f663dc35..93b7c30b5 100644 --- a/src/Bridges/DITracy/ContainerPanel.php +++ b/src/Bridges/DITracy/ContainerPanel.php @@ -21,13 +21,13 @@ class ContainerPanel implements Tracy\IBarPanel { use Nette\SmartObject; - /** @var int */ + /** @var float|null */ public static $compilationTime; /** @var Nette\DI\Container */ private $container; - /** @var int|null */ + /** @var float|null */ private $elapsedTime; diff --git a/src/DI/Config/Helpers.php b/src/DI/Config/Helpers.php index 40052a277..ec1528482 100644 --- a/src/DI/Config/Helpers.php +++ b/src/DI/Config/Helpers.php @@ -35,7 +35,6 @@ public static function merge($left, $right) /** * Return true if array prevents merging and removes this information. - * @return mixed */ public static function takeParent(&$data): bool { diff --git a/src/DI/Definitions/Definition.php b/src/DI/Definitions/Definition.php index 5aa28b775..9a3950483 100644 --- a/src/DI/Definitions/Definition.php +++ b/src/DI/Definitions/Definition.php @@ -19,7 +19,7 @@ abstract class Definition { use Nette\SmartObject; - /** @var string */ + /** @var string|null */ private $name; /** @var string|null class or interface name */ diff --git a/src/DI/Extensions/DIExtension.php b/src/DI/Extensions/DIExtension.php index 9e7b66ac2..83de8e739 100644 --- a/src/DI/Extensions/DIExtension.php +++ b/src/DI/Extensions/DIExtension.php @@ -26,7 +26,7 @@ final class DIExtension extends Nette\DI\CompilerExtension /** @var bool */ private $debugMode; - /** @var int */ + /** @var float */ private $time; diff --git a/src/DI/Extensions/InjectExtension.php b/src/DI/Extensions/InjectExtension.php index 270464c4f..0aebb56b8 100644 --- a/src/DI/Extensions/InjectExtension.php +++ b/src/DI/Extensions/InjectExtension.php @@ -32,12 +32,11 @@ public function getConfigSchema(): Nette\Schema\Schema public function beforeCompile() { foreach ($this->getContainerBuilder()->getDefinitions() as $def) { - if ( - $def->getTag(self::TAG_INJECT) - && ($def = $def instanceof Definitions\FactoryDefinition ? $def->getResultDefinition() : $def) - && ($def instanceof Definitions\ServiceDefinition) - ) { - $this->updateDefinition($def); + if ($def->getTag(self::TAG_INJECT)) { + $def = $def instanceof Definitions\FactoryDefinition ? $def->getResultDefinition() : $def; + if ($def instanceof Definitions\ServiceDefinition) { + $this->updateDefinition($def); + } } } } @@ -142,9 +141,9 @@ public static function callInjects(DI\Container $container, $service): void /** * @param object|string $class - * @param DI\Resolver|DI\Container $container + * @param DI\Container|DI\ContainerBuilder|null $container */ - private static function checkType($class, string $name, ?string $type, $container = null): void + private static function checkType($class, string $name, ?string $type, $container): void { $propName = Reflection::toString(new \ReflectionProperty($class, $name)); if (!$type) { diff --git a/src/DI/Helpers.php b/src/DI/Helpers.php index bc525e8eb..ba335daf8 100644 --- a/src/DI/Helpers.php +++ b/src/DI/Helpers.php @@ -147,6 +147,7 @@ public static function prefixServiceName($config, string $namespace) /** * Returns an annotation value. + * @param \ReflectionFunctionAbstract|\ReflectionProperty|\ReflectionClass $ref */ public static function parseAnnotation(\Reflector $ref, string $name): ?string { diff --git a/src/DI/PhpGenerator.php b/src/DI/PhpGenerator.php index da9a0b716..6ac1b4569 100644 --- a/src/DI/PhpGenerator.php +++ b/src/DI/PhpGenerator.php @@ -84,8 +84,8 @@ public function toString(Nette\PhpGenerator\ClassType $class): string public function generateMethod(Definitions\Definition $def): Nette\PhpGenerator\Method { + $name = $def->getName(); try { - $name = $def->getName(); $method = new Nette\PhpGenerator\Method(Container::getMethodName($name)); $method->setVisibility('public'); $method->setReturnType($def->getType()); diff --git a/src/DI/Resolver.php b/src/DI/Resolver.php index 3438f807c..7ab9d5881 100644 --- a/src/DI/Resolver.php +++ b/src/DI/Resolver.php @@ -109,9 +109,11 @@ public function resolveEntityType(Statement $statement): ?string } try { + /** @var \ReflectionMethod|\ReflectionFunction $reflection */ $reflection = Nette\Utils\Callback::toReflection($entity[0] === '' ? $entity[1] : $entity); $refClass = $reflection instanceof \ReflectionMethod ? $reflection->getDeclaringClass() : null; } catch (\ReflectionException $e) { + $refClass = $reflection = null; } if (isset($e) || ($refClass && (!$reflection->isPublic() @@ -326,16 +328,11 @@ public function normalizeReference(Reference $ref): Reference return $this->currentService && $service === $this->currentService->getName() ? new Reference(Reference::SELF) : $ref; - } else { - try { - $res = $this->getByType($service); - } catch (NotAllowedDuringResolvingException $e) { - return new Reference($service); - } - if (!$res) { - throw new ServiceCreationException("Reference to missing service of type $service."); - } - return $res; + } + try { + return $this->getByType($service); + } catch (NotAllowedDuringResolvingException $e) { + return new Reference($service); } }