diff --git a/src/Query/Constraint/Comparison.php b/src/Query/Constraint/Comparison.php index e346cd0f..70b21ef0 100644 --- a/src/Query/Constraint/Comparison.php +++ b/src/Query/Constraint/Comparison.php @@ -99,7 +99,7 @@ public function compare(mixed $subject, mixed $reference): bool self::NotIn => !in_array($subject, $reference, self::isSingleValue($subject)), /* @phpstan-ignore-line */ self::Regexp => is_string($subject) && 1 === preg_match($reference, $subject), /* @phpstan-ignore-line */ self::NotRegexp => is_string($subject) && 1 !== preg_match($reference, $subject), /* @phpstan-ignore-line */ - self::Contains => str_contains($subject, $reference), /* @phpstan-ignore-line */ + self::Contains => is_string($subject) && str_contains($subject, $reference), /* @phpstan-ignore-line */ self::NotContain => is_string($subject) && !str_contains($subject, $reference), /* @phpstan-ignore-line */ self::StartsWith => is_string($subject) && str_starts_with($subject, $reference), /* @phpstan-ignore-line */ self::EndsWith => is_string($subject) && str_ends_with($subject, $reference), /* @phpstan-ignore-line */ diff --git a/src/StatementTest.php b/src/StatementTest.php index 0dbaf513..f3a1df2c 100644 --- a/src/StatementTest.php +++ b/src/StatementTest.php @@ -13,6 +13,7 @@ namespace League\Csv; +use League\Csv\Query\Constraint\Comparison; use League\Csv\Query\QueryException; use OutOfBoundsException; use PHPUnit\Framework\Attributes\DataProvider; @@ -240,4 +241,22 @@ public function testOrderByDoesNotThrowOnInvalidOffsetOrLimit(): void self::assertSame([], $constraints->process($csv)->nth(42)); } + + public function testItCanCompareNullValue(): void + { + $document = <<setHeaderOffset(0); + $statement = Statement::create()->andWhere('Number', Comparison::Contains, '117'); + + // The count is really just to have the comparison iterate the entire + // list. The last item in the list will break the comparison because of + // the lack of is_string(...). + self::assertSame(1, $statement->process($csv)->count()); + } }