Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ parameters:
- stubs/Utils/Html.stub
- stubs/Utils/Paginator.stub
- stubs/Utils/Random.stub
- stubs/Utils/Strings.stub
universalObjectCratesClasses:
- Nette\Application\UI\ITemplate
- Nette\Application\UI\Template
Expand Down
17 changes: 17 additions & 0 deletions stubs/Utils/Strings.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Nette\Utils;

use Nette;


/**
* String tools library.
*/
class Strings
{
/** @phpstan-assert-if-true non-empty-string $s */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kinda hacky given the fact it doesn't return true, no ? Does the same strategy is used somewhere else ?

@staabm staabm Jun 24, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh thats a great point. maybe we should even error in such case?

public static function length(string $s): int
{}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use function class_exists;
use function version_compare;

class MultiplierTest extends TypeInferenceTestCase
class TypeInferenceTest extends TypeInferenceTestCase
{

public function dataFileAsserts(): iterable
Expand All @@ -26,6 +26,8 @@ public function dataFileAsserts(): iterable
} else {
yield from self::gatherAssertTypes(__DIR__ . '/data/multiplier.php');
}

yield from $this->gatherAssertTypes(__DIR__ . '/data/strings.php');
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/Type/Nette/data/strings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace StringsTypesNarrowing;

use Nette\Utils\Strings;
use function PHPStan\Testing\assertType;

function doFoo(string $string) {
assertType('string', $string);
if (Strings::length($string)) {
assertType('non-empty-string', $string);
} else {
assertType('string', $string);
}
assertType('string', $string);

if (Strings::length($string) === 0) {
assertType('string', $string);
}
assertType('string', $string);
}
Loading