Skip to content

Commit

Permalink
Cache filtered scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 25, 2022
1 parent 1fbfbb7 commit 7b104ae
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ class MutatingScope implements Scope
/** @var Type[] */
private array $resolvedTypes = [];

/** @var array<string, self> */
private array $truthyScopes = [];

/** @var array<string, self> */
private array $falseyScopes = [];

private ?string $namespace;

/**
Expand Down Expand Up @@ -4386,8 +4392,16 @@ public function removeTypeFromExpression(Expr $expr, Type $typeToRemove): self
*/
public function filterByTruthyValue(Expr $expr): Scope
{
$exprString = $this->getNodeKey($expr);
if (array_key_exists($exprString, $this->truthyScopes)) {
return $this->truthyScopes[$exprString];
}

$specifiedTypes = $this->typeSpecifier->specifyTypesInCondition($this, $expr, TypeSpecifierContext::createTruthy());
return $this->filterBySpecifiedTypes($specifiedTypes);
$scope = $this->filterBySpecifiedTypes($specifiedTypes);
$this->truthyScopes[$exprString] = $scope;

return $scope;
}

/**
Expand All @@ -4396,8 +4410,16 @@ public function filterByTruthyValue(Expr $expr): Scope
*/
public function filterByFalseyValue(Expr $expr): Scope
{
$exprString = $this->getNodeKey($expr);
if (array_key_exists($exprString, $this->falseyScopes)) {
return $this->falseyScopes[$exprString];
}

$specifiedTypes = $this->typeSpecifier->specifyTypesInCondition($this, $expr, TypeSpecifierContext::createFalsey());
return $this->filterBySpecifiedTypes($specifiedTypes);
$scope = $this->filterBySpecifiedTypes($specifiedTypes);
$this->falseyScopes[$exprString] = $scope;

return $scope;
}

public function filterBySpecifiedTypes(SpecifiedTypes $specifiedTypes): self
Expand Down

1 comment on commit 7b104ae

@herndlm
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I'm very curious about the results

Please sign in to comment.