Skip to content

Commit 346bddd

Browse files
mondrakemglaman
andauthored
Resolve #[IgnoreDeprecations] to deprecated scope (#779)
* Tests * resolver * fix * fix * fix * set linting to D11/PHP8.3 * fix * fix * change composer to D11 only * fix * fix * fix * more * fix phpstan-dev ci * fix phpunit.xml * fix * Update phpstan-dev.yml * Revert "fix phpunit.xml" This reverts commit 93eed59. * Revert "more" This reverts commit cf6693f. * revert changes to workflows and composer.json * use class_exists for checking attribute class * add conditional logic in test assertion --------- Co-authored-by: Matt Glaman <[email protected]>
1 parent ca61fd6 commit 346bddd

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

.github/workflows/php.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ jobs:
4343
php-version:
4444
- "8.1"
4545
- "8.2"
46+
- "8.3"
4647
drupal:
4748
- "^10"
4849
include:

extension.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ services:
325325
class: mglaman\PHPStanDrupal\DeprecatedScope\GroupLegacyScope
326326
tags:
327327
- phpstan.deprecations.deprecatedScopeResolver
328+
-
329+
class: mglaman\PHPStanDrupal\DeprecatedScope\IgnoreDeprecationsScope
330+
tags:
331+
- phpstan.deprecations.deprecatedScopeResolver
328332
-
329333
class: mglaman\PHPStanDrupal\DeprecatedScope\DeprecationHelperScope
330334
tags:
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
10+
11+
final class IgnoreDeprecationsScope implements DeprecatedScopeResolver
12+
{
13+
14+
public function isScopeDeprecated(Scope $scope): bool
15+
{
16+
if (!class_exists(IgnoreDeprecations::class)) {
17+
return false;
18+
}
19+
20+
if ($scope->isInClass()) {
21+
$class = $scope->getClassReflection()->getNativeReflection();
22+
if ($class->getAttributes(IgnoreDeprecations::class) !== []) {
23+
return true;
24+
}
25+
26+
$function = $scope->getFunction();
27+
if ($function === null) {
28+
return false;
29+
}
30+
31+
$method = $class->getMethod($function->getName());
32+
if ($method->getAttributes(IgnoreDeprecations::class) !== []) {
33+
return true;
34+
}
35+
}
36+
return false;
37+
}
38+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
12+
13+
final class IgnoreDeprecationsScopeTest extends DrupalRuleTestCase {
14+
15+
protected function getRule(): Rule
16+
{
17+
// @phpstan-ignore-next-line
18+
return new CallToDeprecatedFunctionRule(
19+
self::createReflectionProvider(),
20+
self::getContainer()->getByType(DeprecatedScopeHelper::class)
21+
);
22+
}
23+
24+
public function testCustomScope(): void
25+
{
26+
if (!class_exists(IgnoreDeprecations::class)) {
27+
$errors = [
28+
[
29+
'Call to deprecated function Deprecated\deprecated_function().',
30+
12,
31+
],
32+
[
33+
'Call to deprecated function Deprecated\deprecated_function().',
34+
20,
35+
],
36+
[
37+
'Call to deprecated function Deprecated\deprecated_function().',
38+
25,
39+
],
40+
];
41+
} else {
42+
$errors = [
43+
[
44+
'Call to deprecated function Deprecated\deprecated_function().',
45+
20,
46+
],
47+
];
48+
}
49+
require_once __DIR__ . '/data/deprecated-data-definition.php';
50+
$this->analyse(
51+
[__DIR__ . '/data/ignore-deprecations.php'],
52+
$errors
53+
);
54+
}
55+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace IgnoreDeprecations;
4+
5+
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
6+
use function Deprecated\deprecated_function;
7+
8+
#[IgnoreDeprecations]
9+
final class FooTest {
10+
11+
public function foo(): void {
12+
deprecated_function();
13+
}
14+
15+
}
16+
17+
final class BarTest {
18+
19+
public function bar(): void {
20+
deprecated_function();
21+
}
22+
23+
#[IgnoreDeprecations]
24+
public function barNot(): void {
25+
deprecated_function();
26+
}
27+
28+
}

0 commit comments

Comments
 (0)