-
Notifications
You must be signed in to change notification settings - Fork 430
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b2ffb8
commit cc41a91
Showing
6 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Exceptions; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
/** | ||
* @implements Rule<Node\Stmt\Catch_> | ||
*/ | ||
class NoncapturingCatchRule implements Rule | ||
{ | ||
|
||
public function __construct(private PhpVersion $phpVersion) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\Catch_::class; | ||
} | ||
|
||
/** | ||
* @param Node\Stmt\Catch_ $node | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if ($this->phpVersion->supportsNoncapturingCatches()) { | ||
return []; | ||
} | ||
|
||
if ($node->var !== null) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message('Noncapturing catch is supported only on PHP 8.0 and later.') | ||
->nonIgnorable() | ||
->build(), | ||
]; | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
tests/PHPStan/Rules/Exceptions/NoncapturingCatchRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Exceptions; | ||
|
||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<NoncapturingCatchRule> | ||
*/ | ||
class NoncapturingCatchRuleTest extends RuleTestCase | ||
{ | ||
|
||
private PhpVersion $phpVersion; | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new NoncapturingCatchRule($this->phpVersion); | ||
} | ||
|
||
public function dataRule(): array | ||
{ | ||
return [ | ||
[ | ||
70400, | ||
[ | ||
[ | ||
'Noncapturing catch is supported only on PHP 8.0 and later.', | ||
12, | ||
], | ||
[ | ||
'Noncapturing catch is supported only on PHP 8.0 and later.', | ||
21, | ||
], | ||
], | ||
], | ||
[ | ||
80000, | ||
[], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataRule | ||
* | ||
* @param list<array{0: string, 1: int, 2?: string}> $expectedErrors | ||
*/ | ||
public function testRule(int $phpVersion, array $expectedErrors): void | ||
{ | ||
$this->phpVersion = new PhpVersion($phpVersion); | ||
|
||
$this->analyse([ | ||
__DIR__ . '/data/noncapturing-catch.php', | ||
__DIR__ . '/data/bug-8663.php', | ||
], $expectedErrors); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Bug8663; | ||
|
||
/** | ||
* Provides example to demonstrate an issue with PHPStan. | ||
*/ | ||
class StanExample2 | ||
{ | ||
|
||
/** | ||
* An exception is caught but not captured. | ||
* | ||
* That's OK for PHP 8 but not for 7.4 - PHPStan does not report the issue. | ||
*/ | ||
public function catchExceptionsWithoutCapturing(): void | ||
{ | ||
try { | ||
print 'Lets do something nasty here.'; | ||
throw new \Exception('This is nasty'); | ||
} catch (\Exception) { | ||
print 'Exception occured'; | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/PHPStan/Rules/Exceptions/data/noncapturing-catch.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php // lint >= 8.0 | ||
|
||
namespace NoncapturingCatch; | ||
|
||
class HelloWorld | ||
{ | ||
|
||
public function hello(): void | ||
{ | ||
try { | ||
throw new \Exception('Hello'); | ||
} catch (\Exception) { | ||
echo 'Hi!'; | ||
} | ||
} | ||
|
||
} |