Skip to content

Commit 29ba4cb

Browse files
committed
Strings::before(), after(), indexOf() and pos() return NULL instead of FALSE if the needle was not found (BC break)
1 parent fcad715 commit 29ba4cb

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

src/Utils/Strings.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -353,51 +353,51 @@ public static function reverse(string $s): string
353353

354354
/**
355355
* Returns part of $haystack before $nth occurence of $needle (negative value means searching from the end).
356-
* @return string|FALSE returns FALSE if the needle was not found
356+
* @return string|NULL returns NULL if the needle was not found
357357
*/
358358
public static function before(string $haystack, string $needle, int $nth = 1)
359359
{
360360
$pos = self::pos($haystack, $needle, $nth);
361-
return $pos === FALSE
362-
? FALSE
361+
return $pos === NULL
362+
? NULL
363363
: substr($haystack, 0, $pos);
364364
}
365365

366366

367367
/**
368368
* Returns part of $haystack after $nth occurence of $needle (negative value means searching from the end).
369-
* @return string|FALSE returns FALSE if the needle was not found
369+
* @return string|NULL returns NULL if the needle was not found
370370
*/
371371
public static function after(string $haystack, string $needle, int $nth = 1)
372372
{
373373
$pos = self::pos($haystack, $needle, $nth);
374-
return $pos === FALSE
375-
? FALSE
374+
return $pos === NULL
375+
? NULL
376376
: substr($haystack, $pos + strlen($needle));
377377
}
378378

379379

380380
/**
381381
* Returns position of $nth occurence of $needle in $haystack (negative value means searching from the end).
382-
* @return int|FALSE offset in characters or FALSE if the needle was not found
382+
* @return int|NULL offset in characters or NULL if the needle was not found
383383
*/
384384
public static function indexOf(string $haystack, string $needle, int $nth = 1)
385385
{
386386
$pos = self::pos($haystack, $needle, $nth);
387-
return $pos === FALSE
388-
? FALSE
387+
return $pos === NULL
388+
? NULL
389389
: self::length(substr($haystack, 0, $pos));
390390
}
391391

392392

393393
/**
394394
* Returns position of $nth occurence of $needle in $haystack.
395-
* @return int|FALSE offset in bytes or FALSE if the needle was not found
395+
* @return int|NULL offset in bytes or NULL if the needle was not found
396396
*/
397397
private static function pos(string $haystack, string $needle, int $nth = 1)
398398
{
399399
if (!$nth) {
400-
return FALSE;
400+
return NULL;
401401
} elseif ($nth > 0) {
402402
if (strlen($needle) === 0) {
403403
return 0;
@@ -416,7 +416,7 @@ private static function pos(string $haystack, string $needle, int $nth = 1)
416416
$pos--;
417417
}
418418
}
419-
return $pos;
419+
return $pos === FALSE ? NULL : $pos;
420420
}
421421

422422

tests/Utils/Strings.after().phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ test(function () {
2727
Assert::same('c', Strings::after($foo, '789', 3));
2828
Assert::same('a123456789b123456789c', Strings::after($foo, '9', -3));
2929
Assert::same('a123456789b123456789c', Strings::after($foo, '789', -3));
30-
Assert::false(Strings::after($foo, '9', 0));
31-
Assert::false(Strings::after($foo, 'not-in-string'));
32-
Assert::false(Strings::after($foo, 'b', -2));
33-
Assert::false(Strings::after($foo, 'b', 2));
30+
Assert::null(Strings::after($foo, '9', 0));
31+
Assert::null(Strings::after($foo, 'not-in-string'));
32+
Assert::null(Strings::after($foo, 'b', -2));
33+
Assert::null(Strings::after($foo, 'b', 2));
3434
});
3535

3636

tests/Utils/Strings.before().phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ test(function () {
2626
Assert::same('0123456789a123456789b123456', Strings::before($foo, '789', 3));
2727
Assert::same('012345678', Strings::before($foo, '9', -3));
2828
Assert::same('0123456', Strings::before($foo, '789', -3));
29-
Assert::false(Strings::before($foo, '9', 0));
30-
Assert::false(Strings::before($foo, 'not-in-string'));
31-
Assert::false(Strings::before($foo, 'b', -2));
32-
Assert::false(Strings::before($foo, 'b', 2));
29+
Assert::null(Strings::before($foo, '9', 0));
30+
Assert::null(Strings::before($foo, 'not-in-string'));
31+
Assert::null(Strings::before($foo, 'b', -2));
32+
Assert::null(Strings::before($foo, 'b', 2));
3333
});
3434

3535

tests/Utils/Strings.indexOf().phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ test(function () {
2727
Assert::same(27, Strings::indexOf($foo, '789', 3));
2828
Assert::same(9, Strings::indexOf($foo, '9', -3));
2929
Assert::same(7, Strings::indexOf($foo, '789', -3));
30-
Assert::false(Strings::indexOf($foo, '9', 0));
31-
Assert::false(Strings::indexOf($foo, 'not-in-string'));
32-
Assert::false(Strings::indexOf($foo, 'b', -2));
33-
Assert::false(Strings::indexOf($foo, 'b', 2));
30+
Assert::null(Strings::indexOf($foo, '9', 0));
31+
Assert::null(Strings::indexOf($foo, 'not-in-string'));
32+
Assert::null(Strings::indexOf($foo, 'b', -2));
33+
Assert::null(Strings::indexOf($foo, 'b', 2));
3434
});
3535

3636

0 commit comments

Comments
 (0)