Skip to content

Commit b995c21

Browse files
[String] add types to all arguments
1 parent 527f14f commit b995c21

File tree

7 files changed

+81
-108
lines changed

7 files changed

+81
-108
lines changed

AbstractString.php

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,16 @@ public static function wrap(array $values): array
9595
*
9696
* @return static
9797
*/
98-
public function after($needle, bool $includeNeedle = false, int $offset = 0): self
98+
public function after(string|iterable $needle, bool $includeNeedle = false, int $offset = 0): self
9999
{
100100
$str = clone $this;
101101
$i = \PHP_INT_MAX;
102102

103-
foreach ((array) $needle as $n) {
103+
if (\is_string($needle)) {
104+
$needle = [$needle];
105+
}
106+
107+
foreach ($needle as $n) {
104108
$n = (string) $n;
105109
$j = $this->indexOf($n, $offset);
106110

@@ -126,12 +130,16 @@ public function after($needle, bool $includeNeedle = false, int $offset = 0): se
126130
*
127131
* @return static
128132
*/
129-
public function afterLast($needle, bool $includeNeedle = false, int $offset = 0): self
133+
public function afterLast(string|iterable $needle, bool $includeNeedle = false, int $offset = 0): self
130134
{
131135
$str = clone $this;
132136
$i = null;
133137

134-
foreach ((array) $needle as $n) {
138+
if (\is_string($needle)) {
139+
$needle = [$needle];
140+
}
141+
142+
foreach ($needle as $n) {
135143
$n = (string) $n;
136144
$j = $this->indexOfLast($n, $offset);
137145

@@ -162,12 +170,16 @@ abstract public function append(string ...$suffix): self;
162170
*
163171
* @return static
164172
*/
165-
public function before($needle, bool $includeNeedle = false, int $offset = 0): self
173+
public function before(string|iterable $needle, bool $includeNeedle = false, int $offset = 0): self
166174
{
167175
$str = clone $this;
168176
$i = \PHP_INT_MAX;
169177

170-
foreach ((array) $needle as $n) {
178+
if (\is_string($needle)) {
179+
$needle = [$needle];
180+
}
181+
182+
foreach ($needle as $n) {
171183
$n = (string) $n;
172184
$j = $this->indexOf($n, $offset);
173185

@@ -193,12 +205,16 @@ public function before($needle, bool $includeNeedle = false, int $offset = 0): s
193205
*
194206
* @return static
195207
*/
196-
public function beforeLast($needle, bool $includeNeedle = false, int $offset = 0): self
208+
public function beforeLast(string|iterable $needle, bool $includeNeedle = false, int $offset = 0): self
197209
{
198210
$str = clone $this;
199211
$i = null;
200212

201-
foreach ((array) $needle as $n) {
213+
if (\is_string($needle)) {
214+
$needle = [$needle];
215+
}
216+
217+
foreach ($needle as $n) {
202218
$n = (string) $n;
203219
$j = $this->indexOfLast($n, $offset);
204220

@@ -253,17 +269,17 @@ public function collapseWhitespace(): self
253269
/**
254270
* @param string|string[] $needle
255271
*/
256-
public function containsAny($needle): bool
272+
public function containsAny(string|iterable $needle): bool
257273
{
258274
return null !== $this->indexOf($needle);
259275
}
260276

261277
/**
262278
* @param string|string[] $suffix
263279
*/
264-
public function endsWith($suffix): bool
280+
public function endsWith(string|iterable $suffix): bool
265281
{
266-
if (!\is_array($suffix) && !$suffix instanceof \Traversable) {
282+
if (\is_string($suffix)) {
267283
throw new \TypeError(sprintf('Method "%s()" must be overridden by class "%s" to deal with non-iterable values.', __FUNCTION__, static::class));
268284
}
269285

@@ -316,9 +332,9 @@ public function ensureStart(string $prefix): self
316332
/**
317333
* @param string|string[] $string
318334
*/
319-
public function equalsTo($string): bool
335+
public function equalsTo(string|iterable $string): bool
320336
{
321-
if (!\is_array($string) && !$string instanceof \Traversable) {
337+
if (\is_string($string)) {
322338
throw new \TypeError(sprintf('Method "%s()" must be overridden by class "%s" to deal with non-iterable values.', __FUNCTION__, static::class));
323339
}
324340

@@ -350,9 +366,9 @@ public function ignoreCase(): self
350366
/**
351367
* @param string|string[] $needle
352368
*/
353-
public function indexOf($needle, int $offset = 0): ?int
369+
public function indexOf(string|iterable $needle, int $offset = 0): ?int
354370
{
355-
if (!\is_array($needle) && !$needle instanceof \Traversable) {
371+
if (\is_string($needle)) {
356372
throw new \TypeError(sprintf('Method "%s()" must be overridden by class "%s" to deal with non-iterable values.', __FUNCTION__, static::class));
357373
}
358374

@@ -372,9 +388,9 @@ public function indexOf($needle, int $offset = 0): ?int
372388
/**
373389
* @param string|string[] $needle
374390
*/
375-
public function indexOfLast($needle, int $offset = 0): ?int
391+
public function indexOfLast(string|iterable $needle, int $offset = 0): ?int
376392
{
377-
if (!\is_array($needle) && !$needle instanceof \Traversable) {
393+
if (\is_string($needle)) {
378394
throw new \TypeError(sprintf('Method "%s()" must be overridden by class "%s" to deal with non-iterable values.', __FUNCTION__, static::class));
379395
}
380396

@@ -467,7 +483,7 @@ abstract public function replace(string $from, string $to): self;
467483
*
468484
* @return static
469485
*/
470-
abstract public function replaceMatches(string $fromRegexp, $to): self;
486+
abstract public function replaceMatches(string $fromRegexp, string|callable $to): self;
471487

472488
/**
473489
* @return static
@@ -540,9 +556,9 @@ public function split(string $delimiter, int $limit = null, int $flags = null):
540556
/**
541557
* @param string|string[] $prefix
542558
*/
543-
public function startsWith($prefix): bool
559+
public function startsWith(string|iterable $prefix): bool
544560
{
545-
if (!\is_array($prefix) && !$prefix instanceof \Traversable) {
561+
if (\is_string($prefix)) {
546562
throw new \TypeError(sprintf('Method "%s()" must be overridden by class "%s" to deal with non-iterable values.', __FUNCTION__, static::class));
547563
}
548564

AbstractUnicodeString.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public function padStart(int $length, string $padStr = ' '): parent
303303
return $this->pad($length, $pad, \STR_PAD_LEFT);
304304
}
305305

306-
public function replaceMatches(string $fromRegexp, $to): parent
306+
public function replaceMatches(string $fromRegexp, string|callable $to): parent
307307
{
308308
if ($this->ignoreCase) {
309309
$fromRegexp .= 'i';

ByteString.php

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -129,27 +129,23 @@ public function chunk(int $length = 1): array
129129
return $chunks;
130130
}
131131

132-
public function endsWith($suffix): bool
132+
public function endsWith(string|iterable|AbstractString $suffix): bool
133133
{
134-
if ($suffix instanceof parent) {
134+
if ($suffix instanceof AbstractString) {
135135
$suffix = $suffix->string;
136-
} elseif (\is_array($suffix) || $suffix instanceof \Traversable) {
136+
} elseif (!\is_string($suffix)) {
137137
return parent::endsWith($suffix);
138-
} else {
139-
$suffix = (string) $suffix;
140138
}
141139

142140
return '' !== $suffix && \strlen($this->string) >= \strlen($suffix) && 0 === substr_compare($this->string, $suffix, -\strlen($suffix), null, $this->ignoreCase);
143141
}
144142

145-
public function equalsTo($string): bool
143+
public function equalsTo(string|iterable|AbstractString $string): bool
146144
{
147-
if ($string instanceof parent) {
145+
if ($string instanceof AbstractString) {
148146
$string = $string->string;
149-
} elseif (\is_array($string) || $string instanceof \Traversable) {
147+
} elseif (!\is_string($string)) {
150148
return parent::equalsTo($string);
151-
} else {
152-
$string = (string) $string;
153149
}
154150

155151
if ('' !== $string && $this->ignoreCase) {
@@ -167,14 +163,12 @@ public function folded(): parent
167163
return $str;
168164
}
169165

170-
public function indexOf($needle, int $offset = 0): ?int
166+
public function indexOf(string|iterable|AbstractString $needle, int $offset = 0): ?int
171167
{
172-
if ($needle instanceof parent) {
168+
if ($needle instanceof AbstractString) {
173169
$needle = $needle->string;
174-
} elseif (\is_array($needle) || $needle instanceof \Traversable) {
170+
} elseif (!\is_string($needle)) {
175171
return parent::indexOf($needle, $offset);
176-
} else {
177-
$needle = (string) $needle;
178172
}
179173

180174
if ('' === $needle) {
@@ -186,14 +180,12 @@ public function indexOf($needle, int $offset = 0): ?int
186180
return false === $i ? null : $i;
187181
}
188182

189-
public function indexOfLast($needle, int $offset = 0): ?int
183+
public function indexOfLast(string|iterable|AbstractString $needle, int $offset = 0): ?int
190184
{
191-
if ($needle instanceof parent) {
185+
if ($needle instanceof AbstractString) {
192186
$needle = $needle->string;
193-
} elseif (\is_array($needle) || $needle instanceof \Traversable) {
187+
} elseif (!\is_string($needle)) {
194188
return parent::indexOfLast($needle, $offset);
195-
} else {
196-
$needle = (string) $needle;
197189
}
198190

199191
if ('' === $needle) {
@@ -305,7 +297,7 @@ public function replace(string $from, string $to): parent
305297
return $str;
306298
}
307299

308-
public function replaceMatches(string $fromRegexp, $to): parent
300+
public function replaceMatches(string $fromRegexp, string|callable $to): parent
309301
{
310302
if ($this->ignoreCase) {
311303
$fromRegexp .= 'i';
@@ -404,9 +396,9 @@ public function split(string $delimiter, int $limit = null, int $flags = null):
404396
return $chunks;
405397
}
406398

407-
public function startsWith($prefix): bool
399+
public function startsWith(string|iterable|AbstractString $prefix): bool
408400
{
409-
if ($prefix instanceof parent) {
401+
if ($prefix instanceof AbstractString) {
410402
$prefix = $prefix->string;
411403
} elseif (!\is_string($prefix)) {
412404
return parent::startsWith($prefix);

CodePointString.php

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,12 @@ public function codePointsAt(int $offset): array
8080
return '' === $str->string ? [] : [mb_ord($str->string, 'UTF-8')];
8181
}
8282

83-
public function endsWith($suffix): bool
83+
public function endsWith(string|iterable|AbstractString $suffix): bool
8484
{
8585
if ($suffix instanceof AbstractString) {
8686
$suffix = $suffix->string;
87-
} elseif (\is_array($suffix) || $suffix instanceof \Traversable) {
87+
} elseif (!\is_string($suffix)) {
8888
return parent::endsWith($suffix);
89-
} else {
90-
$suffix = (string) $suffix;
9189
}
9290

9391
if ('' === $suffix || !preg_match('//u', $suffix)) {
@@ -101,14 +99,12 @@ public function endsWith($suffix): bool
10199
return \strlen($this->string) >= \strlen($suffix) && 0 === substr_compare($this->string, $suffix, -\strlen($suffix));
102100
}
103101

104-
public function equalsTo($string): bool
102+
public function equalsTo(string|iterable|AbstractString $string): bool
105103
{
106104
if ($string instanceof AbstractString) {
107105
$string = $string->string;
108-
} elseif (\is_array($string) || $string instanceof \Traversable) {
106+
} elseif (!\is_string($string)) {
109107
return parent::equalsTo($string);
110-
} else {
111-
$string = (string) $string;
112108
}
113109

114110
if ('' !== $string && $this->ignoreCase) {
@@ -118,14 +114,12 @@ public function equalsTo($string): bool
118114
return $string === $this->string;
119115
}
120116

121-
public function indexOf($needle, int $offset = 0): ?int
117+
public function indexOf(string|iterable|AbstractString $needle, int $offset = 0): ?int
122118
{
123119
if ($needle instanceof AbstractString) {
124120
$needle = $needle->string;
125-
} elseif (\is_array($needle) || $needle instanceof \Traversable) {
121+
} elseif (!\is_string($needle)) {
126122
return parent::indexOf($needle, $offset);
127-
} else {
128-
$needle = (string) $needle;
129123
}
130124

131125
if ('' === $needle) {
@@ -137,14 +131,12 @@ public function indexOf($needle, int $offset = 0): ?int
137131
return false === $i ? null : $i;
138132
}
139133

140-
public function indexOfLast($needle, int $offset = 0): ?int
134+
public function indexOfLast(string|iterable|AbstractString $needle, int $offset = 0): ?int
141135
{
142136
if ($needle instanceof AbstractString) {
143137
$needle = $needle->string;
144-
} elseif (\is_array($needle) || $needle instanceof \Traversable) {
138+
} elseif (!\is_string($needle)) {
145139
return parent::indexOfLast($needle, $offset);
146-
} else {
147-
$needle = (string) $needle;
148140
}
149141

150142
if ('' === $needle) {
@@ -247,14 +239,12 @@ public function split(string $delimiter, int $limit = null, int $flags = null):
247239
return $chunks;
248240
}
249241

250-
public function startsWith($prefix): bool
242+
public function startsWith(string|iterable|AbstractString $prefix): bool
251243
{
252244
if ($prefix instanceof AbstractString) {
253245
$prefix = $prefix->string;
254-
} elseif (\is_array($prefix) || $prefix instanceof \Traversable) {
246+
} elseif (!\is_string($prefix)) {
255247
return parent::startsWith($prefix);
256-
} else {
257-
$prefix = (string) $prefix;
258248
}
259249

260250
if ('' === $prefix || !preg_match('//u', $prefix)) {

LazyString.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ class LazyString implements \Stringable, \JsonSerializable
2525
*
2626
* @return static
2727
*/
28-
public static function fromCallable($callback, ...$arguments): self
28+
public static function fromCallable(callable|array $callback, mixed ...$arguments): self
2929
{
30-
if (!\is_callable($callback) && !(\is_array($callback) && isset($callback[0]) && $callback[0] instanceof \Closure && 2 >= \count($callback))) {
31-
throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be a callable or a [Closure, method] lazy-callable, "%s" given.', __METHOD__, get_debug_type($callback)));
30+
if (\is_array($callback) && !\is_callable($callback) && !(($callback[0] ?? null) instanceof \Closure || 2 < \count($callback))) {
31+
throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be a callable or a [Closure, method] lazy-callable, "%s" given.', __METHOD__, '['.implode(', ', array_map('get_debug_type', $callback)).']'));
3232
}
3333

3434
$lazyString = new static();
@@ -50,16 +50,10 @@ public static function fromCallable($callback, ...$arguments): self
5050
}
5151

5252
/**
53-
* @param string|int|float|bool|\Stringable $value
54-
*
5553
* @return static
5654
*/
57-
public static function fromStringable($value): self
55+
public static function fromStringable(string|int|float|bool|\Stringable $value): self
5856
{
59-
if (!self::isStringable($value)) {
60-
throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be a scalar or a stringable object, "%s" given.', __METHOD__, get_debug_type($value)));
61-
}
62-
6357
if (\is_object($value)) {
6458
return static::fromCallable([$value, '__toString']);
6559
}
@@ -73,19 +67,17 @@ public static function fromStringable($value): self
7367
/**
7468
* Tells whether the provided value can be cast to string.
7569
*/
76-
final public static function isStringable($value): bool
70+
final public static function isStringable(mixed $value): bool
7771
{
78-
return \is_string($value) || $value instanceof self || (\is_object($value) ? method_exists($value, '__toString') : is_scalar($value));
72+
return \is_string($value) || $value instanceof \Stringable || is_scalar($value);
7973
}
8074

8175
/**
8276
* Casts scalars and stringable objects to strings.
8377
*
84-
* @param object|string|int|float|bool $value
85-
*
8678
* @throws \TypeError When the provided value is not stringable
8779
*/
88-
final public static function resolve($value): string
80+
final public static function resolve(\Stringable|string|int|float|bool $value): string
8981
{
9082
return $value;
9183
}

0 commit comments

Comments
 (0)