Skip to content

Commit 879890e

Browse files
committed
Fixes for TypeSpecifier BC breaks
1 parent eb60ffd commit 879890e

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

Diff for: src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

+11-12
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,9 @@ public function specifyTypes(
165165
$scope,
166166
$expr,
167167
TypeSpecifierContext::createTruthy(),
168-
$rootExpr,
169-
);
168+
)->setRootExpr($rootExpr ?? $expr);
170169

171-
return $this->specifyRootExprIfSet($rootExpr, $specifiedTypes);
170+
return $this->specifyRootExprIfSet($rootExpr, $scope, $specifiedTypes);
172171
}
173172

174173
/**
@@ -688,6 +687,7 @@ private function handleAllNot(
688687
$scope,
689688
$node->getArgs()[0]->value,
690689
static fn (Type $type): Type => TypeCombinator::removeNull($type),
690+
null,
691691
);
692692
}
693693

@@ -703,6 +703,7 @@ private function handleAllNot(
703703
$scope,
704704
$node->getArgs()[0]->value,
705705
static fn (Type $type): Type => TypeCombinator::remove($type, $classNameType),
706+
null,
706707
);
707708
}
708709

@@ -712,6 +713,7 @@ private function handleAllNot(
712713
$scope,
713714
$node->getArgs()[0]->value,
714715
static fn (Type $type): Type => TypeCombinator::remove($type, $valueType),
716+
null,
715717
);
716718
}
717719

@@ -739,8 +741,7 @@ private function handleAll(
739741
$scope,
740742
$expr,
741743
TypeSpecifierContext::createTruthy(),
742-
$rootExpr,
743-
);
744+
)->setRootExpr($rootExpr ?? $expr);
744745

745746
$sureNotTypes = $specifiedTypes->getSureNotTypes();
746747
foreach ($specifiedTypes->getSureTypes() as $exprStr => [$exprNode, $type]) {
@@ -768,7 +769,7 @@ private function allArrayOrIterable(
768769
Scope $scope,
769770
Expr $expr,
770771
Closure $typeCallback,
771-
?Expr $rootExpr = null
772+
?Expr $rootExpr
772773
): SpecifiedTypes
773774
{
774775
$currentType = TypeCombinator::intersect($scope->getType($expr), new IterableType(new MixedType(), new MixedType()));
@@ -812,12 +813,10 @@ private function allArrayOrIterable(
812813
$expr,
813814
$specifiedType,
814815
TypeSpecifierContext::createTruthy(),
815-
false,
816816
$scope,
817-
$rootExpr,
818-
);
817+
)->setRootExpr($rootExpr);
819818

820-
return $this->specifyRootExprIfSet($rootExpr, $specifiedTypes);
819+
return $this->specifyRootExprIfSet($rootExpr, $scope, $specifiedTypes);
821820
}
822821

823822
/**
@@ -882,15 +881,15 @@ private static function createIsNonEmptyStringAndSomethingExprPair(string $name,
882881
return [$expr, $rootExpr];
883882
}
884883

885-
private function specifyRootExprIfSet(?Expr $rootExpr, SpecifiedTypes $specifiedTypes): SpecifiedTypes
884+
private function specifyRootExprIfSet(?Expr $rootExpr, Scope $scope, SpecifiedTypes $specifiedTypes): SpecifiedTypes
886885
{
887886
if ($rootExpr === null) {
888887
return $specifiedTypes;
889888
}
890889

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

Diff for: tests/Type/WebMozartAssert/ImpossibleCheckTypeMethodCallRuleTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ protected function getRule(): Rule
1919

2020
public function testExtension(): void
2121
{
22+
$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%</>.';
23+
2224
$this->analyse([__DIR__ . '/data/impossible-check.php'], [
2325
[
2426
'Call to static method Webmozart\Assert\Assert::stringNotEmpty() with \'\' will always evaluate to false.',
@@ -92,10 +94,12 @@ public function testExtension(): void
9294
[
9395
'Call to static method Webmozart\Assert\Assert::implementsInterface() with class-string<WebmozartAssertImpossibleCheck\Bar>|WebmozartAssertImpossibleCheck\Bar and \'WebmozartAssertImpossibleCheck\\\Bar\' will always evaluate to true.',
9496
105,
97+
$tipText,
9598
],
9699
[
97100
'Call to static method Webmozart\Assert\Assert::implementsInterface() with class-string<WebmozartAssertImpossibleCheck\Bar> and \'WebmozartAssertImpossibleCheck\\\Bar\' will always evaluate to true.',
98101
108,
102+
$tipText,
99103
],
100104
[
101105
'Call to static method Webmozart\Assert\Assert::implementsInterface() with mixed and \'WebmozartAssertImpossibleCheck\\\Foo\' will always evaluate to false.',
@@ -104,6 +108,7 @@ public function testExtension(): void
104108
[
105109
'Call to static method Webmozart\Assert\Assert::isInstanceOf() with Exception and class-string<Exception> will always evaluate to true.',
106110
119,
111+
$tipText,
107112
],
108113
[
109114
'Call to static method Webmozart\Assert\Assert::startsWith() with \'value\' and string will always evaluate to true.',
@@ -184,28 +189,34 @@ public function testEqNotEq(): void
184189

185190
public function testBug8(): void
186191
{
192+
$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%</>.';
187193
$this->analyse([__DIR__ . '/data/bug-8.php'], [
188194
[
189195
'Call to static method Webmozart\Assert\Assert::numeric() with numeric-string will always evaluate to true.',
190196
15,
197+
$tipText,
191198
],
192199
[
193200
'Call to static method Webmozart\Assert\Assert::numeric() with \'foo\' will always evaluate to false.',
194201
16,
202+
$tipText,
195203
],
196204
[
197205
'Call to static method Webmozart\Assert\Assert::numeric() with \'17.19\' will always evaluate to true.',
198206
17,
207+
$tipText,
199208
],
200209
]);
201210
}
202211

203212
public function testBug17(): void
204213
{
214+
$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%</>.';
205215
$this->analyse([__DIR__ . '/data/bug-17.php'], [
206216
[
207217
'Call to static method Webmozart\Assert\Assert::implementsInterface() with \'DateTime\' and \'DateTimeInterface\' will always evaluate to true.',
208218
9,
219+
$tipText,
209220
],
210221
]);
211222
}

0 commit comments

Comments
 (0)