Skip to content

Commit ecf6048

Browse files
committed
Merge remote-tracking branch 'origin/2.2.x' into 2.3.x
# Conflicts: # src/Analyser/NodeScopeResolver.php # src/Analyser/StmtHandler/ExpressionHandler.php
2 parents 8e7f80c + 6a09253 commit ecf6048

13 files changed

Lines changed: 98 additions & 99 deletions

‎src/Analyser/ArgumentsHandler.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ public function processArgs(
431431

432432
$closureResult = $this->closureProcessor->processClosureNode($nodeScopeResolver, $stmt, $arg->value, $scopeToPass, $storage, $nodeCallback, $context, $parameterType, $parameterNativeType);
433433
if ($this->callCallbackImmediately($parameter, $parameterType, $calleeReflection)) {
434-
$throwPoints = array_merge($throwPoints, array_map(static fn (InternalThrowPoint $throwPoint) => $throwPoint->isExplicit() ? InternalThrowPoint::createExplicit($scope, $throwPoint->getType(), $arg->value, $throwPoint->canContainAnyThrowable(), $throwPoint->isFromThrowExpr()) : InternalThrowPoint::createImplicit($scope, $arg->value), $closureResult->getThrowPoints()));
434+
$throwPoints = array_merge($throwPoints, array_map(static fn (InternalThrowPoint $throwPoint) => $throwPoint->isExplicit() ? InternalThrowPoint::createExplicit($scope, $throwPoint->getType(), $arg->value, $throwPoint->canContainAnyThrowable()) : InternalThrowPoint::createImplicit($scope, $arg->value), $closureResult->getThrowPoints()));
435435
$impurePoints = array_merge($impurePoints, $closureResult->getImpurePoints());
436436
}
437437

@@ -578,7 +578,7 @@ public function processArgs(
578578
$arrowFunctionResult = $this->closureProcessor->processArrowFunctionNode($nodeScopeResolver, $stmt, $arg->value, $scopeToPass, $storage, $nodeCallback, $parameterType, $parameterNativeType, $context);
579579
$arrowFunctionExprResult = $arrowFunctionResult->getExpressionResult();
580580
if ($this->callCallbackImmediately($parameter, $parameterType, $calleeReflection)) {
581-
$throwPoints = array_merge($throwPoints, array_map(static fn (InternalThrowPoint $throwPoint) => $throwPoint->isExplicit() ? InternalThrowPoint::createExplicit($scope, $throwPoint->getType(), $arg->value, $throwPoint->canContainAnyThrowable(), $throwPoint->isFromThrowExpr()) : InternalThrowPoint::createImplicit($scope, $arg->value), $arrowFunctionExprResult->getThrowPoints()));
581+
$throwPoints = array_merge($throwPoints, array_map(static fn (InternalThrowPoint $throwPoint) => $throwPoint->isExplicit() ? InternalThrowPoint::createExplicit($scope, $throwPoint->getType(), $arg->value, $throwPoint->canContainAnyThrowable()) : InternalThrowPoint::createImplicit($scope, $arg->value), $arrowFunctionExprResult->getThrowPoints()));
582582
$impurePoints = array_merge($impurePoints, $arrowFunctionExprResult->getImpurePoints());
583583
}
584584
$arrowFunctionScope = $arrowFunctionResult->getArrowFunctionScope();
@@ -666,7 +666,7 @@ public function processArgs(
666666
$deferredInvalidateExpressions[] = [$acceptors[0]->getInvalidateExpressions(), $acceptors[0]->getUsedVariables()];
667667
}
668668
if ($callCallbackImmediately) {
669-
$callableThrowPoints = array_map(static fn (SimpleThrowPoint $throwPoint) => $throwPoint->isExplicit() ? InternalThrowPoint::createExplicit($scope, $throwPoint->getType(), $arg->value, $throwPoint->canContainAnyThrowable(), $throwPoint->isFromThrowExpr()) : InternalThrowPoint::createImplicit($scope, $arg->value), $acceptors[0]->getThrowPoints());
669+
$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());
670670
if (!$this->implicitThrows) {
671671
$callableThrowPoints = array_values(array_filter($callableThrowPoints, static fn (InternalThrowPoint $throwPoint) => $throwPoint->isExplicit()));
672672
}

‎src/Analyser/ExprHandler/FuncCallHandler.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
178178
) {
179179
// processed later
180180
} elseif ($parametersAcceptor instanceof CallableParametersAcceptor) {
181-
$callableThrowPoints = array_map(static fn (SimpleThrowPoint $throwPoint) => $throwPoint->isExplicit() ? InternalThrowPoint::createExplicit($scope, $throwPoint->getType(), $expr, $throwPoint->canContainAnyThrowable(), $throwPoint->isFromThrowExpr()) : InternalThrowPoint::createImplicit($scope, $expr), $parametersAcceptor->getThrowPoints());
181+
$callableThrowPoints = array_map(static fn (SimpleThrowPoint $throwPoint) => $throwPoint->isExplicit() ? InternalThrowPoint::createExplicit($scope, $throwPoint->getType(), $expr, $throwPoint->canContainAnyThrowable()) : InternalThrowPoint::createImplicit($scope, $expr), $parametersAcceptor->getThrowPoints());
182182
if (!$this->implicitThrows) {
183183
$callableThrowPoints = array_values(array_filter($callableThrowPoints, static fn (InternalThrowPoint $throwPoint) => $throwPoint->isExplicit()));
184184
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ private function assembleClosureType(
885885
);
886886
}
887887

888-
$throwPointsForClosureType = array_map(static fn (ThrowPoint $throwPoint) => $throwPoint->isExplicit() ? SimpleThrowPoint::createExplicit($throwPoint->getType(), $throwPoint->canContainAnyThrowable(), $throwPoint->isFromThrowExpr()) : SimpleThrowPoint::createImplicit(), $throwPoints);
888+
$throwPointsForClosureType = array_map(static fn (ThrowPoint $throwPoint) => $throwPoint->isExplicit() ? SimpleThrowPoint::createExplicit($throwPoint->getType(), $throwPoint->canContainAnyThrowable()) : SimpleThrowPoint::createImplicit(), $throwPoints);
889889
$impurePointsForClosureType = array_map(static fn (ImpurePoint $impurePoint) => new SimpleImpurePoint($impurePoint->getIdentifier(), $impurePoint->getDescription(), $impurePoint->isCertain()), $impurePoints);
890890

891891
$cachedTypes = $this->findCachedTypes($expr);

‎src/Analyser/ExprHandler/ThrowHandler.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
5353
variableFlow: VariableFlow::sequence($exprResult->getVariableFlow(), VariableFlow::throwing($exprResult->getType(), false)),
5454
hasYield: false,
5555
isAlwaysTerminating: true,
56-
throwPoints: array_merge($exprResult->getThrowPoints(), [InternalThrowPoint::createExplicit($scope, $exprResult->getType(), $expr, false, fromThrowExpr: true)]),
56+
throwPoints: array_merge($exprResult->getThrowPoints(), [InternalThrowPoint::createExplicit($scope, $exprResult->getType(), $expr, false)]),
5757
impurePoints: $exprResult->getImpurePoints(),
5858
typeCallback: static fn (bool $nativeTypesPromoted): Type => new NonAcceptingNeverType(),
5959
specifyTypesCallback: fn (TypeSpecifierContext $context, bool $nativeTypesPromoted) => $this->defaultNarrowingHelper->specifyDefaultTypes($expr, $context),

‎src/Analyser/InternalThrowPoint.php‎

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ private function __construct(
2222
private Node $node,
2323
private bool $explicit,
2424
private bool $canContainAnyThrowable,
25-
private bool $fromThrowExpr = false,
2625
)
2726
{
2827
}
2928

3029
public function toPublic(): ThrowPoint
3130
{
3231
if ($this->explicit) {
33-
return ThrowPoint::createExplicit($this->scope, $this->type, $this->node, $this->canContainAnyThrowable, $this->fromThrowExpr);
32+
return ThrowPoint::createExplicit($this->scope, $this->type, $this->node, $this->canContainAnyThrowable);
3433
}
3534

3635
return ThrowPoint::createImplicit($this->scope, $this->node, $this->type);
@@ -39,9 +38,9 @@ public function toPublic(): ThrowPoint
3938
/**
4039
* @param Node\Expr|Node\Stmt $node
4140
*/
42-
public static function createExplicit(MutatingScope $scope, Type $type, Node $node, bool $canContainAnyThrowable, bool $fromThrowExpr = false): self
41+
public static function createExplicit(MutatingScope $scope, Type $type, Node $node, bool $canContainAnyThrowable): self
4342
{
44-
return new self($scope, $type, $node, true, $canContainAnyThrowable, $fromThrowExpr);
43+
return new self($scope, $type, $node, true, $canContainAnyThrowable);
4544
}
4645

4746
/**
@@ -54,7 +53,7 @@ public static function createImplicit(MutatingScope $scope, Node $node, ?Type $t
5453

5554
public static function createFromPublic(ThrowPoint $throwPoint, MutatingScope $scope): self
5655
{
57-
return new self($scope, $throwPoint->getType(), $throwPoint->getNode(), $throwPoint->isExplicit(), $throwPoint->canContainAnyThrowable(), $throwPoint->isFromThrowExpr());
56+
return new self($scope, $throwPoint->getType(), $throwPoint->getNode(), $throwPoint->isExplicit(), $throwPoint->canContainAnyThrowable());
5857
}
5958

6059
public function getScope(): MutatingScope
@@ -85,19 +84,9 @@ public function canContainAnyThrowable(): bool
8584
return $this->canContainAnyThrowable;
8685
}
8786

88-
/**
89-
* Whether the throw point comes from a `throw` written in the analysed code,
90-
* as opposed to a throw inferred from what a called function or an operation
91-
* can throw.
92-
*/
93-
public function isFromThrowExpr(): bool
94-
{
95-
return $this->fromThrowExpr;
96-
}
97-
9887
public function subtractCatchType(Type $catchType): self
9988
{
100-
return new self($this->scope, TypeCombinator::remove($this->type, $catchType), $this->node, $this->explicit, $this->canContainAnyThrowable, $this->fromThrowExpr);
89+
return new self($this->scope, TypeCombinator::remove($this->type, $catchType), $this->node, $this->explicit, $this->canContainAnyThrowable);
10190
}
10291

10392
}

‎src/Analyser/StmtHandler/ExpressionHandler.php‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace PHPStan\Analyser\StmtHandler;
44

5-
use Error;
65
use PhpParser\Node;
76
use PhpParser\Node\Expr;
87
use PhpParser\Node\Stmt;
@@ -11,7 +10,6 @@
1110
use PHPStan\Analyser\ExpressionResultStorage;
1211
use PHPStan\Analyser\InternalStatementExitPoint;
1312
use PHPStan\Analyser\InternalStatementResult;
14-
use PHPStan\Analyser\InternalThrowPoint;
1513
use PHPStan\Analyser\MutatingScope;
1614
use PHPStan\Analyser\NodeScopeResolver;
1715
use PHPStan\Analyser\Scope;
@@ -26,7 +24,6 @@
2624
use PHPStan\Turbo\ShadowedByTurboExtension;
2725
use PHPStan\Type\Constant\ConstantBooleanType;
2826
use PHPStan\Type\NeverType;
29-
use PHPStan\Type\ObjectType;
3027
use function array_filter;
3128
use function count;
3229

@@ -88,12 +85,7 @@ public function processStmt(
8885
}
8986

9087
$nodeScopeResolver->callNodeCallback($nodeCallback, $stmt, $stmtScope, $storage);
91-
// Errors signal programmer mistakes (ValueError, TypeError, DivisionByZeroError...),
92-
// nobody calls an otherwise pure expression just to have them thrown, so they
93-
// do not make the expression statement meaningful. A `throw` written in the
94-
// statement is a different story - throwing is the whole point of it.
95-
$errorType = new ObjectType(Error::class);
96-
$throwPoints = array_filter($result->getThrowPoints(), static fn (InternalThrowPoint $throwPoint) => $throwPoint->isExplicit() && ($throwPoint->isFromThrowExpr() || !$errorType->isSuperTypeOf($throwPoint->getType())->yes()));
88+
$throwPoints = array_filter($result->getThrowPoints(), static fn ($throwPoint) => $throwPoint->isExplicit());
9789
if (
9890
count($result->getImpurePoints()) === 0
9991
&& count($throwPoints) === 0

‎src/Analyser/ThrowPoint.php‎

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,16 @@ private function __construct(
2525
private Node $node,
2626
private bool $explicit,
2727
private bool $canContainAnyThrowable,
28-
private bool $fromThrowExpr = false,
2928
)
3029
{
3130
}
3231

3332
/**
3433
* @param Node\Expr|Node\Stmt $node
3534
*/
36-
public static function createExplicit(Scope $scope, Type $type, Node $node, bool $canContainAnyThrowable, bool $fromThrowExpr = false): self
35+
public static function createExplicit(Scope $scope, Type $type, Node $node, bool $canContainAnyThrowable): self
3736
{
38-
return new self($scope, $type, $node, true, $canContainAnyThrowable, $fromThrowExpr);
37+
return new self($scope, $type, $node, true, $canContainAnyThrowable);
3938
}
4039

4140
/**
@@ -74,19 +73,9 @@ public function canContainAnyThrowable(): bool
7473
return $this->canContainAnyThrowable;
7574
}
7675

77-
/**
78-
* Whether the throw point comes from a `throw` written in the analysed code,
79-
* as opposed to a throw inferred from what a called function or an operation
80-
* can throw.
81-
*/
82-
public function isFromThrowExpr(): bool
83-
{
84-
return $this->fromThrowExpr;
85-
}
86-
8776
public function subtractCatchType(Type $catchType): self
8877
{
89-
return new self($this->scope, TypeCombinator::remove($this->type, $catchType), $this->node, $this->explicit, $this->canContainAnyThrowable, $this->fromThrowExpr);
78+
return new self($this->scope, TypeCombinator::remove($this->type, $catchType), $this->node, $this->explicit, $this->canContainAnyThrowable);
9079
}
9180

9281
}

‎src/Reflection/Callables/SimpleThrowPoint.php‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@ private function __construct(
2626
private Type $type,
2727
private bool $explicit,
2828
private bool $canContainAnyThrowable,
29-
private bool $fromThrowExpr = false,
3029
)
3130
{
3231
}
3332

34-
public static function createExplicit(Type $type, bool $canContainAnyThrowable, bool $fromThrowExpr = false): self
33+
public static function createExplicit(Type $type, bool $canContainAnyThrowable): self
3534
{
36-
return new self($type, true, $canContainAnyThrowable, $fromThrowExpr);
35+
return new self($type, true, $canContainAnyThrowable);
3736
}
3837

3938
public static function createImplicit(): self
@@ -56,13 +55,4 @@ public function canContainAnyThrowable(): bool
5655
return $this->canContainAnyThrowable;
5756
}
5857

59-
/**
60-
* Whether the throw point comes from a `throw` written in the callable's body,
61-
* as opposed to a throw only declared in PHPDoc.
62-
*/
63-
public function isFromThrowExpr(): bool
64-
{
65-
return $this->fromThrowExpr;
66-
}
67-
6858
}

‎tests/PHPStan/Rules/DeadCode/NoopRuleTest.php‎

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -143,20 +143,7 @@ public function testRule(): void
143143
#[RequiresPhp('>= 8.0.0')]
144144
public function testErrorThrows(): void
145145
{
146-
$this->analyse([__DIR__ . '/data/noop-error-throws.php'], [
147-
[
148-
'Expression "$a / $b" on a separate line does not do anything.',
149-
6,
150-
],
151-
[
152-
'Expression "$a % $b" on a separate line does not do anything.',
153-
7,
154-
],
155-
[
156-
'Expression "match ($a) {…" on a separate line does not do anything.',
157-
8,
158-
],
159-
]);
146+
$this->analyse([__DIR__ . '/data/noop-error-throws.php'], []);
160147
}
161148

162149
#[RequiresPhp('>= 8.0.0')]

‎tests/PHPStan/Rules/Functions/CallToFunctionStatementWithoutSideEffectsRuleTest.php‎

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -113,30 +113,10 @@ public function testBug11101(): void
113113
]);
114114
}
115115

116+
#[RequiresPhp('>= 8.0.0')]
116117
public function testErrorThrows(): void
117118
{
118-
$this->analyse([__DIR__ . '/data/function-call-statement-no-side-effects-error-throws.php'], [
119-
[
120-
'Call to function sprintf() on a separate line has no effect.',
121-
22,
122-
],
123-
[
124-
'Call to function strpos() on a separate line has no effect.',
125-
23,
126-
],
127-
[
128-
'Call to function intdiv() on a separate line has no effect.',
129-
24,
130-
],
131-
[
132-
'Call to function array_combine() on a separate line has no effect.',
133-
25,
134-
],
135-
[
136-
'Call to function FunctionCallStatementNoSideEffectsErrorThrows\\pureAndThrowsError() on a separate line has no effect.',
137-
27,
138-
],
139-
]);
119+
$this->analyse([__DIR__ . '/data/function-call-statement-no-side-effects-error-throws.php'], []);
140120
}
141121

142122
#[RequiresPhp('>= 8.0.0')]

0 commit comments

Comments
 (0)