Skip to content

Commit c724df7

Browse files
committed
Narrow type after Str::is*(...) check
1 parent 620d6ab commit c724df7

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/Illuminate/Support/Str.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,8 @@ public static function isAscii($value)
569569
*
570570
* @param mixed $value
571571
* @return bool
572+
*
573+
* @phpstan-assert-if-true =non-empty-string $value
572574
*/
573575
public static function isJson($value)
574576
{
@@ -585,6 +587,8 @@ public static function isJson($value)
585587
* @param mixed $value
586588
* @param array $protocols
587589
* @return bool
590+
*
591+
* @phpstan-assert-if-true =non-empty-string $value
588592
*/
589593
public static function isUrl($value, array $protocols = [])
590594
{
@@ -628,6 +632,8 @@ public static function isUrl($value, array $protocols = [])
628632
* @param mixed $value
629633
* @param int<0, 8>|'nil'|'max'|null $version
630634
* @return bool
635+
*
636+
* @phpstan-assert-if-true =non-empty-string $value
631637
*/
632638
public static function isUuid($value, $version = null)
633639
{
@@ -669,6 +675,8 @@ public static function isUuid($value, $version = null)
669675
*
670676
* @param mixed $value
671677
* @return bool
678+
*
679+
* @phpstan-assert-if-true =non-empty-string $value
672680
*/
673681
public static function isUlid($value)
674682
{

types/Support/Str.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Illuminate\Support\Str;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @var mixed $json
9+
* @var mixed $ulid
10+
* @var mixed $url
11+
* @var mixed $uuid
12+
*/
13+
if (Str::isJson($json)) {
14+
assertType('non-empty-string', $json);
15+
} else {
16+
assertType('mixed', $json);
17+
}
18+
19+
if (Str::isUlid($ulid)) {
20+
assertType('non-empty-string', $ulid);
21+
} else {
22+
assertType('mixed', $ulid);
23+
}
24+
25+
if (Str::isUrl($url)) {
26+
assertType('non-empty-string', $url);
27+
} else {
28+
assertType('mixed', $url);
29+
}
30+
31+
if (Str::isUuid($uuid)) {
32+
assertType('non-empty-string', $uuid);
33+
} else {
34+
assertType('mixed', $uuid);
35+
}

0 commit comments

Comments
 (0)