Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Php/PhpVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ public function deprecatesBacktickOperator(): TrinaryLogic
return IntegerRangeType::fromInterval(80500, null)->isSuperTypeOf($this->phpVersions)->result;
}

public function deprecatesReturnInFinally(): TrinaryLogic
{
return IntegerRangeType::fromInterval(80600, null)->isSuperTypeOf($this->phpVersions)->result;
}

public function deprecatesDecOnNonNumericString(): TrinaryLogic
{
return IntegerRangeType::fromInterval(80300, null)->isSuperTypeOf($this->phpVersions)->result;
Expand Down
88 changes: 88 additions & 0 deletions src/Rules/Exceptions/ReturnInFinallyRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Exceptions;

use PhpParser\Node;
use PhpParser\Node\Stmt;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function is_array;

/**
* @implements Rule<Stmt\TryCatch>
*/
#[RegisteredRule(level: 0)]
final class ReturnInFinallyRule implements Rule
{

public function __construct(
#[AutowiredParameter(ref: '%featureToggles.checkWholePhpVersionRange%')]
private bool $checkWholePhpVersionRange,
)
{
}

public function getNodeType(): string
{
return Stmt\TryCatch::class;
}

public function processNode(Node $node, Scope $scope): array
{
if ($node->finally === null) {
return [];
}

$deprecatesReturnInFinally = $scope->getPhpVersion()->deprecatesReturnInFinally();
if ($this->checkWholePhpVersionRange ? $deprecatesReturnInFinally->no() : !$deprecatesReturnInFinally->yes()) {
return [];
}

$errors = [];
foreach ($this->findReturns($node->finally->stmts) as $return) {
$errors[] = RuleErrorBuilder::message('Returning from a finally block is deprecated in PHP 8.6.')
->line($return->getStartLine())
->identifier('finally.deprecatedReturn')
->build();
}

return $errors;
}

/**
* Like PHP, looks for return statements lexically, without descending into
* functions, classes, and expressions (which is where closures live).
* A nested try's own finally block is left out because this rule reports it separately.
*
* @param Node[] $nodes
* @return list<Stmt\Return_>
*/
private function findReturns(array $nodes): array
{
$returns = [];
foreach ($nodes as $node) {
if (!$node instanceof Stmt || $node instanceof Stmt\Function_ || $node instanceof Stmt\ClassLike) {
continue;
}

if ($node instanceof Stmt\Return_) {
$returns[] = $node;
continue;
}

$subNodeNames = $node instanceof Stmt\TryCatch ? ['stmts', 'catches'] : $node->getSubNodeNames();
foreach ($subNodeNames as $subNodeName) {
$subNode = $node->$subNodeName;
foreach ($this->findReturns(is_array($subNode) ? $subNode : [$subNode]) as $return) {
$returns[] = $return;
}
}
}

return $returns;
}

}
86 changes: 86 additions & 0 deletions tests/PHPStan/Rules/Exceptions/ReturnInFinallyRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Exceptions;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

/**
* @extends RuleTestCase<ReturnInFinallyRule>
*/
class ReturnInFinallyRuleTest extends RuleTestCase
{

private bool $checkWholePhpVersionRange;

protected function getRule(): Rule
{
return new ReturnInFinallyRule($this->checkWholePhpVersionRange);
}

public function testRuleWithWholePhpVersionRange(): void
{
$this->checkWholePhpVersionRange = true;
$this->analyse([__DIR__ . '/data/return-in-finally.php'], [
[
'Returning from a finally block is deprecated in PHP 8.6.',
9,
],
[
'Returning from a finally block is deprecated in PHP 8.6.',
17,
],
[
'Returning from a finally block is deprecated in PHP 8.6.',
53,
],
[
'Returning from a finally block is deprecated in PHP 8.6.',
55,
],
[
'Returning from a finally block is deprecated in PHP 8.6.',
57,
],
[
'Returning from a finally block is deprecated in PHP 8.6.',
70,
],
[
'Returning from a finally block is deprecated in PHP 8.6.',
82,
],
[
'Returning from a finally block is deprecated in PHP 8.6.',
86,
],
[
'Returning from a finally block is deprecated in PHP 8.6.',
90,
],
[
'Returning from a finally block is deprecated in PHP 8.6.',
102,
],
]);
}

public function testRule(): void
{
$this->checkWholePhpVersionRange = false;
$this->analyse([__DIR__ . '/data/return-in-finally.php'], [
[
'Returning from a finally block is deprecated in PHP 8.6.',
102,
],
]);
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/data/return-in-finally-php-version.neon',
];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
phpVersion:
min: 80500
max: 80600
112 changes: 112 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/return-in-finally.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace ReturnInFinally;

function withValue()
{
try {
} finally {
return 1;
}
}

function withoutValue()
{
try {
} finally {
return;
}
}

function inTryAndCatch()
{
try {
return 1;
} catch (\Exception $e) {
return 2;
} finally {
}
}

function nestedClosureAndClass()
{
try {
} finally {
$f = function () {
return 1;
};
$g = fn () => 2;
$o = new class () {
public function foo()
{
return 3;
}
};
}
}

function nestedTry()
{
try {
} finally {
try {
return 1;
} catch (\Exception $e) {
return 2;
} finally {
return 3;
}
}
}

function closureWithTry()
{
try {
} finally {
$f = function () {
try {
return 1;
} finally {
return 2;
}
};
}
}

function nestedControlFlow(array $a)
{
try {
} finally {
foreach ($a as $v) {
if ($v) {
return 1;
} elseif ($v === 0) {
switch ($v) {
case 0:
return 2;
}
} else {
{
return 3;
}
}
}
}
}

function phpVersionBranches()
{
if (PHP_VERSION_ID >= 80600) {
try {
} finally {
return 1;
}
}

if (PHP_VERSION_ID < 80600) {
try {
} finally {
return 2;
}
}
}
Loading