Skip to content

Tester\Assert type narrowing improvements - #1

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

xificurk wants to merge 3 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)

@xificurk
xificurk force-pushed the tester-assert-narrowing branch from 920d020 to 62c3e14 Compare November 1, 2026 12:44
Narrows return types based on a configurable table-to-entity-class
mapping (nette.database.mapping.tables), which is not yet part of the
nette/database distribution. Kept as the topmost commit so the whole
feature can be excluded or rebased independently.

Includes:
- TableRowTypeResolver shared resolver
- Explorer::table()        -> Selection<EntityRow>
- ActiveRow::related()     -> GroupedSelection<EntityRow>
- ActiveRow::ref()         -> ?EntityRow
- Selection::insert()      -> mapped EntityRow
- 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
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.

2 participants