Skip to content

Commit

Permalink
Improved compiling of parameter default value so we have less @psalm-…
Browse files Browse the repository at this point in the history
…suppress annotations
  • Loading branch information
kukulich committed Oct 14, 2021
1 parent 488bbb2 commit b8099f7
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions src/Reflection/ReflectionParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit b8099f7

Please sign in to comment.