Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/undefined expression allowed #1174

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Analyser/DirectScopeFactory.php
Expand Up @@ -51,6 +51,7 @@ public function __construct(
* @param VariableTypeHolder[] $moreSpecificTypes
* @param array<string, ConditionalExpressionHolder[]> $conditionalExpressions
* @param array<string, true> $currentlyAssignedExpressions
* @param array<string, bool> $currentlyAllowedUndefinedExpressions
* @param array<string, Type> $nativeExpressionTypes
* @param array<(FunctionReflection|MethodReflection)> $inFunctionCallsStack
*
Expand All @@ -68,6 +69,7 @@ public function create(
?ParametersAcceptor $anonymousFunctionReflection = null,
bool $inFirstLevelStatement = true,
array $currentlyAssignedExpressions = [],
array $currentlyAllowedUndefinedExpressions = [],
array $nativeExpressionTypes = [],
array $inFunctionCallsStack = [],
bool $afterExtractCall = false,
Expand Down Expand Up @@ -102,6 +104,7 @@ public function create(
$anonymousFunctionReflection,
$inFirstLevelStatement,
$currentlyAssignedExpressions,
$currentlyAllowedUndefinedExpressions,
$nativeExpressionTypes,
$inFunctionCallsStack,
$this->dynamicConstantNames,
Expand Down
3 changes: 3 additions & 0 deletions src/Analyser/LazyScopeFactory.php
Expand Up @@ -42,6 +42,7 @@ public function __construct(
* @param VariableTypeHolder[] $moreSpecificTypes
* @param array<string, ConditionalExpressionHolder[]> $conditionalExpressions
* @param array<string, true> $currentlyAssignedExpressions
* @param array<string, bool> $currentlyAllowedUndefinedExpressions
* @param array<string, Type> $nativeExpressionTypes
* @param array<(FunctionReflection|MethodReflection)> $inFunctionCallsStack
*
Expand All @@ -59,6 +60,7 @@ public function create(
?ParametersAcceptor $anonymousFunctionReflection = null,
bool $inFirstLevelStatement = true,
array $currentlyAssignedExpressions = [],
array $currentlyAllowedUndefinedExpressions = [],
array $nativeExpressionTypes = [],
array $inFunctionCallsStack = [],
bool $afterExtractCall = false,
Expand Down Expand Up @@ -93,6 +95,7 @@ public function create(
$anonymousFunctionReflection,
$inFirstLevelStatement,
$currentlyAssignedExpressions,
$currentlyAllowedUndefinedExpressions,
$nativeExpressionTypes,
$inFunctionCallsStack,
$this->dynamicConstantNames,
Expand Down
94 changes: 94 additions & 0 deletions src/Analyser/MutatingScope.php
Expand Up @@ -167,6 +167,7 @@ class MutatingScope implements Scope
* @param VariableTypeHolder[] $moreSpecificTypes
* @param array<string, ConditionalExpressionHolder[]> $conditionalExpressions
* @param array<string, true> $currentlyAssignedExpressions
* @param array<string, bool> $currentlyAllowedUndefinedExpressions
* @param array<string, Type> $nativeExpressionTypes
* @param array<MethodReflection|FunctionReflection> $inFunctionCallsStack
* @param string[] $dynamicConstantNames
Expand Down Expand Up @@ -194,6 +195,7 @@ public function __construct(
private ?ParametersAcceptor $anonymousFunctionReflection = null,
private bool $inFirstLevelStatement = true,
private array $currentlyAssignedExpressions = [],
private array $currentlyAllowedUndefinedExpressions = [],
private array $nativeExpressionTypes = [],
private array $inFunctionCallsStack = [],
private array $dynamicConstantNames = [],
Expand Down Expand Up @@ -341,6 +343,7 @@ public function afterExtractCall(): self
$this->anonymousFunctionReflection,
$this->isInFirstLevelStatement(),
$this->currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
$this->nativeExpressionTypes,
$this->inFunctionCallsStack,
true,
Expand Down Expand Up @@ -397,6 +400,7 @@ public function afterClearstatcacheCall(): self
$this->anonymousFunctionReflection,
$this->isInFirstLevelStatement(),
$this->currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
$this->nativeExpressionTypes,
$this->inFunctionCallsStack,
$this->afterExtractCall,
Expand Down Expand Up @@ -3016,6 +3020,7 @@ public function doNotTreatPhpDocTypesAsCertain(): Scope
$this->anonymousFunctionReflection,
$this->inFirstLevelStatement,
$this->currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
$this->nativeExpressionTypes,
$this->inFunctionCallsStack,
$this->dynamicConstantNames,
Expand Down Expand Up @@ -3056,6 +3061,7 @@ private function promoteNativeTypes(): self
$this->anonymousFunctionReflection,
$this->inFirstLevelStatement,
$this->currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
[],
);
}
Expand Down Expand Up @@ -3315,6 +3321,7 @@ public function pushInFunctionCall($reflection): self
$this->anonymousFunctionReflection,
$this->isInFirstLevelStatement(),
$this->currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
$this->nativeExpressionTypes,
$stack,
$this->afterExtractCall,
Expand All @@ -3340,6 +3347,7 @@ public function popInFunctionCall(): self
$this->anonymousFunctionReflection,
$this->isInFirstLevelStatement(),
$this->currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
$this->nativeExpressionTypes,
$stack,
$this->afterExtractCall,
Expand Down Expand Up @@ -3596,6 +3604,7 @@ private function enterFunctionLike(
null,
true,
[],
[],
$nativeExpressionTypes,
);
}
Expand Down Expand Up @@ -3716,6 +3725,7 @@ public function enterAnonymousFunction(
$anonymousFunctionReflection,
true,
[],
[],
$scope->nativeExpressionTypes,
[],
false,
Expand Down Expand Up @@ -3807,6 +3817,7 @@ private function enterAnonymousFunctionWithoutReflection(
new TrivialParametersAcceptor(),
true,
[],
[],
$nativeTypes,
[],
false,
Expand Down Expand Up @@ -3842,6 +3853,7 @@ public function enterArrowFunction(Expr\ArrowFunction $arrowFunction, ?array $ca
[],
[],
[],
[],
$scope->afterExtractCall,
$scope->parentScope,
);
Expand Down Expand Up @@ -3962,6 +3974,7 @@ private function enterArrowFunctionWithoutReflection(Expr\ArrowFunction $arrowFu
[],
[],
[],
[],
$this->afterExtractCall,
$this->parentScope,
);
Expand Down Expand Up @@ -4089,6 +4102,7 @@ public function enterExpressionAssign(Expr $expr): self
$this->anonymousFunctionReflection,
$this->isInFirstLevelStatement(),
$currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
$this->nativeExpressionTypes,
[],
$this->afterExtractCall,
Expand All @@ -4115,6 +4129,7 @@ public function exitExpressionAssign(Expr $expr): self
$this->anonymousFunctionReflection,
$this->isInFirstLevelStatement(),
$currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
$this->nativeExpressionTypes,
[],
$this->afterExtractCall,
Expand All @@ -4129,6 +4144,70 @@ public function isInExpressionAssign(Expr $expr): bool
return array_key_exists($exprString, $this->currentlyAssignedExpressions);
}

public function setAllowedUndefinedExpression(Expr $expr, bool $isAllowed): self
{
$exprString = $this->getNodeKey($expr);
if (array_key_exists($exprString, $this->currentlyAllowedUndefinedExpressions)) {
return $this;
}
$currentlyAllowedUndefinedExpressions = $this->currentlyAllowedUndefinedExpressions;
$currentlyAllowedUndefinedExpressions[$exprString] = $isAllowed;

return $this->scopeFactory->create(
$this->context,
$this->isDeclareStrictTypes(),
$this->constantTypes,
$this->getFunction(),
$this->getNamespace(),
$this->getVariableTypes(),
$this->moreSpecificTypes,
$this->conditionalExpressions,
$this->inClosureBindScopeClass,
$this->anonymousFunctionReflection,
$this->isInFirstLevelStatement(),
$this->currentlyAssignedExpressions,
$currentlyAllowedUndefinedExpressions,
$this->nativeExpressionTypes,
[],
$this->afterExtractCall,
$this->parentScope,
);
}

public function unsetAllowedUndefinedExpression(Expr $expr): self
{
$exprString = $this->getNodeKey($expr);
$currentlyAllowedUndefinedExpressions = $this->currentlyAllowedUndefinedExpressions;
unset($currentlyAllowedUndefinedExpressions[$exprString]);

return $this->scopeFactory->create(
$this->context,
$this->isDeclareStrictTypes(),
$this->constantTypes,
$this->getFunction(),
$this->getNamespace(),
$this->getVariableTypes(),
$this->moreSpecificTypes,
$this->conditionalExpressions,
$this->inClosureBindScopeClass,
$this->anonymousFunctionReflection,
$this->isInFirstLevelStatement(),
$this->currentlyAssignedExpressions,
$currentlyAllowedUndefinedExpressions,
$this->nativeExpressionTypes,
[],
$this->afterExtractCall,
$this->parentScope,
);
}

/** @api */
public function isUndefinedExpressionAllowed(Expr $expr): bool
{
$exprString = $this->getNodeKey($expr);
return array_key_exists($exprString, $this->currentlyAllowedUndefinedExpressions) && $this->currentlyAllowedUndefinedExpressions[$exprString];
}

public function assignVariable(string $variableName, Type $type, ?TrinaryLogic $certainty = null): self
{
if ($certainty === null) {
Expand Down Expand Up @@ -4191,6 +4270,7 @@ public function assignVariable(string $variableName, Type $type, ?TrinaryLogic $
$this->anonymousFunctionReflection,
$this->inFirstLevelStatement,
$this->currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
$nativeTypes,
$this->inFunctionCallsStack,
$this->afterExtractCall,
Expand Down Expand Up @@ -4227,6 +4307,7 @@ public function unsetExpression(Expr $expr): self
$this->anonymousFunctionReflection,
$this->inFirstLevelStatement,
[],
[],
$nativeTypes,
[],
$this->afterExtractCall,
Expand Down Expand Up @@ -4270,6 +4351,7 @@ public function specifyExpressionType(Expr $expr, Type $type, ?Type $nativeType
$this->anonymousFunctionReflection,
$this->inFirstLevelStatement,
$this->currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
$this->nativeExpressionTypes,
$this->inFunctionCallsStack,
$this->afterExtractCall,
Expand Down Expand Up @@ -4308,6 +4390,7 @@ public function specifyExpressionType(Expr $expr, Type $type, ?Type $nativeType
$this->anonymousFunctionReflection,
$this->inFirstLevelStatement,
$this->currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
$nativeTypes,
$this->inFunctionCallsStack,
$this->afterExtractCall,
Expand Down Expand Up @@ -4425,6 +4508,7 @@ public function invalidateExpression(Expr $expressionToInvalidate, bool $require
$this->anonymousFunctionReflection,
$this->inFirstLevelStatement,
$this->currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
$nativeExpressionTypes,
[],
$this->afterExtractCall,
Expand Down Expand Up @@ -4483,6 +4567,7 @@ public function invalidateMethodsOnExpression(Expr $expressionToInvalidate): sel
$this->anonymousFunctionReflection,
$this->inFirstLevelStatement,
$this->currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
$nativeExpressionTypes,
[],
$this->afterExtractCall,
Expand Down Expand Up @@ -4700,6 +4785,7 @@ public function changeConditionalExpressions(array $newConditionalExpressionHold
$this->anonymousFunctionReflection,
$this->inFirstLevelStatement,
$this->currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
$this->nativeExpressionTypes,
$this->inFunctionCallsStack,
$this->afterExtractCall,
Expand Down Expand Up @@ -4727,6 +4813,7 @@ public function addConditionalExpressions(string $exprString, array $conditional
$this->anonymousFunctionReflection,
$this->inFirstLevelStatement,
$this->currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
$this->nativeExpressionTypes,
$this->inFunctionCallsStack,
$this->afterExtractCall,
Expand All @@ -4749,6 +4836,7 @@ public function exitFirstLevelStatements(): self
$this->anonymousFunctionReflection,
false,
$this->currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
$this->nativeExpressionTypes,
$this->inFunctionCallsStack,
$this->afterExtractCall,
Expand Down Expand Up @@ -4786,6 +4874,7 @@ private function addMoreSpecificTypes(array $types): self
$this->anonymousFunctionReflection,
$this->inFirstLevelStatement,
$this->currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
$this->nativeExpressionTypes,
[],
$this->afterExtractCall,
Expand Down Expand Up @@ -4854,6 +4943,7 @@ public function mergeWith(?self $otherScope): self
$this->anonymousFunctionReflection,
$this->inFirstLevelStatement,
[],
[],
array_map($variableHolderToType, array_filter($this->mergeVariableHolders(
array_map($typeToVariableHolder, $this->nativeExpressionTypes),
array_map($typeToVariableHolder, $otherScope->nativeExpressionTypes),
Expand Down Expand Up @@ -5025,6 +5115,7 @@ public function processFinallyScope(self $finallyScope, self $originalFinallySco
$this->anonymousFunctionReflection,
$this->inFirstLevelStatement,
[],
[],
array_map($variableHolderToType, array_filter($this->processFinallyScopeVariableTypeHolders(
array_map($typeToVariableHolder, $this->nativeExpressionTypes),
array_map($typeToVariableHolder, $finallyScope->nativeExpressionTypes),
Expand Down Expand Up @@ -5122,6 +5213,7 @@ public function processClosureScope(
$this->anonymousFunctionReflection,
$this->inFirstLevelStatement,
[],
[],
$nativeExpressionTypes,
$this->inFunctionCallsStack,
$this->afterExtractCall,
Expand Down Expand Up @@ -5172,6 +5264,7 @@ public function processAlwaysIterableForeachScopeWithoutPollute(self $finalScope
$this->anonymousFunctionReflection,
$this->inFirstLevelStatement,
[],
[],
$nativeTypes,
[],
$this->afterExtractCall,
Expand Down Expand Up @@ -5215,6 +5308,7 @@ public function generalizeWith(self $otherScope): self
$this->anonymousFunctionReflection,
$this->inFirstLevelStatement,
[],
[],
$nativeTypes,
[],
$this->afterExtractCall,
Expand Down