Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 72 additions & 31 deletions src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ public function specifyTypes(
);
}

if (substr($staticMethodReflection->getName(), 0, 6) === 'nullOr') {
return $this->handleNullOr(
$staticMethodReflection->getName(),
$node,
$scope
);
}

$expression = self::createExpression($scope, $staticMethodReflection->getName(), $node->getArgs());
if ($expression === null) {
return new SpecifiedTypes([], []);
Expand All @@ -170,22 +178,7 @@ private static function createExpression(
$trimmedName = self::trimName($name);
$resolvers = self::getExpressionResolvers();
$resolver = $resolvers[$trimmedName];
$expression = $resolver($scope, ...$args);
if ($expression === null) {
return null;
}

if (substr($name, 0, 6) === 'nullOr') {
$expression = new BooleanOr(
$expression,
new Identical(
$args[0]->value,
new ConstFetch(new Name('null'))
)
);
}

return $expression;
return $resolver($scope, ...$args);
}

/**
Expand Down Expand Up @@ -799,24 +792,45 @@ private function handleAll(
TypeSpecifierContext::createTruthy()
);

if (count($specifiedTypes->getSureTypes()) > 0) {
$sureTypes = $specifiedTypes->getSureTypes();
$exprString = key($sureTypes);
[$exprNode, $type] = $sureTypes[$exprString];
$type = $this->determineVariableTypeFromSpecifiedTypes($scope, $specifiedTypes);

return $this->arrayOrIterable(
$scope,
$exprNode,
static function () use ($type): Type {
return $type->getIterableValueType();
}
);
}
if (count($specifiedTypes->getSureNotTypes()) > 0) {
throw new ShouldNotHappenException();
return $this->arrayOrIterable(
$scope,
$node->getArgs()[0]->value,
static function () use ($type): Type {
return $type->getIterableValueType();
}
);
}

private function handleNullOr(
string $methodName,
StaticCall $node,
Scope $scope
): SpecifiedTypes
{
$innerExpression = self::createExpression($scope, $methodName, $node->getArgs());
if ($innerExpression === null) {
return new SpecifiedTypes();
}

return $specifiedTypes;
$expression = new BooleanAnd(
new NotIdentical(
$node->getArgs()[0]->value,
new ConstFetch(new Name('null'))
),
$innerExpression
);

$specifiedTypes = $this->typeSpecifier->specifyTypesInCondition(
$scope,
$expression,
TypeSpecifierContext::createTruthy()
);

$type = $this->determineVariableTypeFromSpecifiedTypes($scope, $specifiedTypes);

return $this->typeSpecifier->create($node->getArgs()[0]->value, TypeCombinator::addNull($type), TypeSpecifierContext::createTruthy(), false, $scope);
}

private function arrayOrIterable(
Expand Down Expand Up @@ -856,6 +870,33 @@ private function arrayOrIterable(
);
}

private function determineVariableTypeFromSpecifiedTypes(Scope $scope, SpecifiedTypes $specifiedTypes): Type
{
$sureTypes = $specifiedTypes->getSureTypes();
$sureNotTypes = $specifiedTypes->getSureNotTypes();

if (count($sureTypes) > 0) {
$exprString = key($sureTypes);
[, $type] = $sureTypes[$exprString];

if (array_key_exists($exprString, $sureNotTypes)) {
$type = TypeCombinator::remove($type, $sureNotTypes[$exprString][1]);
}

return $type;
}

if (count($sureNotTypes) > 0) {
$sureNotTypes = $specifiedTypes->getSureNotTypes();
$exprString = key($sureNotTypes);
[$exprNode, $type] = $sureNotTypes[$exprString];

return TypeCombinator::remove($scope->getType($exprNode), $type);
}

throw new ShouldNotHappenException();
}

/**
* @param Expr[] $expressions
* @param class-string<BinaryOp> $binaryOp
Expand Down