Skip to content

Commit abab32b

Browse files
authored
skip framework and web profile bundle imports in NoRoutingPrefixRule (#181)
1 parent dac84b8 commit abab32b

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

src/Rules/Symfony/NoRoutingPrefixRule.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Expr\MethodCall;
99
use PhpParser\Node\Identifier;
10+
use PhpParser\Node\Scalar\String_;
1011
use PHPStan\Analyser\Scope;
1112
use PHPStan\Rules\Rule;
1213
use PHPStan\Rules\RuleError;
@@ -42,8 +43,7 @@ public function processNode(Node $node, Scope $scope): array
4243
return [];
4344
}
4445

45-
$methodName = $node->name->toString();
46-
if ($methodName !== 'prefix') {
46+
if ($node->name->toString() !== 'prefix') {
4747
return [];
4848
}
4949

@@ -56,11 +56,39 @@ public function processNode(Node $node, Scope $scope): array
5656
return [];
5757
}
5858

59+
if ($this->isAllowedExternalBundleImport($node)) {
60+
return [];
61+
}
62+
5963
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
6064
->line($node->getStartLine())
6165
->identifier(SymfonyRuleIdentifier::NO_ROUTING_PREFIX)
6266
->build();
6367

6468
return [$ruleError];
6569
}
70+
71+
private function isAllowedExternalBundleImport(MethodCall $methodCall): bool
72+
{
73+
if (! $methodCall->var instanceof MethodCall) {
74+
return false;
75+
}
76+
77+
$parentCaller = $methodCall->var;
78+
if (! $parentCaller->name instanceof Identifier || $parentCaller->name->toString() !== 'import') {
79+
return false;
80+
}
81+
82+
$importArgPath = $parentCaller->getArgs()[0]->value;
83+
if (! $importArgPath instanceof String_) {
84+
return false;
85+
}
86+
87+
// these external bundles are typically prefixed on purpose
88+
if (str_starts_with($importArgPath->value, '@FrameworkBundle')) {
89+
return true;
90+
}
91+
92+
return str_starts_with($importArgPath->value, '@WebProfilerBundle');
93+
}
6694
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
6+
7+
return static function (RoutingConfigurator $routingConfigurator): void {
8+
$routingConfigurator->import('@FrameworkBundle/Resources/config/routing/errors.xml')
9+
->prefix('/_error');
10+
};

tests/Rules/Symfony/NoRoutingPrefixRule/NoRoutingPrefixRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public static function provideData(): Iterator
2525
__DIR__ . '/Fixture/skip_no_route_prefix.php',
2626
], []];
2727

28+
yield [[
29+
__DIR__ . '/Fixture/skip_bundle_import.php',
30+
], []];
31+
2832
yield [[
2933
__DIR__ . '/Fixture/routing_imports.php',
3034
], [[NoRoutingPrefixRule::ERROR_MESSAGE, 8]]];

0 commit comments

Comments
 (0)