Skip to content

Commit ae49138

Browse files
committed
feat: add ForbidGlobBraceRule with tests
1 parent c3ebe44 commit ae49138

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

rules.neon

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ rules:
77
- Shopware\PhpStan\Rule\MethodBecomesAbstractRule
88
- Shopware\PhpStan\Rule\ScheduledTaskTooLowIntervalRule
99
- Shopware\PhpStan\Rule\InternalClassExtendsRule
10+
- Shopware\PhpStan\Rule\ForbidGlobBraceRule
1011

1112
services:
1213
-

src/Rule/ForbidGlobBraceRule.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Shopware\PhpStan\Rule;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Expr\ConstFetch;
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\Rules\Rule;
9+
use PHPStan\Rules\RuleErrorBuilder;
10+
11+
/**
12+
* @implements Rule<ConstFetch>
13+
*/
14+
final class ForbidGlobBraceRule implements Rule
15+
{
16+
public const ERROR_IDENTIFIER = 'shopware.forbidGlobBrace';
17+
18+
public function getNodeType(): string
19+
{
20+
return ConstFetch::class;
21+
}
22+
23+
public function processNode(Node $node, Scope $scope): array
24+
{
25+
if ($node->name->toString() === 'GLOB_BRACE') {
26+
return [
27+
RuleErrorBuilder::message(
28+
'Usage of GLOB_BRACE constant is forbidden. GLOB_BRACE is not supported on any platform.',
29+
)
30+
->identifier(self::ERROR_IDENTIFIER)
31+
->build(),
32+
];
33+
}
34+
35+
return [];
36+
}
37+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Shopware\PhpStan\Tests\Rule;
4+
5+
use PHPStan\Testing\RuleTestCase;
6+
use PHPStan\Rules\Rule;
7+
use Shopware\PhpStan\Rule\ForbidGlobBraceRule;
8+
9+
/**
10+
* @extends RuleTestCase<ForbidGlobBraceRule>
11+
*/
12+
final class ForbidGlobBraceRuleTest extends RuleTestCase
13+
{
14+
protected function getRule(): Rule
15+
{
16+
return new ForbidGlobBraceRule();
17+
}
18+
19+
public function testRule(): void
20+
{
21+
$this->analyse([__DIR__ . '/../data/ForbidGlobBraceRule/glob.php'], [
22+
[
23+
'Usage of GLOB_BRACE constant is forbidden. GLOB_BRACE is not supported on any platform.',
24+
3,
25+
],
26+
[
27+
'Usage of GLOB_BRACE constant is forbidden. GLOB_BRACE is not supported on any platform.',
28+
6,
29+
],
30+
]);
31+
}
32+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
$files = glob('*.{jpg,jpeg,png}', GLOB_BRACE);
4+
5+
// Should also detect when used with other constants
6+
$files = glob('*.{jpg,jpeg,png}', GLOB_BRACE | GLOB_NOSORT);

0 commit comments

Comments
 (0)