Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions Tests/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static function seedTestStrpos(): array
[0, 'на карте с', 'на карте', 0],
[false, 'на карте с', 'на каррте', 0],
[false, 'на карте с', 'на карте', 2],
[3, 'missing', 'sing', false],
[3, 'missing', 'sing', null],
];
}

Expand Down Expand Up @@ -99,10 +99,10 @@ public static function seedTestStrrpos(): array
public static function seedTestSubstr(): array
{
return [
['issauga', 'Mississauga', 4, false],
['на карте с', ' объектов на карте с', 10, false],
['issauga', 'Mississauga', 4, null],
['на карте с', ' объектов на карте с', 10, null],
['на ка', ' объектов на карте с', 10, 5],
['те с', ' объектов на карте с', -4, false],
['те с', ' объектов на карте с', -4, null],
[false, ' объектов на карте с', 99, false],
];
}
Expand Down Expand Up @@ -151,12 +151,12 @@ public static function seedTestStrlen(): array
public static function seedTestStr_ireplace(): array
{
return [
['Pig', 'cow', 'the pig jumped', false, 'the cow jumped'],
['Pig', 'cow', 'the pig jumped', true, 'the cow jumped'],
['Pig', 'cow', 'the pig jumped over the cow', true, 'the cow jumped over the cow'],
[['PIG', 'JUMPED'], ['cow', 'hopped'], 'the pig jumped over the pig', true, 'the cow hopped over the cow'],
['шил', 'биш', 'Би шил идэй чадна', true, 'Би биш идэй чадна'],
['/', ':', '/test/slashes/', true, ':test:slashes:'],
['Pig', 'cow', 'the pig jumped', null, 'the cow jumped'],
['Pig', 'cow', 'the pig jumped', null, 'the cow jumped'],
['Pig', 'cow', 'the pig jumped over the cow', null, 'the cow jumped over the cow'],
[['PIG', 'JUMPED'], ['cow', 'hopped'], 'the pig jumped over the pig', null, 'the cow hopped over the cow'],
['шил', 'биш', 'Би шил идэй чадна', null, 'Би биш идэй чадна'],
['/', ':', '/test/slashes/', null, ':test:slashes:'],
];
}

Expand Down Expand Up @@ -304,9 +304,9 @@ public static function seedTestStrspn(): array
public static function seedTestSubstr_replace(): array
{
return [
['321 Broadway Avenue', '321 Main Street', 'Broadway Avenue', 4, false],
['321 Broadway Avenue', '321 Main Street', 'Broadway Avenue', 4, null],
['321 Broadway Street', '321 Main Street', 'Broadway', 4, 4],
['чадна 我能吞', 'чадна Би шил идэй чадна', '我能吞', 6, false],
['чадна 我能吞', 'чадна Би шил идэй чадна', '我能吞', 6, null],
['чадна 我能吞 шил идэй чадна', 'чадна Би шил идэй чадна', '我能吞', 6, 2],
];
}
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* [Overview](overview.md)
* [Updating from v4 to v5](v4-to-v5-update.md)
33 changes: 33 additions & 0 deletions docs/v4-to-v5-update.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## Updating from v4 to v5

The following changes were made to the String package between version 4 and version 5.

### Native parameter and return types

All methods of `StringHelper` and `Normalise` have native parameter and return types. Passing a value
of the wrong type, for example `null` instead of a string, now throws a `TypeError`. A subclass that
overrides one of these static methods must use compatible signatures.

The `false` sentinels for optional integer parameters have been replaced with `null`:

| Method | 4.x | 5.x |
|---|---|---|
| `StringHelper::strpos()` | `$offset = false` | `?int $offset = null` |
| `StringHelper::substr()` | `$length = false` | `?int $length = null` |
| `StringHelper::substr_replace()` | `$length = null`, `false` also accepted | `?int $length = null` |
| `StringHelper::str_ireplace()` | `$count = null`, `true`/`false` also meant "no limit" | `?int $count = null` |
| `StringHelper::strcspn()`, `strspn()` | `$start`/`$length` also accepted `false` | `?int $start = null, ?int $length = null` |

Without `declare(strict_types=1)`, PHP converts `false` to `0` and `true` to `1` for an `int`
parameter instead of throwing, which changes the result silently. Replace these calls:

* `StringHelper::substr($str, $offset, false)` returned the rest of the string and now returns `''`. Pass `null` or leave the argument out.
* `StringHelper::substr_replace($str, $repl, $start, false)` replaced up to the end of the string and now inserts without replacing. Pass `null`.
* `StringHelper::str_ireplace($search, $replace, $str, true)` replaced every occurrence and now replaces only the first one. Pass `null`.

Other signature changes:

* `strcmp()` and `strcasecmp()` take `string|false $locale`. `strcmp()` used to accept any value and treated falsy values as "no locale".
* `ltrim()`, `rtrim()` and `trim()` keep `string|false $charlist = false`.
* `ord()`, `strpos()`, `strrpos()` and `stristr()` declare the `false` they return when nothing is found. `transcode()` declares `string|false`, because `iconv()` returns `false` on failure. `strtolower()`, `strtoupper()` and `substr()` return `string` only, as the `mb_*` functions they wrap have done since PHP 8.0.
* The protected property `StringHelper::$incrementStyles` is typed `array`.
14 changes: 7 additions & 7 deletions src/Normalise.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ abstract class Normalise
*
* @since 1.0
*/
public static function fromCamelCase($input, $grouped = false)
public static function fromCamelCase(string $input, bool $grouped = false): array|string
{
return $grouped
? preg_split('/(?<=[^A-Z_])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][^A-Z_])/x', $input)
Expand All @@ -54,7 +54,7 @@ public static function fromCamelCase($input, $grouped = false)
*
* @since 1.0
*/
public static function toCamelCase($input)
public static function toCamelCase(string $input): string
{
// Convert words to uppercase and then remove spaces.
$input = static::toSpaceSeparated($input);
Expand All @@ -73,7 +73,7 @@ public static function toCamelCase($input)
*
* @since 1.0
*/
public static function toDashSeparated($input)
public static function toDashSeparated(string $input): string
{
// Convert spaces and underscores to dashes.
return preg_replace('#[ \-_]+#', '-', $input);
Expand All @@ -88,7 +88,7 @@ public static function toDashSeparated($input)
*
* @since 1.0
*/
public static function toSpaceSeparated($input)
public static function toSpaceSeparated(string $input): string
{
// Convert underscores and dashes to spaces.
return preg_replace('#[ \-_]+#', ' ', $input);
Expand All @@ -103,7 +103,7 @@ public static function toSpaceSeparated($input)
*
* @since 1.0
*/
public static function toUnderscoreSeparated($input)
public static function toUnderscoreSeparated(string $input): string
{
// Convert spaces and dashes to underscores.
return preg_replace('#[ \-_]+#', '_', $input);
Expand All @@ -118,7 +118,7 @@ public static function toUnderscoreSeparated($input)
*
* @since 1.0
*/
public static function toVariable($input)
public static function toVariable(string $input): string
{
// Remove dashes and underscores, then convert to camel case.
$input = static::toCamelCase($input);
Expand All @@ -141,7 +141,7 @@ public static function toVariable($input)
*
* @since 1.0
*/
public static function toKey($input)
public static function toKey(string $input): string
{
// Remove spaces and dashes, then convert to lower case.
return strtolower(static::toUnderscoreSeparated($input));
Expand Down
Loading
Loading