Skip to content

Commit 480245d

Browse files
authored
Merge pull request #459 from mglaman/gh458
More specific rules for RequestStack shim
2 parents 64ff852 + 2945b49 commit 480245d

8 files changed

+112
-194
lines changed

rules.neon

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,4 @@ rules:
1717
- mglaman\PHPStanDrupal\Rules\Drupal\EntityQuery\EntityQueryHasAccessCheckRule
1818
- mglaman\PHPStanDrupal\Rules\Deprecations\SymfonyCmfRouteObjectInterfaceConstantsRule
1919
- mglaman\PHPStanDrupal\Rules\Deprecations\SymfonyCmfRoutingInClassMethodSignatureRule
20-
- mglaman\PHPStanDrupal\Rules\Drupal\DrupalRequestStackShimInClassMethodRule
21-
- mglaman\PHPStanDrupal\Rules\Drupal\DrupalRequestStackShimInstantiationRule
20+
- mglaman\PHPStanDrupal\Rules\Drupal\RequestStackGetMainRequestRule

src/Rules/Drupal/DrupalRequestStackShimInClassMethodRule.php

Lines changed: 0 additions & 71 deletions
This file was deleted.

src/Rules/Drupal/DrupalRequestStackShimInstantiationRule.php

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace mglaman\PHPStanDrupal\Rules\Drupal;
4+
5+
use Drupal\Core\Http\RequestStack as DrupalRequestStack;
6+
use mglaman\PHPStanDrupal\Internal\DeprecatedScopeCheck;
7+
use PhpParser\Node;
8+
use PHPStan\Analyser\Scope;
9+
use PHPStan\Rules\Rule;
10+
use PHPStan\Rules\RuleErrorBuilder;
11+
use PHPStan\Type\ObjectType;
12+
use Symfony\Component\HttpFoundation\RequestStack as SymfonyRequestStack;
13+
14+
final class RequestStackGetMainRequestRule implements Rule
15+
{
16+
17+
public function getNodeType(): string
18+
{
19+
return Node\Expr\MethodCall::class;
20+
}
21+
22+
public function processNode(Node $node, Scope $scope): array
23+
{
24+
assert($node instanceof Node\Expr\MethodCall);
25+
if (DeprecatedScopeCheck::inDeprecatedScope($scope)) {
26+
return [];
27+
}
28+
[$major, $minor] = explode('.', \Drupal::VERSION, 3);
29+
// Only valid for 9.3 -> 9.5. Deprecated in Drupal 10.
30+
if ($major !== '9' || (int) $minor < 3) {
31+
return [];
32+
}
33+
if (!$node->name instanceof Node\Identifier) {
34+
return [];
35+
}
36+
$method_name = $node->name->toString();
37+
if ($method_name !== 'getMasterRequest') {
38+
return [];
39+
}
40+
$type = $scope->getType($node->var);
41+
$symfonyRequestStackType = new ObjectType(SymfonyRequestStack::class);
42+
if ($symfonyRequestStackType->isSuperTypeOf($type)->yes()) {
43+
$message = sprintf(
44+
'%s::getMasterRequest() is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0 for Symfony 6 compatibility. Use the forward compatibility shim class %s and its getMainRequest() method instead.',
45+
SymfonyRequestStack::class,
46+
DrupalRequestStack::class
47+
);
48+
return [
49+
RuleErrorBuilder::message($message)
50+
->tip('Change record: https://www.drupal.org/node/3253744')
51+
->build(),
52+
];
53+
}
54+
return [];
55+
}
56+
}

tests/src/Rules/DrupalRequestStackShimInClassMethodRuleTest.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

tests/src/Rules/DrupalRequestStackShimInstantiationRuleTest.php

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace mglaman\PHPStanDrupal\Tests\Rules;
4+
5+
use mglaman\PHPStanDrupal\Rules\Drupal\DrupalRequestStackShimInstantiationRule;
6+
use mglaman\PHPStanDrupal\Rules\Drupal\RequestStackGetMainRequestRule;
7+
use mglaman\PHPStanDrupal\Tests\DrupalRuleTestCase;
8+
9+
final class RequestStackGetMainRequestRuleTest extends DrupalRuleTestCase
10+
{
11+
12+
protected function getRule(): \PHPStan\Rules\Rule
13+
{
14+
return new RequestStackGetMainRequestRule();
15+
}
16+
17+
public function testRule(): void
18+
{
19+
[$version] = explode('.', \Drupal::VERSION, 2);
20+
if ($version === '8') {
21+
$this->analyse([__DIR__.'/data/request-stack.php'], []);
22+
} elseif ($version === '10') {
23+
self::markTestSkipped('Not tested on 10.x.x');
24+
} else {
25+
$this->analyse(
26+
[__DIR__.'/data/request-stack.php'],
27+
[
28+
[
29+
'Symfony\Component\HttpFoundation\RequestStack::getMasterRequest() is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0 for Symfony 6 compatibility. Use the forward compatibility shim class Drupal\Core\Http\RequestStack and its getMainRequest() method instead.',
30+
17,
31+
'Change record: https://www.drupal.org/node/3253744',
32+
],
33+
[
34+
'Symfony\Component\HttpFoundation\RequestStack::getMasterRequest() is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0 for Symfony 6 compatibility. Use the forward compatibility shim class Drupal\Core\Http\RequestStack and its getMainRequest() method instead.',
35+
29,
36+
'Change record: https://www.drupal.org/node/3253744',
37+
],
38+
]
39+
);
40+
}
41+
}
42+
43+
}

tests/src/Rules/data/request-stack.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace DrupalRequestStackShim;
44

5+
use Symfony\Component\HttpFoundation\Request;
6+
57
class Foo {
68
public function __construct(\Symfony\Component\HttpFoundation\RequestStack $stack)
79
{
@@ -10,6 +12,10 @@ public function getStack(): \Symfony\Component\HttpFoundation\RequestStack
1012
{
1113
return new \Symfony\Component\HttpFoundation\RequestStack();
1214
}
15+
16+
public function a(): ?Request {
17+
return $this->getStack()->getMasterRequest();
18+
}
1319
}
1420
class Bar {
1521
public function __construct(\Drupal\Core\Http\RequestStack $stack)
@@ -19,4 +25,10 @@ public function getStack(): \Drupal\Core\Http\RequestStack
1925
{
2026
return new \Drupal\Core\Http\RequestStack();
2127
}
28+
public function a(): ?Request {
29+
return $this->getStack()->getMasterRequest();
30+
}
31+
public function b(): ?Request {
32+
return $this->getStack()->getMainRequest();
33+
}
2234
}

0 commit comments

Comments
 (0)