Skip to content

Tester\Assert type narrowing improvements - #1

Open
xificurk wants to merge 2 commits into
nette:masterfrom
xificurk:tester-assert-narrowing
Open

xificurk wants to merge 2 commits into
nette:masterfrom
xificurk:tester-assert-narrowing

Conversation

@xificurk

@xificurk xificurk commented Nov 1, 2026 •

Copy link
Copy Markdown
  • new feature
  • BC break no

Porting the missing support for type-narrowing of Tester\Assert methods from https://github.com/nepada/phpstan-nette-tester

  • robustness fixes for code edge cases:
    • unpacked arguments and first-class callables are skipped (Assert::null(...$args) used to narrow $args to never)
    • Assert::type('', $x) no longer crashes the analysis with "Name cannot be empty"
    • named arguments PHPStan couldn't resolve to positions (an invalid call) are skipped (Assert::null(description: $d) used to narrow $d to null)
    • a leading backslash in a class name is ignored (Assert::type('\stdClass', $x))
  • narrowing for:
    • Assert::nan($x) → float
    • Assert::count($n, $x) → array|Countable with count($x) === $n (e.g. non-empty-array for $n > 0)
    • Assert::hasKey($k, $x) / Assert::hasNotKey($k, $x) → via array_key_exists(), e.g. array{foo?: int} → array{foo: int}
    • Assert::type($type, $x) with a union of constant strings ('int'|'string' → int|string)
    • Assert::type('list', $x) → list (was array)

- unpacked arguments and first-class callables are declined
  (Assert::null(...$args) narrowed $args to never)
- named arguments PHPStan couldn't resolve to positions (an invalid call)
  are declined (Assert::null(description: $d) narrowed $d to null)
- type('') no longer crashes on an empty class name
- type() ignores a leading backslash in a class name

Co-Authored-By: Claude Code
…pe() accepts unions and list

- nan() narrows to float, count() to array|Countable with count() === $n,
  hasKey()/hasNotKey() via array_key_exists()
- type() with a union of constant strings narrows to the union of the types,
  'list' narrows to list

Co-Authored-By: Claude Code
@xificurk
xificurk force-pushed the tester-assert-narrowing branch from 62c3e14 to 78f014e Compare November 7, 2026 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant