Skip to content

Commit cd16cbc

Browse files
committed
Report deprecated return in finally block on PHP 8.6
PHP 8.6 deprecates returning from a finally block at compile time. Like PHP, the rule looks for return statements lexically in the finally block, including nested try and catch blocks, but not in closures, arrow functions, anonymous classes or nested function declarations. A nested try's own finally block is reported by its own TryCatch node. Amp-Thread-ID: https://ampcode.com/threads/T-01a1dfa1-37b0-77d8-96bc-e3bdf810d9d7
1 parent d0fcfc5 commit cd16cbc

5 files changed

Lines changed: 295 additions & 0 deletions

File tree

‎src/Php/PhpVersions.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ public function deprecatesBacktickOperator(): TrinaryLogic
254254
return IntegerRangeType::fromInterval(80500, null)->isSuperTypeOf($this->phpVersions)->result;
255255
}
256256

257+
public function deprecatesReturnInFinally(): TrinaryLogic
258+
{
259+
return IntegerRangeType::fromInterval(80600, null)->isSuperTypeOf($this->phpVersions)->result;
260+
}
261+
257262
public function deprecatesDecOnNonNumericString(): TrinaryLogic
258263
{
259264
return IntegerRangeType::fromInterval(80300, null)->isSuperTypeOf($this->phpVersions)->result;
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Exceptions;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Stmt;
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\DependencyInjection\AutowiredParameter;
9+
use PHPStan\DependencyInjection\RegisteredRule;
10+
use PHPStan\Rules\Rule;
11+
use PHPStan\Rules\RuleErrorBuilder;
12+
use function is_array;
13+
14+
/**
15+
* @implements Rule<Stmt\TryCatch>
16+
*/
17+
#[RegisteredRule(level: 0)]
18+
final class ReturnInFinallyRule implements Rule
19+
{
20+
21+
public function __construct(
22+
#[AutowiredParameter(ref: '%featureToggles.checkWholePhpVersionRange%')]
23+
private bool $checkWholePhpVersionRange,
24+
)
25+
{
26+
}
27+
28+
public function getNodeType(): string
29+
{
30+
return Stmt\TryCatch::class;
31+
}
32+
33+
public function processNode(Node $node, Scope $scope): array
34+
{
35+
if ($node->finally === null) {
36+
return [];
37+
}
38+
39+
$deprecatesReturnInFinally = $scope->getPhpVersion()->deprecatesReturnInFinally();
40+
if ($this->checkWholePhpVersionRange ? $deprecatesReturnInFinally->no() : !$deprecatesReturnInFinally->yes()) {
41+
return [];
42+
}
43+
44+
$errors = [];
45+
foreach ($this->findReturns($node->finally->stmts) as $return) {
46+
$errors[] = RuleErrorBuilder::message('Returning from a finally block is deprecated in PHP 8.6.')
47+
->line($return->getStartLine())
48+
->identifier('finally.deprecatedReturn')
49+
->build();
50+
}
51+
52+
return $errors;
53+
}
54+
55+
/**
56+
* Like PHP, looks for return statements lexically, without descending into
57+
* functions, classes, and expressions (which is where closures live).
58+
* A nested try's own finally block is left out because this rule reports it separately.
59+
*
60+
* @param Node[] $nodes
61+
* @return list<Stmt\Return_>
62+
*/
63+
private function findReturns(array $nodes): array
64+
{
65+
$returns = [];
66+
foreach ($nodes as $node) {
67+
if (!$node instanceof Stmt || $node instanceof Stmt\Function_ || $node instanceof Stmt\ClassLike) {
68+
continue;
69+
}
70+
71+
if ($node instanceof Stmt\Return_) {
72+
$returns[] = $node;
73+
continue;
74+
}
75+
76+
$subNodeNames = $node instanceof Stmt\TryCatch ? ['stmts', 'catches'] : $node->getSubNodeNames();
77+
foreach ($subNodeNames as $subNodeName) {
78+
$subNode = $node->$subNodeName;
79+
foreach ($this->findReturns(is_array($subNode) ? $subNode : [$subNode]) as $return) {
80+
$returns[] = $return;
81+
}
82+
}
83+
}
84+
85+
return $returns;
86+
}
87+
88+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Exceptions;
4+
5+
use PHPStan\Rules\Rule;
6+
use PHPStan\Testing\RuleTestCase;
7+
8+
/**
9+
* @extends RuleTestCase<ReturnInFinallyRule>
10+
*/
11+
class ReturnInFinallyRuleTest extends RuleTestCase
12+
{
13+
14+
private bool $checkWholePhpVersionRange;
15+
16+
protected function getRule(): Rule
17+
{
18+
return new ReturnInFinallyRule($this->checkWholePhpVersionRange);
19+
}
20+
21+
public function testRuleWithWholePhpVersionRange(): void
22+
{
23+
$this->checkWholePhpVersionRange = true;
24+
$this->analyse([__DIR__ . '/data/return-in-finally.php'], [
25+
[
26+
'Returning from a finally block is deprecated in PHP 8.6.',
27+
9,
28+
],
29+
[
30+
'Returning from a finally block is deprecated in PHP 8.6.',
31+
17,
32+
],
33+
[
34+
'Returning from a finally block is deprecated in PHP 8.6.',
35+
53,
36+
],
37+
[
38+
'Returning from a finally block is deprecated in PHP 8.6.',
39+
55,
40+
],
41+
[
42+
'Returning from a finally block is deprecated in PHP 8.6.',
43+
57,
44+
],
45+
[
46+
'Returning from a finally block is deprecated in PHP 8.6.',
47+
70,
48+
],
49+
[
50+
'Returning from a finally block is deprecated in PHP 8.6.',
51+
82,
52+
],
53+
[
54+
'Returning from a finally block is deprecated in PHP 8.6.',
55+
86,
56+
],
57+
[
58+
'Returning from a finally block is deprecated in PHP 8.6.',
59+
90,
60+
],
61+
[
62+
'Returning from a finally block is deprecated in PHP 8.6.',
63+
102,
64+
],
65+
]);
66+
}
67+
68+
public function testRule(): void
69+
{
70+
$this->checkWholePhpVersionRange = false;
71+
$this->analyse([__DIR__ . '/data/return-in-finally.php'], [
72+
[
73+
'Returning from a finally block is deprecated in PHP 8.6.',
74+
102,
75+
],
76+
]);
77+
}
78+
79+
public static function getAdditionalConfigFiles(): array
80+
{
81+
return [
82+
__DIR__ . '/data/return-in-finally-php-version.neon',
83+
];
84+
}
85+
86+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
phpVersion:
3+
min: 80500
4+
max: 80600
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
namespace ReturnInFinally;
4+
5+
function withValue()
6+
{
7+
try {
8+
} finally {
9+
return 1;
10+
}
11+
}
12+
13+
function withoutValue()
14+
{
15+
try {
16+
} finally {
17+
return;
18+
}
19+
}
20+
21+
function inTryAndCatch()
22+
{
23+
try {
24+
return 1;
25+
} catch (\Exception $e) {
26+
return 2;
27+
} finally {
28+
}
29+
}
30+
31+
function nestedClosureAndClass()
32+
{
33+
try {
34+
} finally {
35+
$f = function () {
36+
return 1;
37+
};
38+
$g = fn () => 2;
39+
$o = new class () {
40+
public function foo()
41+
{
42+
return 3;
43+
}
44+
};
45+
}
46+
}
47+
48+
function nestedTry()
49+
{
50+
try {
51+
} finally {
52+
try {
53+
return 1;
54+
} catch (\Exception $e) {
55+
return 2;
56+
} finally {
57+
return 3;
58+
}
59+
}
60+
}
61+
62+
function closureWithTry()
63+
{
64+
try {
65+
} finally {
66+
$f = function () {
67+
try {
68+
return 1;
69+
} finally {
70+
return 2;
71+
}
72+
};
73+
}
74+
}
75+
76+
function nestedControlFlow(array $a)
77+
{
78+
try {
79+
} finally {
80+
foreach ($a as $v) {
81+
if ($v) {
82+
return 1;
83+
} elseif ($v === 0) {
84+
switch ($v) {
85+
case 0:
86+
return 2;
87+
}
88+
} else {
89+
{
90+
return 3;
91+
}
92+
}
93+
}
94+
}
95+
}
96+
97+
function phpVersionBranches()
98+
{
99+
if (PHP_VERSION_ID >= 80600) {
100+
try {
101+
} finally {
102+
return 1;
103+
}
104+
}
105+
106+
if (PHP_VERSION_ID < 80600) {
107+
try {
108+
} finally {
109+
return 2;
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)