Skip to content

Commit d721420

Browse files
authored
Add @group legacy deprecated scope resolver (#595)
* Add `@group legacy` deprecated scope resolver * Ignore DeprecatedScope test data * Try and workaround adding deprecation rules as dependency * fix phpcs * Make phpstan/phpstan-deprecation-rules a direct dependency * Update README docs
1 parent 73dab31 commit d721420

File tree

8 files changed

+129
-14
lines changed

8 files changed

+129
-14
lines changed

README.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,8 @@ parameters:
4949

5050
## Deprecation testing
5151

52-
Add the deprecation rules to your Drupal project's dependencies
53-
54-
```
55-
composer require --dev phpstan/phpstan-deprecation-rules
56-
```
57-
58-
Edit your `phpstan.neon` to look like the following:
59-
60-
```
61-
includes:
62-
- vendor/mglaman/phpstan-drupal/extension.neon
63-
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
64-
```
52+
This project depends on `phpstan/phpstan-deprecation-rules` which adds deprecation rules. We provide Drupal-specific
53+
deprecated scope resolvers.
6554

6655
To only handle deprecation testing, use a `phpstan.neon` like this:
6756

@@ -80,6 +69,22 @@ includes:
8069
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
8170
```
8271

72+
To disable deprecation rules while using `phpstan/extension-installer`, you can do the following:
73+
74+
```json
75+
{
76+
"extra": {
77+
"phpstan/extension-installer": {
78+
"ignore": [
79+
"phpstan/phpstan-deprecation-rules"
80+
]
81+
}
82+
}
83+
}
84+
```
85+
86+
See the `extension-installer` documentation for more information: https://github.com/phpstan/extension-installer#ignoring-a-particular-extension
87+
8388
## Adapting to your project
8489

8590
### Specifying your Drupal project's root

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"php": "^7.4 || ^8.0",
1414
"symfony/finder": "~3.4.5 ||^4.2 || ^5.0 || ^6.0",
1515
"phpstan/phpstan": "^1.10.1",
16+
"phpstan/phpstan-deprecation-rules": "^1.1.4",
1617
"symfony/yaml": "~3.4.5 || ^4.2|| ^5.0 || ^6.0",
1718
"webflo/drupal-finder": "^1.2"
1819
},
@@ -22,7 +23,6 @@
2223
"drupal/core-recommended": "^8.8@alpha || ^9.0",
2324
"drush/drush": "^9.6 || ^10.0 || ^11",
2425
"phpstan/extension-installer": "^1.1",
25-
"phpstan/phpstan-deprecation-rules": "^1.0",
2626
"phpstan/phpstan-strict-rules": "^1.0",
2727
"phpunit/phpunit": "^6.5 || ^7.5 || ^8.0 || ^9",
2828
"slevomat/coding-standard": "^7.1",

extension.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,7 @@ services:
298298
class: mglaman\PHPStanDrupal\Type\EntityQuery\AccessCheckTypeSpecifyingExtension
299299
tags:
300300
- phpstan.typeSpecifier.methodTypeSpecifyingExtension
301+
-
302+
class: mglaman\PHPStanDrupal\DeprecatedScope\GroupLegacyScope
303+
tags:
304+
- phpstan.deprecations.deprecatedScopeResolver

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ parameters:
1111
- tests/src/data/*.php
1212
- tests/src/Type/data/*.php
1313
- tests/src/Rules/data/*.php
14+
- tests/src/DeprecatedScope/data/*.php
1415
dynamicConstantNames:
1516
- Drupal::VERSION
1617
includes:
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace mglaman\PHPStanDrupal\DeprecatedScope;
6+
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\Rules\Deprecations\DeprecatedScopeResolver;
9+
10+
final class GroupLegacyScope implements DeprecatedScopeResolver
11+
{
12+
13+
public function isScopeDeprecated(Scope $scope): bool
14+
{
15+
if ($scope->isInClass()) {
16+
$class = $scope->getClassReflection();
17+
$phpDoc = $class->getResolvedPhpDoc();
18+
if ($phpDoc !== null && strpos($phpDoc->getPhpDocString(), '@group legacy') !== false) {
19+
return true;
20+
}
21+
}
22+
23+
$function = $scope->getFunction();
24+
return $function !== null
25+
&& $function->getDocComment() !== null
26+
&& strpos($function->getDocComment(), '@group legacy') !== false;
27+
}
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace mglaman\PHPStanDrupal\Tests\DeprecatedScope;
6+
7+
use mglaman\PHPStanDrupal\Tests\DrupalRuleTestCase;
8+
use PHPStan\Rules\Deprecations\CallToDeprecatedFunctionRule;
9+
use PHPStan\Rules\Deprecations\DeprecatedScopeHelper;
10+
use PHPStan\Rules\Rule;
11+
12+
final class GlobalLegacyScopeTest extends DrupalRuleTestCase {
13+
14+
protected function getRule(): Rule
15+
{
16+
// @phpstan-ignore-next-line
17+
return new CallToDeprecatedFunctionRule(
18+
self::createReflectionProvider(),
19+
self::getContainer()->getByType(DeprecatedScopeHelper::class)
20+
);
21+
}
22+
23+
public function testCustomScope(): void
24+
{
25+
require_once __DIR__ . '/data/deprecated-data-definition.php';
26+
$this->analyse(
27+
[__DIR__ . '/data/group-legacy.php'],
28+
[
29+
[
30+
'Call to deprecated function Deprecated\deprecated_function().',
31+
21,
32+
],
33+
]
34+
);
35+
}
36+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Deprecated;
4+
5+
/**
6+
* @deprecated
7+
*/
8+
function deprecated_function(): void {
9+
10+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace GroupLegacy;
4+
5+
use function Deprecated\deprecated_function;
6+
7+
/**
8+
* @group legacy
9+
*/
10+
final class FooTest {
11+
12+
public function foo(): void {
13+
deprecated_function();
14+
}
15+
16+
}
17+
18+
final class BarTest {
19+
20+
public function bar(): void {
21+
deprecated_function();
22+
}
23+
24+
/**
25+
* @group legacy
26+
*/
27+
public function barNot(): void {
28+
deprecated_function();
29+
}
30+
31+
}

0 commit comments

Comments
 (0)