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
3 changes: 3 additions & 0 deletions src/Type/Generic/TemplateTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,9 @@ public function tryRemove(Type $typeToRemove): ?Type
if ($this->getBound() === $bound || $this->getBound()->equals($bound)) {
return null;
}
if ($bound instanceof NeverType) {
return $bound;
}

return TemplateTypeFactory::create(
$this->getScope(),
Expand Down
3 changes: 3 additions & 0 deletions src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2094,6 +2094,9 @@ public function tryRemove(Type $typeToRemove): ?Type
// time, without rebuilding the subtracted union once per member.
$membersToRemove = [];
foreach ($typeToRemove->getTypes() as $member) {
if ($member->isSuperTypeOf($this)->yes()) {
return new NeverType();
}
$isSuperTypeOfMember = $this->isSuperTypeOf($member);
if ($isSuperTypeOfMember->yes()) {
$membersToRemove[] = $member;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,11 @@ public function testBug13515(): void
$this->analyse([__DIR__ . '/data/bug-13515.php'], []);
}

public function testBug15392(): void
{
$this->missingCheckedExceptionInThrows = false;
$this->checkedExceptionClasses = [];
$this->analyse([__DIR__ . '/data/bug-15392.php'], []);
}

}
33 changes: 33 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/bug-15392.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types = 1);

namespace Bug15392;

/** @phpstan-sealed SubA|SubB */
abstract class BaseEx extends \RuntimeException {}
final class SubA extends BaseEx {}
final class SubB extends BaseEx {}
final class OtherEx extends \RuntimeException {}

/** @throws BaseEx */
function process(): void
{
throw new SubA();
}

/** @throws void */
function g(): void
{
try {
process();
} catch (BaseEx | OtherEx $e) {
}
}

/** @throws void */
function h(): void
{
try {
process();
} catch (OtherEx | BaseEx $e) {
}
}
41 changes: 41 additions & 0 deletions tests/PHPStan/Type/TypeCombinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6530,6 +6530,47 @@ public static function dataRemove(): array
IntersectionType::class,
'non-empty-array&hasOffsetValue(\'a\', string)',
],
[
new ObjectType('PHPStan\Fixture\TestEnum'),
new UnionType([
new ObjectType('PHPStan\Fixture\TestEnum'),
new ObjectType(stdClass::class),
]),
NeverType::class,
'*NEVER*=implicit',
],
[
new ObjectType('PHPStan\Fixture\TestEnum'),
new UnionType([
new ObjectType(stdClass::class),
new ObjectType('PHPStan\Fixture\TestEnumInterface'),
]),
NeverType::class,
'*NEVER*=implicit',
],
[
new ObjectType('PHPStan\Fixture\TestEnum'),
new UnionType([
new ObjectType(stdClass::class),
new ObjectType('PHPStan\Fixture\TestEnum'),
]),
NeverType::class,
'*NEVER*=implicit',
],
[
TemplateTypeFactory::create(
TemplateTypeScope::createWithFunction('a'),
'T',
new ObjectType('PHPStan\Fixture\TestEnum'),
TemplateTypeVariance::createInvariant(),
),
new UnionType([
new ObjectType('PHPStan\Fixture\TestEnum'),
new ObjectType(stdClass::class),
]),
NeverType::class,
'*NEVER*=implicit',
],
];
}

Expand Down
8 changes: 8 additions & 0 deletions turbo-ext/src/ObjectType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2799,6 +2799,14 @@ class ObjectType
zend_type_error("phpstan_turbo: a union member must be %s", ptcls::type);
return zv::Val();
}
/* if ($member->isSuperTypeOf($this)->yes()) return new NeverType(); */
zval selfZv;
ZVAL_OBJ(&selfZv, self);
zv::Val memberIsSuperType = pt_type_call(Z_OBJ_P(member), PT_LC("issupertypeof"), 1, &selfZv);
if (UNEXPECTED(memberIsSuperType.isUndef())) return zv::Val();
zend_long memberIsSuperTypeValue = pt_type_result_trinary(memberIsSuperType.raw());
if (UNEXPECTED(memberIsSuperTypeValue < 0)) return zv::Val();
if (memberIsSuperTypeValue == PT_TRI_YES) return pt_type_new_never_type();
zv::Val isSuperTypeOfMember = thisCall(PT_LC("issupertypeof"), otIsSuperTypeOf, 1, member, [&]() { return isSuperTypeOf(member); });
if (UNEXPECTED(isSuperTypeOfMember.isUndef())) return zv::Val();
zend_long memberValue = pt_type_result_trinary(isSuperTypeOfMember.raw());
Expand Down
2 changes: 2 additions & 0 deletions turbo-ext/src/TypeTraits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4705,6 +4705,8 @@ class TemplateTypeTrait
zv::Val equal = pt_type_op(Z_OBJ_P(boundOnceMore.raw()), PT_OP_EQUALS, 1, removed.raw());
if (UNEXPECTED(equal.isUndef())) return zv::Val();
if (zend_is_true(equal.raw())) return zv::Val::null();
/* if ($bound instanceof NeverType) return $bound; */
if (zv::Ref(removed.raw()).instanceOf(pt_ce_never_type)) return removed;
return recreateKeepingDefault(removed.raw());
}

Expand Down
Loading