Skip to content

Commit

Permalink
Resolve #[IgnoreDeprecations] to deprecated scope (#779)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
mondrake and mglaman authored Aug 7, 2024
1 parent ca61fd6 commit 346bddd
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jobs:
php-version:
- "8.1"
- "8.2"
- "8.3"
drupal:
- "^10"
include:
Expand Down
4 changes: 4 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ services:
class: mglaman\PHPStanDrupal\DeprecatedScope\GroupLegacyScope
tags:
- phpstan.deprecations.deprecatedScopeResolver
-
class: mglaman\PHPStanDrupal\DeprecatedScope\IgnoreDeprecationsScope
tags:
- phpstan.deprecations.deprecatedScopeResolver
-
class: mglaman\PHPStanDrupal\DeprecatedScope\DeprecationHelperScope
tags:
Expand Down
38 changes: 38 additions & 0 deletions src/DeprecatedScope/IgnoreDeprecationsScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace mglaman\PHPStanDrupal\DeprecatedScope;

use PHPStan\Analyser\Scope;
use PHPStan\Rules\Deprecations\DeprecatedScopeResolver;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;

final class IgnoreDeprecationsScope implements DeprecatedScopeResolver
{

public function isScopeDeprecated(Scope $scope): bool
{
if (!class_exists(IgnoreDeprecations::class)) {
return false;
}

if ($scope->isInClass()) {
$class = $scope->getClassReflection()->getNativeReflection();
if ($class->getAttributes(IgnoreDeprecations::class) !== []) {
return true;
}

$function = $scope->getFunction();
if ($function === null) {
return false;
}

$method = $class->getMethod($function->getName());
if ($method->getAttributes(IgnoreDeprecations::class) !== []) {
return true;
}
}
return false;
}
}
55 changes: 55 additions & 0 deletions tests/src/DeprecatedScope/IgnoreDeprecationsScopeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace mglaman\PHPStanDrupal\Tests\DeprecatedScope;

use mglaman\PHPStanDrupal\Tests\DrupalRuleTestCase;
use PHPStan\Rules\Deprecations\CallToDeprecatedFunctionRule;
use PHPStan\Rules\Deprecations\DeprecatedScopeHelper;
use PHPStan\Rules\Rule;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;

final class IgnoreDeprecationsScopeTest extends DrupalRuleTestCase {

protected function getRule(): Rule
{
// @phpstan-ignore-next-line
return new CallToDeprecatedFunctionRule(
self::createReflectionProvider(),
self::getContainer()->getByType(DeprecatedScopeHelper::class)
);
}

public function testCustomScope(): void
{
if (!class_exists(IgnoreDeprecations::class)) {
$errors = [
[
'Call to deprecated function Deprecated\deprecated_function().',
12,
],
[
'Call to deprecated function Deprecated\deprecated_function().',
20,
],
[
'Call to deprecated function Deprecated\deprecated_function().',
25,
],
];
} else {
$errors = [
[
'Call to deprecated function Deprecated\deprecated_function().',
20,
],
];
}
require_once __DIR__ . '/data/deprecated-data-definition.php';
$this->analyse(
[__DIR__ . '/data/ignore-deprecations.php'],
$errors
);
}
}
28 changes: 28 additions & 0 deletions tests/src/DeprecatedScope/data/ignore-deprecations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace IgnoreDeprecations;

use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use function Deprecated\deprecated_function;

#[IgnoreDeprecations]
final class FooTest {

public function foo(): void {
deprecated_function();
}

}

final class BarTest {

public function bar(): void {
deprecated_function();
}

#[IgnoreDeprecations]
public function barNot(): void {
deprecated_function();
}

}

0 comments on commit 346bddd

Please sign in to comment.