Skip to content

Commit 8229b5c

Browse files
authored
Improve readability of ArgumentsHandler, FuncCallHandler (#6951)
1 parent 4be202f commit 8229b5c

4 files changed

Lines changed: 24 additions & 49 deletions

File tree

‎src/Analyser/ArgumentsHandler.php‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
use PHPStan\Node\Expr\NativeTypeExpr;
2929
use PHPStan\Node\InvalidateExprNode;
3030
use PHPStan\Reflection\Callables\SimpleImpurePoint;
31-
use PHPStan\Reflection\Callables\SimpleThrowPoint;
3231
use PHPStan\Reflection\ExtendedMethodReflection;
3332
use PHPStan\Reflection\ExtendedParameterReflection;
3433
use PHPStan\Reflection\FunctionReflection;
@@ -61,7 +60,6 @@
6160
use PHPStan\Type\Type;
6261
use PHPStan\Type\TypeCombinator;
6362
use PHPStan\Type\TypeUtils;
64-
use function array_filter;
6563
use function array_keys;
6664
use function array_last;
6765
use function array_map;
@@ -654,11 +652,17 @@ public function processArgs(
654652
$deferredInvalidateExpressions[] = [$acceptors[0]->getInvalidateExpressions(), $acceptors[0]->getUsedVariables()];
655653
}
656654
if ($callCallbackImmediately) {
657-
$callableThrowPoints = array_map(static fn (SimpleThrowPoint $throwPoint) => $throwPoint->isExplicit() ? InternalThrowPoint::createExplicit($scope, $throwPoint->getType(), $arg->value, $throwPoint->canContainAnyThrowable()) : InternalThrowPoint::createImplicit($scope, $arg->value), $acceptors[0]->getThrowPoints());
658-
if (!$this->implicitThrows) {
659-
$callableThrowPoints = array_values(array_filter($callableThrowPoints, static fn (InternalThrowPoint $throwPoint) => $throwPoint->isExplicit()));
655+
foreach ($acceptors[0]->getThrowPoints() as $throwPoint) {
656+
$isExplicit = $throwPoint->isExplicit();
657+
if (!$this->implicitThrows && !$isExplicit) {
658+
continue;
659+
}
660+
if ($isExplicit) {
661+
$throwPoints[] = InternalThrowPoint::createExplicit($scope, $throwPoint->getType(), $arg->value, $throwPoint->canContainAnyThrowable());
662+
} else {
663+
$throwPoints[] = InternalThrowPoint::createImplicit($scope, $arg->value);
664+
}
660665
}
661-
$throwPoints = array_merge($throwPoints, $callableThrowPoints);
662666
$impurePoints = array_merge($impurePoints, array_map(static fn (SimpleImpurePoint $impurePoint) => new ImpurePoint($scope, $arg->value, $impurePoint->getIdentifier(), $impurePoint->getDescription(), $impurePoint->isCertain()), $acceptors[0]->getImpurePoints()));
663667
}
664668
}

‎src/Analyser/ExprHandler/FuncCallHandler.php‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
use PHPStan\Parser\ImmediatelyInvokedClosureVisitor;
5353
use PHPStan\Reflection\Callables\CallableParametersAcceptor;
5454
use PHPStan\Reflection\Callables\SimpleImpurePoint;
55-
use PHPStan\Reflection\Callables\SimpleThrowPoint;
5655
use PHPStan\Reflection\ExtendedParametersAcceptor;
5756
use PHPStan\Reflection\FunctionReflection;
5857
use PHPStan\Reflection\Native\NativeParameterReflection;
@@ -85,12 +84,10 @@
8584
use PHPStan\Type\UnionType;
8685
use Throwable;
8786
use WeakReference;
88-
use function array_filter;
8987
use function array_key_first;
9088
use function array_keys;
9189
use function array_map;
9290
use function array_merge;
93-
use function array_values;
9491
use function count;
9592
use function in_array;
9693
use function is_string;
@@ -194,11 +191,17 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
194191
) {
195192
// processed later
196193
} elseif ($parametersAcceptor instanceof CallableParametersAcceptor) {
197-
$callableThrowPoints = array_map(static fn (SimpleThrowPoint $throwPoint) => $throwPoint->isExplicit() ? InternalThrowPoint::createExplicit($scope, $throwPoint->getType(), $expr, $throwPoint->canContainAnyThrowable()) : InternalThrowPoint::createImplicit($scope, $expr), $parametersAcceptor->getThrowPoints());
198-
if (!$this->implicitThrows) {
199-
$callableThrowPoints = array_values(array_filter($callableThrowPoints, static fn (InternalThrowPoint $throwPoint) => $throwPoint->isExplicit()));
194+
foreach ($parametersAcceptor->getThrowPoints() as $throwPoint) {
195+
$isExplicit = $throwPoint->isExplicit();
196+
if (!$this->implicitThrows && !$isExplicit) {
197+
continue;
198+
}
199+
if ($isExplicit) {
200+
$throwPoints[] = InternalThrowPoint::createExplicit($scope, $throwPoint->getType(), $expr, $throwPoint->canContainAnyThrowable());
201+
} else {
202+
$throwPoints[] = InternalThrowPoint::createImplicit($scope, $expr);
203+
}
200204
}
201-
$throwPoints = array_merge($throwPoints, $callableThrowPoints);
202205
// A callable value's impure points are resolved from its
203206
// ParametersAcceptor alone, so a conditionally pure function reached
204207
// through a first-class callable arrives here as an unconditional

‎turbo-ext/src/ArgumentsHandler.cpp‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,11 +2148,11 @@ class ArgumentsHandler
21482148
zv::Val simpleThrowPoints = callOn(throwPointsSite, acceptor, PT_LC("getthrowpoints"), "getThrowPoints", 0, NULL);
21492149
if (UNEXPECTED(simpleThrowPoints.isUndef() || !requireArray(simpleThrowPoints.raw(), "array_map(): Argument #2 ($array)"))) return false;
21502150
zval *scope = w.scope.raw();
2151-
zv::Arr callableThrowPoints = zv::Arr::create(zend_hash_num_elements(Z_ARRVAL_P(simpleThrowPoints.raw())));
21522151
for (zv::ArrayEntry entry : zv::ArrRef(simpleThrowPoints.raw())) {
21532152
zval *throwPoint = entry.value().deref().raw();
21542153
zv::Val isExplicit = callOn(pt_ah_simple_throw_point_is_explicit_site, throwPoint, PT_LC("isexplicit"), "isExplicit", 0, NULL);
21552154
if (UNEXPECTED(isExplicit.isUndef())) return false;
2155+
if (!zend_is_true(slot(slots::implicitThrows)) && !zend_is_true(isExplicit.raw())) continue;
21562156
zv::Val mapped;
21572157
if (zend_is_true(isExplicit.raw())) {
21582158
zv::Val type = callOn(pt_ah_simple_throw_point_get_type_site, throwPoint, PT_LC("gettype"), "getType", 0, NULL);
@@ -2164,20 +2164,8 @@ class ArgumentsHandler
21642164
mapped = pt_internal_throw_point_create_implicit(scope, value);
21652165
}
21662166
if (UNEXPECTED(mapped.isUndef())) return false;
2167-
setKey(callableThrowPoints, entry.stringKeyOrNull(), entry.indexKey(), mapped.raw());
2168-
}
2169-
if (!zend_is_true(slot(slots::implicitThrows))) {
2170-
zv::Arr explicitOnly = zv::Arr::create(zend_hash_num_elements(callableThrowPoints.table()));
2171-
for (zv::ArrayEntry entry : zv::TableRef(callableThrowPoints.table())) {
2172-
bool isExplicit = false;
2173-
if (UNEXPECTED(!pt_internal_throw_point_is_explicit(entry.value().raw(), isExplicit))) return false;
2174-
if (isExplicit) {
2175-
explicitOnly.push(zv::Ref(entry.value().raw()));
2176-
}
2177-
}
2178-
callableThrowPoints = std::move(explicitOnly);
2167+
w.throwPoints.push(std::move(mapped));
21792168
}
2180-
if (UNEXPECTED(!arrayMerge(w.throwPoints, callableThrowPoints.raw()))) return false;
21812169

21822170
zv::Val simpleImpurePoints = callOn(impurePointsSite, acceptor, PT_LC("getimpurepoints"), "getImpurePoints", 0, NULL);
21832171
if (UNEXPECTED(simpleImpurePoints.isUndef() || !requireArray(simpleImpurePoints.raw(), "array_map(): Argument #2 ($array)"))) return false;

‎turbo-ext/src/FuncCallHandler.cpp‎

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,12 +1469,11 @@ class FuncCallHandler
14691469
zend_type_error("array_map(): Argument #2 ($array) must be of type array, %s given", zend_zval_value_name(simpleThrowPoints.raw()));
14701470
return false;
14711471
}
1472-
/* array_map() keeps the keys of its one array */
1473-
zv::Arr callableThrowPoints = zv::Arr::create(zend_hash_num_elements(Z_ARRVAL_P(simpleThrowPoints.raw())));
14741472
for (zv::ArrayEntry entry : zv::ArrRef(simpleThrowPoints.raw())) {
14751473
zval *simpleThrowPoint = entry.value().deref().raw();
14761474
zv::Val explicitValue = callNoArgs(pt_fch_throw_point_explicit_site, simpleThrowPoint, PT_LC("isexplicit"), "isExplicit");
14771475
if (UNEXPECTED(explicitValue.isUndef())) return false;
1476+
if (!flag(slots::implicitThrows) && !zend_is_true(explicitValue.raw())) continue;
14781477
zv::Val throwPoint;
14791478
if (zend_is_true(explicitValue.raw())) {
14801479
zv::Val type = callNoArgs(pt_fch_throw_point_type_site, simpleThrowPoint, PT_LC("gettype"), "getType");
@@ -1486,27 +1485,8 @@ class FuncCallHandler
14861485
throwPoint = pt_internal_throw_point_create_implicit(scope.raw(), expr);
14871486
}
14881487
if (UNEXPECTED(throwPoint.isUndef())) return false;
1489-
zend_string *key = entry.stringKeyOrNull();
1490-
if (key != NULL) {
1491-
callableThrowPoints.set(key, std::move(throwPoint));
1492-
} else {
1493-
callableThrowPoints.separate();
1494-
zval value = throwPoint.take();
1495-
zend_hash_index_update(callableThrowPoints.table(), entry.indexKey(), &value);
1496-
}
1497-
}
1498-
zv::Val callableThrowPointsHold = zv::Val(std::move(callableThrowPoints));
1499-
if (!flag(slots::implicitThrows)) {
1500-
/* array_values(array_filter($callableThrowPoints, static fn (InternalThrowPoint $throwPoint) => $throwPoint->isExplicit())) */
1501-
zv::Arr explicitOnly = zv::Arr::create(countOf(callableThrowPointsHold.raw()));
1502-
for (zv::ArrayEntry entry : zv::ArrRef(callableThrowPointsHold.raw())) {
1503-
bool isExplicit;
1504-
if (UNEXPECTED(!pt_internal_throw_point_is_explicit(entry.value().raw(), isExplicit))) return false;
1505-
if (isExplicit) explicitOnly.push(entry.value());
1506-
}
1507-
callableThrowPointsHold = zv::Val(std::move(explicitOnly));
1488+
zv::ArrRef(throwPoints.raw()).push(zv::Ref(throwPoint.raw()));
15081489
}
1509-
if (UNEXPECTED(!arrayMerge(throwPoints, callableThrowPointsHold.raw()))) return false;
15101490

15111491
zv::Val acceptorImpurePoints = pt_parameters_acceptor_call(parametersAcceptor, PT_PA_GET_IMPURE_POINTS);
15121492
if (UNEXPECTED(acceptorImpurePoints.isUndef())) return false;

0 commit comments

Comments
 (0)