Skip to content

Commit

Permalink
Fixes for TypeSpecifier BC breaks
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 13, 2024
1 parent eb60ffd commit 879890e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,9 @@ public function specifyTypes(
$scope,
$expr,
TypeSpecifierContext::createTruthy(),
$rootExpr,
);
)->setRootExpr($rootExpr ?? $expr);

return $this->specifyRootExprIfSet($rootExpr, $specifiedTypes);
return $this->specifyRootExprIfSet($rootExpr, $scope, $specifiedTypes);
}

/**
Expand Down Expand Up @@ -688,6 +687,7 @@ private function handleAllNot(
$scope,
$node->getArgs()[0]->value,
static fn (Type $type): Type => TypeCombinator::removeNull($type),
null,
);
}

Expand All @@ -703,6 +703,7 @@ private function handleAllNot(
$scope,
$node->getArgs()[0]->value,
static fn (Type $type): Type => TypeCombinator::remove($type, $classNameType),
null,
);
}

Expand All @@ -712,6 +713,7 @@ private function handleAllNot(
$scope,
$node->getArgs()[0]->value,
static fn (Type $type): Type => TypeCombinator::remove($type, $valueType),
null,
);
}

Expand Down Expand Up @@ -739,8 +741,7 @@ private function handleAll(
$scope,
$expr,
TypeSpecifierContext::createTruthy(),
$rootExpr,
);
)->setRootExpr($rootExpr ?? $expr);

$sureNotTypes = $specifiedTypes->getSureNotTypes();
foreach ($specifiedTypes->getSureTypes() as $exprStr => [$exprNode, $type]) {
Expand Down Expand Up @@ -768,7 +769,7 @@ private function allArrayOrIterable(
Scope $scope,
Expr $expr,
Closure $typeCallback,
?Expr $rootExpr = null
?Expr $rootExpr
): SpecifiedTypes
{
$currentType = TypeCombinator::intersect($scope->getType($expr), new IterableType(new MixedType(), new MixedType()));
Expand Down Expand Up @@ -812,12 +813,10 @@ private function allArrayOrIterable(
$expr,
$specifiedType,
TypeSpecifierContext::createTruthy(),
false,
$scope,
$rootExpr,
);
)->setRootExpr($rootExpr);

return $this->specifyRootExprIfSet($rootExpr, $specifiedTypes);
return $this->specifyRootExprIfSet($rootExpr, $scope, $specifiedTypes);
}

/**
Expand Down Expand Up @@ -882,15 +881,15 @@ private static function createIsNonEmptyStringAndSomethingExprPair(string $name,
return [$expr, $rootExpr];
}

private function specifyRootExprIfSet(?Expr $rootExpr, SpecifiedTypes $specifiedTypes): SpecifiedTypes
private function specifyRootExprIfSet(?Expr $rootExpr, Scope $scope, SpecifiedTypes $specifiedTypes): SpecifiedTypes
{
if ($rootExpr === null) {
return $specifiedTypes;
}

// Makes consecutive calls with a rootExpr adding unknown info via FAUX_FUNCTION evaluate to true
return $specifiedTypes->unionWith(
$this->typeSpecifier->create($rootExpr, new ConstantBooleanType(true), TypeSpecifierContext::createTruthy()),
$this->typeSpecifier->create($rootExpr, new ConstantBooleanType(true), TypeSpecifierContext::createTruthy(), $scope),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ protected function getRule(): Rule

public function testExtension(): void
{
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';

$this->analyse([__DIR__ . '/data/impossible-check.php'], [
[
'Call to static method Webmozart\Assert\Assert::stringNotEmpty() with \'\' will always evaluate to false.',
Expand Down Expand Up @@ -92,10 +94,12 @@ public function testExtension(): void
[
'Call to static method Webmozart\Assert\Assert::implementsInterface() with class-string<WebmozartAssertImpossibleCheck\Bar>|WebmozartAssertImpossibleCheck\Bar and \'WebmozartAssertImpossibleCheck\\\Bar\' will always evaluate to true.',
105,
$tipText,
],
[
'Call to static method Webmozart\Assert\Assert::implementsInterface() with class-string<WebmozartAssertImpossibleCheck\Bar> and \'WebmozartAssertImpossibleCheck\\\Bar\' will always evaluate to true.',
108,
$tipText,
],
[
'Call to static method Webmozart\Assert\Assert::implementsInterface() with mixed and \'WebmozartAssertImpossibleCheck\\\Foo\' will always evaluate to false.',
Expand All @@ -104,6 +108,7 @@ public function testExtension(): void
[
'Call to static method Webmozart\Assert\Assert::isInstanceOf() with Exception and class-string<Exception> will always evaluate to true.',
119,
$tipText,
],
[
'Call to static method Webmozart\Assert\Assert::startsWith() with \'value\' and string will always evaluate to true.',
Expand Down Expand Up @@ -184,28 +189,34 @@ public function testEqNotEq(): void

public function testBug8(): void
{
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';
$this->analyse([__DIR__ . '/data/bug-8.php'], [
[
'Call to static method Webmozart\Assert\Assert::numeric() with numeric-string will always evaluate to true.',
15,
$tipText,
],
[
'Call to static method Webmozart\Assert\Assert::numeric() with \'foo\' will always evaluate to false.',
16,
$tipText,
],
[
'Call to static method Webmozart\Assert\Assert::numeric() with \'17.19\' will always evaluate to true.',
17,
$tipText,
],
]);
}

public function testBug17(): void
{
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';
$this->analyse([__DIR__ . '/data/bug-17.php'], [
[
'Call to static method Webmozart\Assert\Assert::implementsInterface() with \'DateTime\' and \'DateTimeInterface\' will always evaluate to true.',
9,
$tipText,
],
]);
}
Expand Down

0 comments on commit 879890e

Please sign in to comment.