Skip to content

Commit be3dcd8

Browse files
committed
Improve string asserts that assume some othe property besides being a non-empty-string
1 parent ae758a2 commit be3dcd8

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -757,12 +757,22 @@ private function getExpressionResolvers(): array
757757
];
758758

759759
foreach (['contains', 'startsWith', 'endsWith'] as $name) {
760-
$this->resolvers[$name] = function (Scope $scope, Arg $value, Arg $subString) use ($name): array {
760+
$this->resolvers[$name] = static function (Scope $scope, Arg $value, Arg $subString) use ($name): array {
761761
if ($scope->getType($subString->value)->isNonEmptyString()->yes()) {
762762
return self::createIsNonEmptyStringAndSomethingExprPair($name, [$value, $subString]);
763763
}
764764

765-
return [$this->resolvers['string']($scope, $value), null];
765+
$expr = new FuncCall(
766+
new Name('is_string'),
767+
[$value]
768+
);
769+
770+
$rootExpr = new BooleanAnd(
771+
$expr,
772+
new FuncCall(new Name('FAUX_FUNCTION_ ' . $name), [$value, $subString])
773+
);
774+
775+
return [$expr, $rootExpr];
766776
};
767777
}
768778

tests/Type/WebMozartAssert/ImpossibleCheckTypeMethodCallRuleTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ public function testExtension(): void
105105
'Call to static method Webmozart\Assert\Assert::isInstanceOf() with Exception and class-string<Exception> will always evaluate to true.',
106106
119,
107107
],
108+
[
109+
'Call to static method Webmozart\Assert\Assert::startsWith() with \'value\' and string will always evaluate to true.',
110+
126,
111+
],
108112
]);
109113
}
110114

tests/Type/WebMozartAssert/data/impossible-check.php

+8
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ public function testInstanceOfClassString(\Exception $e, string $name): void
119119
Assert::isInstanceOf($e, $name);
120120
}
121121

122+
public function testStartsWith(string $a): void
123+
{
124+
Assert::startsWith("value", "val");
125+
Assert::startsWith("value", $a);
126+
Assert::startsWith("value", $a);
127+
Assert::startsWith("value", "bix");
128+
}
129+
122130
}
123131

124132
interface Bar {};

tests/Type/WebMozartAssert/data/string.php

+7
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,11 @@ public function notWhitespaceOnly(string $a): void
188188
assertType('non-empty-string', $a);
189189
}
190190

191+
public function testStartsWithMixedHaystack($haystack, string $a): void
192+
{
193+
assertType('mixed', $haystack);
194+
Assert::startsWith($haystack, $a);
195+
assertType('string', $haystack);
196+
}
197+
191198
}

0 commit comments

Comments
 (0)