Skip to content

Commit c094914

Browse files
eyupcanakmanondrejmirtes
authored andcommitted
Fix phpstan/phpstan#14467: dynamic throw extensions on union types
1 parent 091c5a7 commit c094914

7 files changed

Lines changed: 149 additions & 0 deletions

File tree

‎src/Analyser/ExprHandler/Helper/MethodThrowPointHelper.php‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@
1616
use PHPStan\Reflection\MethodReflection;
1717
use PHPStan\Reflection\ParametersAcceptor;
1818
use PHPStan\Reflection\ParametersAcceptorSelector;
19+
use PHPStan\Reflection\Type\UnionTypeMethodReflection;
1920
use PHPStan\ShouldNotHappenException;
2021
use PHPStan\Turbo\ShadowedByTurboExtension;
2122
use PHPStan\Type\DynamicMethodThrowTypeExtension;
2223
use PHPStan\Type\DynamicStaticMethodThrowTypeExtension;
2324
use PHPStan\Type\NeverType;
2425
use PHPStan\Type\ObjectType;
2526
use PHPStan\Type\Type;
27+
use PHPStan\Type\TypeCombinator;
2628
use ReflectionFunction;
2729
use ReflectionMethod;
2830
use Throwable;
31+
use function count;
2932
use function in_array;
3033

3134
#[AutowiredService]
@@ -63,6 +66,42 @@ public function getThrowPoint(
6366
Type $methodCallReturnType,
6467
): ?InternalThrowPoint
6568
{
69+
if ($methodReflection instanceof UnionTypeMethodReflection) {
70+
$throwPoints = [];
71+
foreach ($methodReflection->getMethods() as $innerMethodReflection) {
72+
$throwPoint = $this->getThrowPoint($innerMethodReflection, $parametersAcceptor, $normalizedMethodCall, $scope, $context, $methodCallReturnType);
73+
if ($throwPoint === null) {
74+
continue;
75+
}
76+
77+
$throwPoints[] = $throwPoint;
78+
}
79+
80+
if (count($throwPoints) === 0) {
81+
return null;
82+
}
83+
84+
$throwTypes = [];
85+
$canContainAnyThrowable = false;
86+
$isExplicit = false;
87+
foreach ($throwPoints as $throwPoint) {
88+
$throwTypes[] = $throwPoint->getType();
89+
if ($throwPoint->canContainAnyThrowable()) {
90+
$canContainAnyThrowable = true;
91+
}
92+
if (!$throwPoint->isExplicit()) {
93+
continue;
94+
}
95+
$isExplicit = true;
96+
}
97+
98+
if (!$isExplicit) {
99+
return InternalThrowPoint::createImplicit($scope, $normalizedMethodCall);
100+
}
101+
102+
return InternalThrowPoint::createExplicit($scope, TypeCombinator::union(...$throwTypes), $normalizedMethodCall, $canContainAnyThrowable);
103+
}
104+
66105
if ($normalizedMethodCall instanceof MethodCall) {
67106
foreach ($this->dynamicMethodThrowTypeExtensions->getAll() as $extension) {
68107
if (!$extension->isMethodSupported($methodReflection)) {

‎src/Reflection/Type/UnionTypeMethodReflection.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\Reflection\MethodReflection;
1212
use PHPStan\Reflection\ParametersAcceptorSelector;
1313
use PHPStan\TrinaryLogic;
14+
use PHPStan\Turbo\ReferencedByTurboExtension;
1415
use PHPStan\Type\Type;
1516
use PHPStan\Type\TypeCombinator;
1617
use function array_map;
@@ -19,6 +20,7 @@
1920
use function implode;
2021
use function is_bool;
2122

23+
#[ReferencedByTurboExtension(key: 'unionTypeMethodReflection')]
2224
final class UnionTypeMethodReflection implements ExtendedMethodReflection
2325
{
2426

@@ -34,6 +36,14 @@ public function getDeclaringClass(): ClassReflection
3436
return $this->methods[0]->getDeclaringClass();
3537
}
3638

39+
/**
40+
* @return ExtendedMethodReflection[]
41+
*/
42+
public function getMethods(): array
43+
{
44+
return $this->methods;
45+
}
46+
3747
public function isStatic(): bool
3848
{
3949
foreach ($this->methods as $method) {

‎tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,12 @@ public function testBug14479(): void
783783
$this->analyse([__DIR__ . '/data/bug-14479.php'], []);
784784
}
785785

786+
#[RequiresPhp('>= 8.3.0')]
787+
public function testBug14467(): void
788+
{
789+
$this->analyse([__DIR__ . '/data/bug-14467.php'], []);
790+
}
791+
786792
public function testBug5952(): void
787793
{
788794
$this->analyse([__DIR__ . '/data/bug-5952.php'], [
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php // lint >= 8.3
2+
3+
// FQN must sort after \DateTime so methods[0] resolves to DateTime.
4+
namespace TestBug14467;
5+
6+
class Foo
7+
{
8+
9+
/** @throws \DateMalformedStringException */
10+
public function modify(string $s): self
11+
{
12+
return $this;
13+
}
14+
15+
}
16+
17+
class Bar
18+
{
19+
20+
/** @param \DateTime|Foo $obj */
21+
public function caseDateTimeFirst($obj): void
22+
{
23+
try {
24+
$obj->modify('+1 day');
25+
} catch (\DateMalformedStringException $e) {
26+
}
27+
}
28+
29+
/** @param Foo|\DateTime $obj */
30+
public function caseFooFirst($obj): void
31+
{
32+
try {
33+
$obj->modify('+1 day');
34+
} catch (\DateMalformedStringException $e) {
35+
}
36+
}
37+
38+
}

‎turbo-ext/src/MethodThrowPointHelper.cpp‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace sigs = ptdecl::MethodThrowPointHelper::sig;
2626
#include "TypeOps.h"
2727
#include "AcceptorValues.h"
2828
#include "ParserVisitors.h"
29+
#include "AnalyserValues.h"
2930

3031
static zend_class_entry *pt_ce_method_throw_point_helper;
3132

@@ -104,6 +105,12 @@ class MethodThrowPointHelper
104105
* exception */
105106
zv::Val getThrowPoint(zval *methodReflection, zval *parametersAcceptor, zval *normalizedMethodCall, zval *scope, zval *context, zval *methodCallReturnType) const
106107
{
108+
zend_class_entry *unionTypeMethodReflectionCe = pt_class(PT_CLASS_UNION_TYPE_METHOD_REFLECTION);
109+
if (UNEXPECTED(unionTypeMethodReflectionCe == NULL)) return zv::Val();
110+
if (instanceof_function(Z_OBJCE_P(methodReflection), unionTypeMethodReflectionCe)) {
111+
return getUnionThrowPoint(methodReflection, parametersAcceptor, normalizedMethodCall, scope, context, methodCallReturnType);
112+
}
113+
107114
zend_class_entry *methodCallCe = pt_class(PT_CLASS_METHOD_CALL);
108115
if (UNEXPECTED(methodCallCe == NULL)) return zv::Val();
109116
bool isMethodCall = instanceof_function(Z_OBJCE_P(normalizedMethodCall), methodCallCe);
@@ -258,6 +265,52 @@ class MethodThrowPointHelper
258265
private:
259266
zend_object *self;
260267

268+
/* Mirrors getThrowPoint()'s UnionTypeMethodReflection arm: the inner
269+
* methods' throw points, combined */
270+
zv::Val getUnionThrowPoint(zval *methodReflection, zval *parametersAcceptor, zval *normalizedMethodCall, zval *scope, zval *context, zval *methodCallReturnType) const
271+
{
272+
zv::Val methods = pt_type_call(Z_OBJ_P(methodReflection), PT_LC("getmethods"), 0, NULL);
273+
if (UNEXPECTED(methods.isUndef())) return zv::Val();
274+
275+
zv::Arr throwTypes = zv::Arr::create(0);
276+
bool canContainAnyThrowable = false;
277+
bool isExplicit = false;
278+
if (EXPECTED(Z_TYPE_P(methods.raw()) == IS_ARRAY)) {
279+
for (zv::ArrayEntry entry : zv::ArrRef(methods.raw())) {
280+
zval *innerMethodReflection = entry.value().deref().raw();
281+
if (UNEXPECTED(Z_TYPE_P(innerMethodReflection) != IS_OBJECT)) {
282+
zend_type_error("PHPStan\\Analyser\\ExprHandler\\Helper\\MethodThrowPointHelper::getThrowPoint(): Argument #1 ($methodReflection) must be of type PHPStan\\Reflection\\MethodReflection, %s given", zend_zval_value_name(innerMethodReflection));
283+
return zv::Val();
284+
}
285+
zv::Val throwPoint = getThrowPoint(innerMethodReflection, parametersAcceptor, normalizedMethodCall, scope, context, methodCallReturnType);
286+
if (UNEXPECTED(throwPoint.isUndef())) return zv::Val();
287+
if (Z_TYPE_P(throwPoint.raw()) == IS_NULL) continue;
288+
289+
zv::Val typeHold;
290+
zval *type = pt_internal_throw_point_type(throwPoint.raw(), typeHold);
291+
if (UNEXPECTED(type == NULL)) return zv::Val();
292+
throwTypes.push(zv::Val::copyOf(zv::Ref(type)));
293+
bool innerCanContainAnyThrowable;
294+
if (UNEXPECTED(!pt_internal_throw_point_can_contain_any_throwable(throwPoint.raw(), innerCanContainAnyThrowable))) return zv::Val();
295+
if (innerCanContainAnyThrowable) canContainAnyThrowable = true;
296+
bool innerIsExplicit;
297+
if (UNEXPECTED(!pt_internal_throw_point_is_explicit(throwPoint.raw(), innerIsExplicit))) return zv::Val();
298+
if (innerIsExplicit) isExplicit = true;
299+
}
300+
}
301+
302+
HashTable *table = throwTypes.table();
303+
uint32_t count = zend_hash_num_elements(table);
304+
if (count == 0) return zv::Val::null();
305+
if (!isExplicit) return createImplicit(scope, normalizedMethodCall);
306+
307+
zv::Val throwType = EXPECTED(HT_IS_PACKED(table) && table->nNumUsed == count)
308+
? pt_type_combinator_union(count, table->arPacked)
309+
: pt_type_combinator_call_spread(PT_LC("union"), table);
310+
if (UNEXPECTED(throwType.isUndef())) return zv::Val();
311+
return createExplicit(scope, throwType.raw(), normalizedMethodCall, canContainAnyThrowable);
312+
}
313+
261314
zval *slot(uint32_t index) const { return OBJ_PROP_NUM(self, index); }
262315

263316
/* the engine's Error for reading a never-written typed property */

‎turbo-ext/src/support.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ static const pt_class_template pt_class_templates[PT_CLASS_COUNT] = {
415415
/* PT_CLASS_NATIVE_PHP_TYPE_NODE_HELPER */ {"nativePhpTypeNodeHelper", "PHPStan\\Type\\NativePhpTypeNodeHelper"},
416416
/* PT_CLASS_FULLY_QUALIFIED_NAME_NODE */ {"fullyQualifiedNameNode", "PHPStan\\PhpDocParser\\Ast\\Type\\FullyQualifiedNameNode"},
417417
/* PT_CLASS_TYPE_NODE_DESCRIBER */ {"typeNodeDescriber", "PHPStan\\Type\\TypeNodeDescriber"},
418+
/* PT_CLASS_UNION_TYPE_METHOD_REFLECTION */ {"unionTypeMethodReflection", "PHPStan\\Reflection\\Type\\UnionTypeMethodReflection"},
418419
};
419420

420421
zend_class_entry *pt_class(int idx)

‎turbo-ext/src/support.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,8 @@ enum {
519519
/* the class names of Type::toPhpDocNode() and how describe() prints them */
520520
PT_CLASS_FULLY_QUALIFIED_NAME_NODE,
521521
PT_CLASS_TYPE_NODE_DESCRIBER,
522+
/* MethodThrowPointHelper's union arm */
523+
PT_CLASS_UNION_TYPE_METHOD_REFLECTION,
522524
PT_CLASS_COUNT
523525
};
524526

0 commit comments

Comments
 (0)