Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ to `TypeSpecifier::specifyTypesInCondition()`. Supports `null`, `notNull`, `true
`truthy`, `falsey`, `same`, `notSame`, `type` (built-in type strings and class/interface names).
A shipped feature, not test-only.

### Assert::fail() as early-terminating (earlyTerminatingMethodCalls, not a PHP extension)

`Tester\Assert::fail()` is listed in `earlyTerminatingMethodCalls`, so code after it is
unreachable and narrowing holds (`if ($x === null) Assert::fail(...)` leaves `$x` non-null). It
can't be native `never`: an `Assert::$onFailure` handler may swallow the failure and continue.
Consistent with the type-narrowing assertions, which assume the same.

### Assets family (`MapperTypeResolver` shared service)

`MapperTypeResolver` resolves mapper IDs → mapper class types from a `mapping` config
Expand Down
6 changes: 6 additions & 0 deletions extension-nette.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ parameters:
- Nette\Security\SimpleIdentity
- Nette\Utils\Html

earlyTerminatingMethodCalls:
# fail() returns void because an Assert::$onFailure handler may swallow the failure,
# but like every other assertion it is meant to end the test
Tester\Assert:
- fail

ignoreErrors:
-
# Form event-handler callbacks (onSuccess, onClick, ...) receive data coerced
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ parameters:

**Subject narrowing after match** — inside `if (Strings::match($s, '#\d+#'))` the subject `$s` is narrowed (e.g. to `non-empty-string`) based on the pattern.

**Assert type narrowing** — PHPStan understands type guarantees after `Tester\Assert` calls like `notNull()`, `type()`, `true()`, etc.
**Assert type narrowing** — PHPStan understands type guarantees after `Tester\Assert` calls like `notNull()`, `type()`, `true()`, etc. Code after `Assert::fail()` is treated as unreachable.

**False positive suppression** — silences known PHPStan false positives in Nette patterns (arrow functions passed as `void` callbacks, runtime type validation closures, callbacks assigned to Form event-handler properties like `$onSuccess`, `$onClick`).

Expand Down
1 change: 0 additions & 1 deletion src/Tester/TypeAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ private static function processAssertTypeCall(Node $node, Scope $scope): ?array
$expectedType->describe(VerbosityLevel::precise()),
$node->getStartLine(),
));
return null;
}

$actualType = $scope->getType($node->getArgs()[1]->value);
Expand Down
9 changes: 9 additions & 0 deletions tests/Tester/assert-type-narrowing.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,12 @@ function testTypeInterface(mixed $val): void
Assert::type(Countable::class, $val);
assertType('Countable', $val);
}


function testFailTerminates(?int $val): void
{
if ($val === null) {
Assert::fail('must not be null');
}
assertType('int', $val);
}