Skip to content

Commit 551f6de

Browse files
committed
Narrow to non-empty-string via Strings::length
1 parent 845f5d0 commit 551f6de

4 files changed

Lines changed: 45 additions & 1 deletion

File tree

extension.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ parameters:
2929
- stubs/Utils/Html.stub
3030
- stubs/Utils/Paginator.stub
3131
- stubs/Utils/Random.stub
32+
- stubs/Utils/Strings.stub
3233
universalObjectCratesClasses:
3334
- Nette\Application\UI\ITemplate
3435
- Nette\Application\UI\Template

stubs/Utils/Strings.stub

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Nette\Utils;
4+
5+
use Nette;
6+
7+
8+
/**
9+
* String tools library.
10+
*/
11+
class Strings
12+
{
13+
use Nette\StaticClass;
14+
15+
/** @phpstan-assert-if-true non-empty-string $s */
16+
public static function length(string $s): int
17+
{}
18+
19+
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use function class_exists;
99
use function version_compare;
1010

11-
class MultiplierTest extends TypeInferenceTestCase
11+
class TypeInferenceTest extends TypeInferenceTestCase
1212
{
1313

1414
public function dataFileAsserts(): iterable
@@ -26,6 +26,9 @@ public function dataFileAsserts(): iterable
2626
} else {
2727
yield from self::gatherAssertTypes(__DIR__ . '/data/multiplier.php');
2828
}
29+
30+
yield from $this->gatherAssertTypes(__DIR__ . '/data/multiplier.php');
31+
yield from $this->gatherAssertTypes(__DIR__ . '/data/strings.php');
2932
}
3033

3134
/**

tests/Type/Nette/data/strings.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace StringsTypesNarrowing;
4+
5+
use Nette\Utils\Strings;
6+
use function PHPStan\Testing\assertType;
7+
8+
function doFoo(string $string) {
9+
assertType('string', $string);
10+
if (Strings::length($string)) {
11+
assertType('non-empty-string', $string);
12+
} else {
13+
assertType('string', $string);
14+
}
15+
assertType('string', $string);
16+
17+
if (Strings::length($string) === 0) {
18+
assertType('string', $string);
19+
}
20+
assertType('string', $string);
21+
}

0 commit comments

Comments
 (0)