From b8099f7077a5bea0cc0782e98fceba4d7ba1a3a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Thu, 14 Oct 2021 14:04:41 +0200 Subject: [PATCH] Improved compiling of parameter default value so we have less @psalm-suppress annotations --- src/Reflection/ReflectionParameter.php | 27 ++++++++++---------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/src/Reflection/ReflectionParameter.php b/src/Reflection/ReflectionParameter.php index 80fa95f8c..71db4b914 100644 --- a/src/Reflection/ReflectionParameter.php +++ b/src/Reflection/ReflectionParameter.php @@ -152,20 +152,20 @@ public static function createFromNode( /** * @throws LogicException */ - private function compileDefaultValueNode(): void + private function getCompiledDefaultValueNode(): CompiledValue { if (! $this->isDefaultValueAvailable()) { throw new LogicException('This parameter does not have a default value available'); } - if ($this->compiledDefaultValue !== null) { - return; + if ($this->compiledDefaultValue === null) { + $this->compiledDefaultValue = (new CompileNodeToValue())->__invoke( + $this->node->default, + new CompilerContext($this->reflector, $this), + ); } - $this->compiledDefaultValue = (new CompileNodeToValue())->__invoke( - $this->node->default, - new CompilerContext($this->reflector, $this), - ); + return $this->compiledDefaultValue; } /** @@ -248,10 +248,7 @@ public function isDefaultValueAvailable(): bool */ public function getDefaultValue(): string|int|float|bool|array|null { - $this->compileDefaultValueNode(); - - /** @psalm-suppress PossiblyNullPropertyFetch */ - return $this->compiledDefaultValue->value; + return $this->getCompiledDefaultValueNode()->value; } /** @@ -441,10 +438,7 @@ public function isPromoted(): bool */ public function isDefaultValueConstant(): bool { - $this->compileDefaultValueNode(); - - /** @psalm-suppress PossiblyNullPropertyFetch */ - return $this->compiledDefaultValue->constantName !== null; + return $this->getCompiledDefaultValueNode()->constantName !== null; } /** @@ -456,8 +450,7 @@ public function getDefaultValueConstantName(): string throw new LogicException('This parameter is not a constant default value, so cannot have a constant name'); } - /** @psalm-suppress PossiblyNullPropertyFetch */ - return $this->compiledDefaultValue->constantName; + return $this->getCompiledDefaultValueNode()->constantName; } /**