Skip to content
Merged
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
4 changes: 4 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,7 @@ When methods are called on union types (`Foo|Bar`), the resolved method reflecti
- `react/child-process`, `react/async` - Parallel analysis
- `symfony/console` - CLI interface
- `hoa/compiler` - Used for regex type parsing

### Ternary expression type narrowing in TypeSpecifier

`TypeSpecifier::specifyTypesInCondition()` handles ternary expressions (`$cond ? $a : $b`) for type narrowing. In a truthy context (e.g., inside `assert()`), the ternary is semantically equivalent to `($cond && $a) || (!$cond && $b)` — meaning if the condition is true, the "if" branch must be truthy, and if false, the "else" branch must be truthy. The fix converts ternary expressions to this `BooleanOr(BooleanAnd(...), BooleanAnd(...))` form so the existing OR/AND narrowing logic handles both branches correctly. This enables `assert($cond ? $x instanceof A : $x instanceof B)` to narrow `$x` to `A|B`. The `AssertFunctionTypeSpecifyingExtension` calls `specifyTypesInCondition` with `TypeSpecifierContext::createTruthy()` context for the assert argument.
10 changes: 5 additions & 5 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -1062,12 +1062,12 @@ public function specifyTypesInCondition(
} elseif (
$expr instanceof Expr\Ternary
&& !$context->null()
&& $scope->getType($expr->else)->isFalse()->yes()
) {
$conditionExpr = $expr->cond;
if ($expr->if !== null) {
$conditionExpr = new BooleanAnd($conditionExpr, $expr->if);
}
$ifExpr = $expr->if ?? $expr->cond;
$conditionExpr = new BooleanOr(
new BooleanAnd($expr->cond, $ifExpr),
new BooleanAnd(new Expr\BooleanNot($expr->cond), $expr->else),
);

return $this->specifyTypesInCondition($scope, $conditionExpr, $context)->setRootExpr($expr);

Expand Down
55 changes: 55 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14100.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php declare(strict_types = 1);

namespace Bug14100;

use function PHPStan\Testing\assertType;

interface Foo {}
interface Bar {}
interface Baz {}

class HelloWorld
{
/** @param resource|object $conn */
public function ternaryAssertInstanceof($conn): void
{
assert(rand(0, 1) ? $conn instanceof Foo : $conn instanceof Bar);
assertType('Bug14100\Bar|Bug14100\Foo', $conn);
}

/** @param resource|object $conn */
public function ifElseAssertInstanceof($conn): void
{
if (rand(0, 1)) {
assert($conn instanceof Foo);
} else {
assert($conn instanceof Bar);
}
assertType('Bug14100\Bar|Bug14100\Foo', $conn);
}

/** @param resource|object $conn */
public function ternaryAssertThreeBranches($conn): void
{
assert(rand(0, 2) === 0 ? $conn instanceof Foo : (rand(0, 2) === 1 ? $conn instanceof Bar : $conn instanceof Baz));
assertType('Bug14100\Bar|Bug14100\Baz|Bug14100\Foo', $conn);
}

/** @param mixed $val */
public function ternaryAssertScalar($val): void
{
assert(rand(0, 1) ? is_string($val) : is_int($val));
assertType('int|string', $val);
}

/**
* Short ternary syntax (Elvis operator): $a ?: $b
*
* @param resource|object $conn
*/
public function shortTernaryAssert($conn): void
{
assert($conn instanceof Foo ?: $conn instanceof Bar);
assertType('Bug14100\Bar|Bug14100\Foo', $conn);
}
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Classes/InstantiationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ public function testBug10248(): void
$this->analyse([__DIR__ . '/data/bug-10248.php'], []);
}

#[RequiresPhp('>= 8.0')]
#[RequiresPhp('>= 8.2')]
public function testBug11815(): void
{
$this->analyse([__DIR__ . '/data/bug-11815.php'], []);
Expand Down
Loading