Skip to content

Commit 8712fc8

Browse files
Rework
1 parent dc2324f commit 8712fc8

4 files changed

Lines changed: 15 additions & 25 deletions

File tree

src/Analyser/ExprHandler/NewHandler.php

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,9 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
8181
{
8282
$parametersAcceptor = null;
8383
$constructorReflection = null;
84-
$className = null;
8584
$classReflection = null;
8685
$hasYield = false;
8786
$throwPoints = [];
88-
$deferConstructorThrowPoints = false;
8987
$impurePoints = [];
9088
$isAlwaysTerminating = false;
9189
$normalizedExpr = $expr;
@@ -94,7 +92,6 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
9492

9593
[$constructorReflection, $classReflection, $parametersAcceptor, $constructorImpurePoints] = $this->processConstructorReflection($className, $expr, $scope);
9694
$impurePoints = array_merge($impurePoints, $constructorImpurePoints);
97-
$deferConstructorThrowPoints = true;
9895

9996
if ($parametersAcceptor !== null) {
10097
$normalizedExpr = ArgumentsNormalizer::reorderNewArguments($parametersAcceptor, $expr) ?? $expr;
@@ -139,19 +136,13 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
139136
}
140137
} else {
141138
$nodeScopeResolver->processStmtNode($expr->class, $scope, $storage, $nodeCallback, StatementContext::createTopLevel());
142-
$declaringClass = $constructorReflection->getDeclaringClass();
143-
$constructorThrowPoint = $this->getConstructorThrowPoint($constructorReflection, $parametersAcceptor, $classReflection, $expr, new Name\FullyQualified($declaringClass->getName()), $expr->getArgs(), $scope);
144-
if ($constructorThrowPoint !== null) {
145-
$throwPoints[] = $constructorThrowPoint;
146-
}
147-
148139
if (!$constructorReflection->hasSideEffects()->no()) {
149140
$certain = $constructorReflection->isPure()->no();
150141
$impurePoints[] = new ImpurePoint(
151142
$scope,
152143
$expr,
153144
'new',
154-
sprintf('instantiation of class %s', $declaringClass->getDisplayName()),
145+
sprintf('instantiation of class %s', $constructorReflection->getDeclaringClass()->getDisplayName()),
155146
$certain,
156147
);
157148
}
@@ -181,9 +172,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
181172
if ($className !== null) {
182173
[$constructorReflection, $classReflection, $parametersAcceptor, $constructorImpurePoints] = $this->processConstructorReflection($className, $expr, $scope);
183174
$impurePoints = array_merge($impurePoints, $constructorImpurePoints);
184-
$deferConstructorThrowPoints = true;
185175
} else {
186-
$throwPoints[] = InternalThrowPoint::createImplicit($scope, $expr);
187176
$impurePoints[] = new ImpurePoint(
188177
$scope,
189178
$expr,
@@ -205,15 +194,14 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
205194
$impurePoints = array_merge($impurePoints, $argsResult->getImpurePoints());
206195
$isAlwaysTerminating = $isAlwaysTerminating || $argsResult->isAlwaysTerminating();
207196

208-
if ($deferConstructorThrowPoints && $className !== null) {
209-
if ($constructorReflection !== null && $parametersAcceptor !== null && $classReflection !== null) {
210-
$constructorThrowPoint = $this->getConstructorThrowPoint($constructorReflection, $parametersAcceptor, $classReflection, $expr, new Name\FullyQualified($className), $expr->getArgs(), $scope);
211-
if ($constructorThrowPoint !== null) {
212-
$throwPoints[] = $constructorThrowPoint;
213-
}
214-
} elseif ($classReflection === null) {
215-
$throwPoints[] = InternalThrowPoint::createImplicit($scope, $expr);
197+
if ($constructorReflection !== null && $parametersAcceptor !== null) {
198+
$className ??= $constructorReflection->getDeclaringClass()->getName();
199+
$constructorThrowPoint = $this->getConstructorThrowPoint($constructorReflection, $parametersAcceptor, $expr, new Name\FullyQualified($className), $expr->getArgs(), $scope);
200+
if ($constructorThrowPoint !== null) {
201+
$throwPoints[] = $constructorThrowPoint;
216202
}
203+
} elseif ($classReflection === null) {
204+
$throwPoints[] = InternalThrowPoint::createImplicit($scope, $expr);
217205
}
218206

219207
return new ExpressionResult(
@@ -275,7 +263,7 @@ private function processConstructorReflection(string $className, New_ $expr, Mut
275263
/**
276264
* @param list<Node\Arg> $args
277265
*/
278-
private function getConstructorThrowPoint(MethodReflection $constructorReflection, ParametersAcceptor $parametersAcceptor, ClassReflection $classReflection, New_ $new, Name $className, array $args, MutatingScope $scope): ?InternalThrowPoint
266+
private function getConstructorThrowPoint(MethodReflection $constructorReflection, ParametersAcceptor $parametersAcceptor, New_ $new, Name $className, array $args, MutatingScope $scope): ?InternalThrowPoint
279267
{
280268
$methodCall = new StaticCall($className, $constructorReflection->getName(), $args);
281269
$normalizedMethodCall = ArgumentsNormalizer::reorderStaticCallArguments($parametersAcceptor, $methodCall);
@@ -300,7 +288,7 @@ private function getConstructorThrowPoint(MethodReflection $constructorReflectio
300288
return InternalThrowPoint::createExplicit($scope, $throwType, $new, true);
301289
}
302290
} elseif ($this->implicitThrows) {
303-
if (!$classReflection->is(Throwable::class)) {
291+
if (!$constructorReflection->getDeclaringClass()->is(Throwable::class)) {
304292
return InternalThrowPoint::createImplicit($scope, $methodCall);
305293
}
306294
}

src/Analyser/InternalScopeFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface InternalScopeFactory
1515
* @param array<string, ExpressionTypeHolder> $expressionTypes
1616
* @param array<string, ExpressionTypeHolder> $nativeExpressionTypes
1717
* @param array<string, ConditionalExpressionHolder[]> $conditionalExpressions
18-
* @param list<string> $inClosureBindScopeClasses
18+
* @param list<non-empty-string> $inClosureBindScopeClasses
1919
* @param array<string, true> $currentlyAssignedExpressions
2020
* @param array<string, true> $currentlyAllowedUndefinedExpressions
2121
* @param list<array{FunctionReflection|MethodReflection|null, ParameterReflection|null}> $inFunctionCallsStack

src/Analyser/MutatingScope.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class MutatingScope implements Scope, NodeCallbackInvoker
162162
* @param callable(Node $node, Scope $scope): void|null $nodeCallback
163163
* @param array<string, ExpressionTypeHolder> $expressionTypes
164164
* @param array<string, ConditionalExpressionHolder[]> $conditionalExpressions
165-
* @param list<string> $inClosureBindScopeClasses
165+
* @param list<non-empty-string> $inClosureBindScopeClasses
166166
* @param array<string, true> $currentlyAssignedExpressions
167167
* @param array<string, true> $currentlyAllowedUndefinedExpressions
168168
* @param array<string, ExpressionTypeHolder> $nativeExpressionTypes
@@ -1820,7 +1820,7 @@ public function enterNamespace(string $namespaceName): self
18201820
}
18211821

18221822
/**
1823-
* @param list<string> $scopeClasses
1823+
* @param list<non-empty-string> $scopeClasses
18241824
*/
18251825
public function enterClosureBind(?Type $thisType, ?Type $nativeThisType, array $scopeClasses): self
18261826
{

src/Analyser/Scope.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ public function getScopeNativeType(Expr $expr): Type;
173173
* (they should already be fully qualified by the PHP parser's name resolver).
174174
*
175175
* Inside a Closure::bind() context, `self`/`static` resolve to the bound class.
176+
*
177+
* @return non-empty-string
176178
*/
177179
public function resolveName(Name $name): string;
178180

0 commit comments

Comments
 (0)