Skip to content

Commit

Permalink
[ExpressionLanguage] Remove usage of properties
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jul 25, 2022
1 parent 82815d0 commit be7db38
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
21 changes: 8 additions & 13 deletions Node/GetAttrNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@ class GetAttrNode extends Node
public const METHOD_CALL = 2;
public const ARRAY_CALL = 3;

private bool $isShortCircuited = false;
public bool $isNullCoalesce = false;

public function __construct(Node $node, Node $attribute, ArrayNode $arguments, int $type)
{
parent::__construct(
['node' => $node, 'attribute' => $attribute, 'arguments' => $arguments],
['type' => $type]
['type' => $type, 'is_null_coalesce' => false, 'is_short_circuited' => false],
);
}

Expand Down Expand Up @@ -73,8 +70,8 @@ public function evaluate(array $functions, array $values)
switch ($this->attributes['type']) {
case self::PROPERTY_CALL:
$obj = $this->nodes['node']->evaluate($functions, $values);
if (null === $obj && ($this->nodes['attribute']->isNullSafe || $this->isNullCoalesce)) {
$this->isShortCircuited = true;
if (null === $obj && ($this->nodes['attribute']->isNullSafe || $this->attributes['is_null_coalesce'])) {
$this->attributes['is_short_circuited'] = true;

return null;
}
Expand All @@ -88,7 +85,7 @@ public function evaluate(array $functions, array $values)

$property = $this->nodes['attribute']->attributes['value'];

if ($this->isNullCoalesce) {
if ($this->attributes['is_null_coalesce']) {
return $obj->$property ?? null;
}

Expand All @@ -98,7 +95,7 @@ public function evaluate(array $functions, array $values)
$obj = $this->nodes['node']->evaluate($functions, $values);

if (null === $obj && $this->nodes['attribute']->isNullSafe) {
$this->isShortCircuited = true;
$this->attributes['is_short_circuited'] = true;

return null;
}
Expand All @@ -122,11 +119,11 @@ public function evaluate(array $functions, array $values)
return null;
}

if (!\is_array($array) && !$array instanceof \ArrayAccess && !(null === $array && $this->isNullCoalesce)) {
if (!\is_array($array) && !$array instanceof \ArrayAccess && !(null === $array && $this->attributes['is_null_coalesce'])) {
throw new \RuntimeException(sprintf('Unable to get an item of non-array "%s".', $this->nodes['node']->dump()));
}

if ($this->isNullCoalesce) {
if ($this->attributes['is_null_coalesce']) {
return $array[$this->nodes['attribute']->evaluate($functions, $values)] ?? null;
}

Expand All @@ -136,9 +133,7 @@ public function evaluate(array $functions, array $values)

private function isShortCircuited(): bool
{
return $this->isShortCircuited
|| ($this->nodes['node'] instanceof self && $this->nodes['node']->isShortCircuited())
;
return $this->attributes['is_short_circuited'] || ($this->nodes['node'] instanceof self && $this->nodes['node']->isShortCircuited());
}

public function toArray()
Expand Down
2 changes: 1 addition & 1 deletion Node/NullCoalesceNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function compile(Compiler $compiler)
public function evaluate(array $functions, array $values)
{
if ($this->nodes['expr1'] instanceof GetAttrNode) {
$this->nodes['expr1']->isNullCoalesce = true;
$this->nodes['expr1']->attributes['is_null_coalesce'] = true;
}

return $this->nodes['expr1']->evaluate($functions, $values) ?? $this->nodes['expr2']->evaluate($functions, $values);
Expand Down

0 comments on commit be7db38

Please sign in to comment.