Skip to content

Commit 3391874

Browse files
committed
✨ Banned Functions Rule
1 parent 3a8f5b5 commit 3391874

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

README.md

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

4848
## Rules
4949

50+
### Banned Functions Rule
51+
52+
*Identifier : xefi.bannedFunctions*
53+
54+
Ensure banned functions like `dd()` are not called.
55+
5056
### Boolean Property Naming Rule
5157

5258
*Identifier : xefi.booleanPropertyNaming*

extension.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
rules:
2+
- Xefi\PHPStanRules\Rules\BannedFunctionsRule
23
- Xefi\PHPStanRules\Rules\BooleanPropertyNamingRule
34
- Xefi\PHPStanRules\Rules\MaxLinePerClassRule
45
- Xefi\PHPStanRules\Rules\MaxLinePerMethodRule

src/Rules/BannedFunctionsRule.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Xefi\PHPStanRules\Rules;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Name;
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\Rules\Rule;
9+
use PHPStan\Rules\RuleErrorBuilder;
10+
11+
class BannedFunctionsRule implements Rule
12+
{
13+
const BANNED_FUNCTIONS = ['dd'];
14+
15+
public function getNodeType(): string
16+
{
17+
return Node\Expr\FuncCall::class;
18+
}
19+
20+
public function processNode(Node $node, Scope $scope): array
21+
{
22+
$errors = [];
23+
24+
if (! $node->name instanceof Name) {
25+
return [];
26+
}
27+
28+
$functionName = $node->name->toString();
29+
30+
if (in_array($functionName, self::BANNED_FUNCTIONS, true)) {
31+
$errors[] = RuleErrorBuilder::message(
32+
sprintf('Method %s() is prohibited. Remove it or write in the logs using `Log::debug()` instead.', $functionName)
33+
)
34+
->identifier('xefi.bannedFunctions')
35+
->build();
36+
}
37+
38+
return $errors;
39+
}
40+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Xefi\PHPStanRules\Tests\Rules;
4+
5+
use Xefi\PHPStanRules\Rules\BannedFunctionsRule;
6+
use PHPStan\Rules\Rule;
7+
use PHPStan\Testing\RuleTestCase;
8+
9+
/**
10+
* @extends RuleTestCase<BannedFunctionsRule>
11+
*/
12+
class BannedFunctionsRuleTest extends RuleTestCase
13+
{
14+
protected function getRule(): Rule
15+
{
16+
return new BannedFunctionsRule();
17+
}
18+
19+
public function testRuleDetectsBannedFunctions(): void
20+
{
21+
$this->analyse([__DIR__ . '/data/BannedFunctionsRule/BannedFunctionsClass.php'], [
22+
[
23+
'Method dd() is prohibited. Remove it or write in the logs using `Log::debug()` instead.',
24+
3
25+
],
26+
]);
27+
}
28+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
dd('stop');
4+
array_merge(['a'], ['b']);
5+
echo 'test';

0 commit comments

Comments
 (0)