Skip to content

Commit 87ff7d1

Browse files
committed
Improve assert for contains / startsWith / endsWith
1 parent ae758a2 commit 87ff7d1

File tree

1 file changed

+54
-10
lines changed

1 file changed

+54
-10
lines changed

src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -754,18 +754,62 @@ private function getExpressionResolvers(): array
754754
)
755755
);
756756
},
757+
'contains' => static function (Scope $scope, Arg $value, Arg $subString): Expr {
758+
return new BooleanAnd(
759+
new FuncCall(
760+
new Name('is_string'),
761+
[$value]
762+
),
763+
new GreaterOrEqual(
764+
new FuncCall(
765+
new Name('strpos'),
766+
[$value]
767+
),
768+
0
769+
)
770+
);
771+
},
772+
'startsWith' => static function (Scope $scope, Arg $value, Arg $subString): Expr {
773+
return new BooleanAnd(
774+
new FuncCall(
775+
new Name('is_string'),
776+
[$value]
777+
),
778+
new Identical(
779+
new FuncCall(
780+
new Name('strpos'),
781+
[$value, $subString]
782+
),
783+
0
784+
)
785+
);
786+
},
787+
'endsWith' => static function (Scope $scope, Arg $value, Arg $subString): Expr {
788+
return new BooleanAnd(
789+
new FuncCall(
790+
new Name('is_string'),
791+
[$value]
792+
),
793+
new Identical(
794+
new FuncCall(
795+
new Name('strpos'),
796+
[$value, $subString]
797+
),
798+
new BinaryOp\Minus(
799+
new FuncCall(
800+
new Name('strlen'),
801+
[$value]
802+
),
803+
new FuncCall(
804+
new Name('strlen'),
805+
[$subString]
806+
)
807+
)
808+
)
809+
);
810+
},
757811
];
758812

759-
foreach (['contains', 'startsWith', 'endsWith'] as $name) {
760-
$this->resolvers[$name] = function (Scope $scope, Arg $value, Arg $subString) use ($name): array {
761-
if ($scope->getType($subString->value)->isNonEmptyString()->yes()) {
762-
return self::createIsNonEmptyStringAndSomethingExprPair($name, [$value, $subString]);
763-
}
764-
765-
return [$this->resolvers['string']($scope, $value), null];
766-
};
767-
}
768-
769813
$assertionsResultingAtLeastInNonEmptyString = [
770814
'startsWithLetter',
771815
'unicodeLetters',

0 commit comments

Comments
 (0)