Skip to content

Commit cf11c9e

Browse files
committed
✨ No basic exception rule
1 parent 8027837 commit cf11c9e

5 files changed

Lines changed: 96 additions & 6 deletions

File tree

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ For all available options, please take a look at the PHPStan documentation: **ht
4646

4747
## Rules
4848

49+
### Boolean Property Naming Rule
50+
51+
*Identifier : martinsoenen.booleanPropertyNaming*
52+
53+
Ensure that a Boolean is always named with an “is” at the beginning of its name, to clarify the condition it represents.
54+
4955
### Max Line Per Class Rule
5056

5157
*Identifier : martinsoenen.maxLinePerClass*
@@ -58,6 +64,12 @@ Guarantee that a class is no more than 100 lines long, to clearly separate roles
5864

5965
Guarantee that a method is no more than 20 lines long, to separate roles between classes and encourage specification.
6066

67+
### No Basic Exception Rule
68+
69+
*Identifier : martinsoenen.noBasicException*
70+
71+
Ensure that no base exceptions are thrown in order to make custom and specific exceptions everywhere for better maintainability.
72+
6173
### No Delete Cascade Rule (Laravel)
6274

6375
*Identifier : martinsoenen.noCascadeDeleteLaravel*
@@ -87,9 +99,3 @@ This rule encourages the use of Events & Listeners, whose behavior is similar bu
8799

88100
Prevent `try catch` usage to ensure that the user uses PHP exceptions.
89101
This makes the code more readable and errors are handled by the framework if there is one.
90-
91-
### Boolean Property Naming Rule
92-
93-
*Identifier : martinsoenen.booleanPropertyNaming*
94-
95-
Ensure that a Boolean is always named with an “is” at the beginning of its name, to clarify the condition it represents.

src/Rules/NoBasicExceptionRule.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace MartinSoenen\PHPStanRules\Rules;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Rules\Rule;
8+
use PHPStan\Rules\RuleErrorBuilder;
9+
10+
class NoBasicExceptionRule implements Rule
11+
{
12+
public function getNodeType(): string
13+
{
14+
return Node\Expr\Throw_::class;
15+
}
16+
17+
public function processNode(Node $node, Scope $scope): array
18+
{
19+
if ($node->expr instanceof Node\Expr\New_) {
20+
$exceptionClass = $node->expr->class;
21+
if ($exceptionClass instanceof Node\Name && $exceptionClass->toString() === 'Exception') {
22+
return [
23+
RuleErrorBuilder::message('Basic exceptions are not allowed. Please create a custom Exception instead.')
24+
->line($node->getLine())
25+
->identifier('martinsoenen.noBasicException')
26+
->build()
27+
];
28+
}
29+
}
30+
31+
return [];
32+
}
33+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace MartinSoenen\PHPStanRules\Tests\Rules;
4+
5+
use PHPStan\Testing\RuleTestCase;
6+
use MartinSoenen\PHPStanRules\Rules\NoBasicExceptionRule;
7+
8+
/**
9+
* @extends RuleTestCase<NoBasicExceptionRule>
10+
*/
11+
class NoBasicExceptionRuleTest extends RuleTestCase
12+
{
13+
protected function getRule(): \PHPStan\Rules\Rule
14+
{
15+
return new NoBasicExceptionRule();
16+
}
17+
18+
public function testRuleThrowsException(): void
19+
{
20+
$this->analyse([__DIR__ . '/data/NoBasicExceptionRule/BasicExceptionClass.php'], [
21+
["Basic exceptions are not allowed. Please create a custom Exception instead.", 8]
22+
]);
23+
}
24+
25+
public function testRuleDoesNotThrowsException(): void
26+
{
27+
$this->analyse([__DIR__ . '/data/NoBasicExceptionRule/AdvancedExceptionClass.php'], []);
28+
}
29+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace MartinSoenen\PHPStanRules\Tests\Rules\data\NoBasicExceptionRule;
4+
5+
use MartinSoenen\PHPStanRules\Tests\Rules\data\FooException;
6+
7+
class AdvancedExceptionClass
8+
{
9+
public function doFoo() {
10+
throw new FooException("Test exception");
11+
}
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace MartinSoenen\PHPStanRules\Tests\Rules\data\NoBasicExceptionRule;
4+
5+
class BasicExceptionClass
6+
{
7+
public function doFoo() {
8+
throw new \Exception("Test exception");
9+
}
10+
}

0 commit comments

Comments
 (0)