Skip to content

Commit 530b155

Browse files
committed
TASK: Narrow nullableString && true
1 parent d00a194 commit 530b155

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

src/TypeSystem/Narrower/ExpressionTypeNarrower.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
namespace PackageFactory\ComponentEngine\TypeSystem\Narrower;
2424

25+
use PackageFactory\ComponentEngine\Definition\BinaryOperator;
2526
use PackageFactory\ComponentEngine\Parser\Ast\BinaryOperationNode;
2627
use PackageFactory\ComponentEngine\Parser\Ast\BooleanLiteralNode;
2728
use PackageFactory\ComponentEngine\Parser\Ast\ExpressionNode;
@@ -74,18 +75,28 @@ public function narrowTypesOfSymbolsIn(ExpressionNode $expressionNode, TypeNarro
7475
&& $other = $first // @phpstan-ignore-line
7576
)
7677
) {
77-
if (!$contextBasedOnOperator = $context->basedOnBinaryOperator($binaryOperationNode->operator)) {
78-
return NarrowedTypes::empty();
79-
}
78+
switch ($binaryOperationNode->operator) {
79+
case BinaryOperator::AND:
80+
if ($boolean->value && $context === TypeNarrowerContext::TRUTHY) {
81+
return $this->narrowTypesOfSymbolsIn($other, $context);
82+
}
83+
break;
84+
case BinaryOperator::EQUAL:
85+
case BinaryOperator::NOT_EQUAL:
86+
$contextBasedOnOperator = $context->basedOnBinaryOperator($binaryOperationNode->operator);
87+
assert($contextBasedOnOperator !== null);
8088

81-
if ($other->root instanceof IdentifierNode) {
82-
return NarrowedTypes::empty();
89+
if ($other->root instanceof IdentifierNode) {
90+
return NarrowedTypes::empty();
91+
}
92+
93+
return $this->narrowTypesOfSymbolsIn(
94+
$other,
95+
$boolean->value ? $contextBasedOnOperator : $contextBasedOnOperator->negate()
96+
);
8397
}
8498

85-
return $this->narrowTypesOfSymbolsIn(
86-
$other,
87-
$boolean->value ? $contextBasedOnOperator : $contextBasedOnOperator->negate()
88-
);
99+
return NarrowedTypes::empty();
89100
}
90101

91102
$expressionTypeResolver = (new ExpressionTypeResolver($this->scope));

test/Unit/TypeSystem/Narrower/ExpressionTypeNarrowerTest.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ public function narrowedExpressionsExamples(): mixed
7777
'nullableString === variableOfTypeNull', $variableIsNull
7878
],
7979

80+
'nullableString && true' => [
81+
'nullableString && true',
82+
$variableIsString
83+
],
84+
85+
'FALSY:: nullableString && true' => [
86+
'nullableString && true',
87+
NarrowedTypes::empty(),
88+
TypeNarrowerContext::FALSY
89+
],
90+
8091
'nullableString === true' => [
8192
'nullableString === true',
8293
NarrowedTypes::empty()
@@ -88,8 +99,11 @@ public function narrowedExpressionsExamples(): mixed
8899
* @dataProvider narrowedExpressionsExamples
89100
* @test
90101
*/
91-
public function narrowedExpressions(string $expressionAsString, NarrowedTypes $expectedTypes): void
92-
{
102+
public function narrowedExpressions(
103+
string $expressionAsString,
104+
NarrowedTypes $expectedTypes,
105+
TypeNarrowerContext $context = TypeNarrowerContext::TRUTHY
106+
): void {
93107
$expressionTypeNarrower = new ExpressionTypeNarrower(
94108
scope: new DummyScope([
95109
'nullableString' => UnionType::of(StringType::get(), NullType::get()),
@@ -99,7 +113,7 @@ public function narrowedExpressions(string $expressionAsString, NarrowedTypes $e
99113

100114
$expressionNode = ExpressionNode::fromString($expressionAsString);
101115

102-
$actualTypes = $expressionTypeNarrower->narrowTypesOfSymbolsIn($expressionNode, TypeNarrowerContext::TRUTHY);
116+
$actualTypes = $expressionTypeNarrower->narrowTypesOfSymbolsIn($expressionNode, $context);
103117

104118
$this->assertEqualsCanonicalizing(
105119
$expectedTypes->toArray(),

0 commit comments

Comments
 (0)