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

refactor $conditionalExpressions #1270

Merged
merged 2 commits into from May 2, 2022
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
2 changes: 1 addition & 1 deletion src/Analyser/MutatingScope.php
Expand Up @@ -4494,7 +4494,7 @@ public function filterBySpecifiedTypes(SpecifiedTypes $specifiedTypes): self
}

if (!$typeGuards[$conditionExprString]->equals($conditionalType)) {
continue 2;
continue;
}

$matchingConditions[$conditionExprString] = $conditionalType;
Expand Down
9 changes: 6 additions & 3 deletions src/Analyser/NodeScopeResolver.php
Expand Up @@ -3490,11 +3490,12 @@ private function processSureTypesForConditionalExpressionsAfterAssign(Scope $sco
$conditionalExpressions[$exprString] = [];
}

$conditionalExpressions[$exprString][] = new ConditionalExpressionHolder([
$holder = new ConditionalExpressionHolder([
'$' . $variableName => $variableType,
], VariableTypeHolder::createYes(
TypeCombinator::intersect($scope->getType($expr), $exprType),
));
$conditionalExpressions[$exprString][$holder->getKey()] = $holder;
}

return $conditionalExpressions;
Expand All @@ -3518,11 +3519,12 @@ private function processSureNotTypesForConditionalExpressionsAfterAssign(Scope $
$conditionalExpressions[$exprString] = [];
}

$conditionalExpressions[$exprString][] = new ConditionalExpressionHolder([
$holder = new ConditionalExpressionHolder([
'$' . $variableName => $variableType,
], VariableTypeHolder::createYes(
TypeCombinator::remove($scope->getType($expr), $exprType),
));
$conditionalExpressions[$exprString][$holder->getKey()] = $holder;
}

return $conditionalExpressions;
Expand Down Expand Up @@ -3689,9 +3691,10 @@ static function (): void {
$conditionalHolders = [];
foreach ($iterateeType->getKeyTypes() as $i => $keyType) {
$valueType = $iterateeType->getValueTypes()[$i];
$conditionalHolders[] = new ConditionalExpressionHolder([
$holder = new ConditionalExpressionHolder([
'$' . $stmt->keyVar->name => $keyType,
], new VariableTypeHolder($valueType, TrinaryLogic::createYes()));
$conditionalHolders[$holder->getKey()] = $holder;
}

$scope = $scope->addConditionalExpressions(
Expand Down
3 changes: 2 additions & 1 deletion src/Analyser/TypeSpecifier.php
Expand Up @@ -962,10 +962,11 @@ private function processBooleanConditionalTypes(Scope $scope, SpecifiedTypes $le
$holders[$exprString] = [];
}

$holders[$exprString][] = new ConditionalExpressionHolder(
$holder = new ConditionalExpressionHolder(
$conditionExpressionTypes,
new VariableTypeHolder(TypeCombinator::remove($scope->getType($expr), $type), TrinaryLogic::createYes()),
);
$holders[$exprString][$holder->getKey()] = $holder;
}

return $holders;
Expand Down
Expand Up @@ -140,15 +140,7 @@ public function testTypesAssignedToPropertiesExpressionNames(): void
66,
],
[
'Property PropertiesFromArrayIntoObject\Foo::$float_test (float) does not accept float|int|string.',
69,
],
[
'Property PropertiesFromArrayIntoObject\Foo::$foo (string) does not accept float|int|string.',
69,
],
[
'Property PropertiesFromArrayIntoObject\Foo::$lall (int) does not accept float|int|string.',
'Property PropertiesFromArrayIntoObject\Foo::$lall (int) does not accept string.',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change makes sense to me. The error must be same with line 54.

public function create_simple_1(): self {
$self = new self();
$data = $this->data();
foreach($data as $property => $value) {
$self->{$property} = $value;
}
return $self;
}
public function create_complex(): self {
$self = new self();
foreach($this->data() as $property => $value) {
if ($property === 'test') {
if ($self->{$property} === null) {
$self->{$property} = new \stdClass();
}
} else {
$self->{$property} = $value;
}

69,
],
[
Expand Down