-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
Showing
5 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ jobs: | |
php-version: | ||
- "8.1" | ||
- "8.2" | ||
- "8.3" | ||
drupal: | ||
- "^10" | ||
include: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |