Skip to content

Commit

Permalink
Add deprecated scope resolving
Browse files Browse the repository at this point in the history
Adds the ability to register deprecated scope resolvers for controlling
the deprecated scope.
  • Loading branch information
mglaman authored and ondrejmirtes committed Aug 5, 2023
1 parent 94d68d3 commit 6d416c7
Show file tree
Hide file tree
Showing 37 changed files with 337 additions and 59 deletions.
15 changes: 13 additions & 2 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@ parameters:
deprecationRulesInstalled: true

services:
-
class: PHPStan\Rules\Deprecations\DeprecatedClassHelper
-
class: PHPStan\Rules\Deprecations\DeprecatedClassHelper

-
class: PHPStan\DependencyInjection\LazyDeprecatedScopeResolverProvider
-
class: PHPStan\Rules\Deprecations\DeprecatedScopeHelper
factory: @PHPStan\DependencyInjection\LazyDeprecatedScopeResolverProvider::get

-
class: PHPStan\Rules\Deprecations\DefaultDeprecatedScopeResolver
tags:
- phpstan.deprecations.deprecatedScopeResolver

rules:
- PHPStan\Rules\Deprecations\AccessDeprecatedPropertyRule
Expand Down
33 changes: 33 additions & 0 deletions src/DependencyInjection/LazyDeprecatedScopeResolverProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types = 1);

namespace PHPStan\DependencyInjection;

use PHPStan\Rules\Deprecations\DeprecatedScopeHelper;

final class LazyDeprecatedScopeResolverProvider
{

public const EXTENSION_TAG = 'phpstan.deprecations.deprecatedScopeResolver';

/** @var Container */
private $container;

/** @var DeprecatedScopeHelper */
private $scopeHelper;

public function __construct(Container $container)
{
$this->container = $container;
}

public function get(): DeprecatedScopeHelper
{
if ($this->scopeHelper === null) {
$this->scopeHelper = new DeprecatedScopeHelper(
$this->container->getServicesByTag(self::EXTENSION_TAG)
);
}
return $this->scopeHelper;
}

}
8 changes: 6 additions & 2 deletions src/Rules/Deprecations/AccessDeprecatedPropertyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ class AccessDeprecatedPropertyRule implements Rule
/** @var ReflectionProvider */
private $reflectionProvider;

public function __construct(ReflectionProvider $reflectionProvider)
/** @var DeprecatedScopeHelper */
private $deprecatedScopeHelper;

public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper)
{
$this->reflectionProvider = $reflectionProvider;
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
}

public function getNodeType(): string
Expand All @@ -33,7 +37,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
return [];
}

Expand Down
8 changes: 6 additions & 2 deletions src/Rules/Deprecations/AccessDeprecatedStaticPropertyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ class AccessDeprecatedStaticPropertyRule implements Rule
/** @var RuleLevelHelper */
private $ruleLevelHelper;

public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper)
/** @var DeprecatedScopeHelper */
private $deprecatedScopeHelper;

public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper, DeprecatedScopeHelper $deprecatedScopeHelper)
{
$this->reflectionProvider = $reflectionProvider;
$this->ruleLevelHelper = $ruleLevelHelper;
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
}

public function getNodeType(): string
Expand All @@ -41,7 +45,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
return [];
}

Expand Down
8 changes: 6 additions & 2 deletions src/Rules/Deprecations/CallToDeprecatedFunctionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ class CallToDeprecatedFunctionRule implements Rule
/** @var ReflectionProvider */
private $reflectionProvider;

public function __construct(ReflectionProvider $reflectionProvider)
/** @var DeprecatedScopeHelper */
private $deprecatedScopeHelper;

public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper)
{
$this->reflectionProvider = $reflectionProvider;
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
}

public function getNodeType(): string
Expand All @@ -32,7 +36,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
return [];
}

Expand Down
8 changes: 6 additions & 2 deletions src/Rules/Deprecations/CallToDeprecatedMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ class CallToDeprecatedMethodRule implements Rule
/** @var ReflectionProvider */
private $reflectionProvider;

public function __construct(ReflectionProvider $reflectionProvider)
/** @var DeprecatedScopeHelper */
private $deprecatedScopeHelper;

public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper)
{
$this->reflectionProvider = $reflectionProvider;
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
}

public function getNodeType(): string
Expand All @@ -33,7 +37,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
return [];
}

Expand Down
8 changes: 6 additions & 2 deletions src/Rules/Deprecations/CallToDeprecatedStaticMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ class CallToDeprecatedStaticMethodRule implements Rule
/** @var RuleLevelHelper */
private $ruleLevelHelper;

public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper)
/** @var DeprecatedScopeHelper */
private $deprecatedScopeHelper;

public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper, DeprecatedScopeHelper $deprecatedScopeHelper)
{
$this->reflectionProvider = $reflectionProvider;
$this->ruleLevelHelper = $ruleLevelHelper;
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
}

public function getNodeType(): string
Expand All @@ -41,7 +45,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
return [];
}

Expand Down
30 changes: 30 additions & 0 deletions src/Rules/Deprecations/DefaultDeprecatedScopeResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Deprecations;

use PHPStan\Analyser\Scope;

final class DefaultDeprecatedScopeResolver implements DeprecatedScopeResolver
{

public function isScopeDeprecated(Scope $scope): bool
{
$class = $scope->getClassReflection();
if ($class !== null && $class->isDeprecated()) {
return true;
}

$trait = $scope->getTraitReflection();
if ($trait !== null && $trait->isDeprecated()) {
return true;
}

$function = $scope->getFunction();
if ($function !== null && $function->isDeprecated()->yes()) {
return true;
}

return false;
}

}
28 changes: 15 additions & 13 deletions src/Rules/Deprecations/DeprecatedScopeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@
class DeprecatedScopeHelper
{

public static function isScopeDeprecated(Scope $scope): bool
{
$class = $scope->getClassReflection();
if ($class !== null && $class->isDeprecated()) {
return true;
}
/** @var DeprecatedScopeResolver[] */
private $resolvers;

$trait = $scope->getTraitReflection();
if ($trait !== null && $trait->isDeprecated()) {
return true;
}
/**
* @param DeprecatedScopeResolver[] $checkers
*/
public function __construct(array $checkers)
{
$this->resolvers = $checkers;
}

$function = $scope->getFunction();
if ($function !== null && $function->isDeprecated()->yes()) {
return true;
public function isScopeDeprecated(Scope $scope): bool
{
foreach ($this->resolvers as $checker) {
if ($checker->isScopeDeprecated($scope)) {
return true;
}
}

return false;
Expand Down
12 changes: 12 additions & 0 deletions src/Rules/Deprecations/DeprecatedScopeResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Deprecations;

use PHPStan\Analyser\Scope;

interface DeprecatedScopeResolver
{

public function isScopeDeprecated(Scope $scope): bool;

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ class FetchingClassConstOfDeprecatedClassRule implements Rule
/** @var RuleLevelHelper */
private $ruleLevelHelper;

public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper)
/** @var DeprecatedScopeHelper */
private $deprecatedScopeHelper;

public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper, DeprecatedScopeHelper $deprecatedScopeHelper)
{
$this->reflectionProvider = $reflectionProvider;
$this->ruleLevelHelper = $ruleLevelHelper;
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
}

public function getNodeType(): string
Expand All @@ -41,7 +45,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
return [];
}

Expand Down
8 changes: 6 additions & 2 deletions src/Rules/Deprecations/FetchingDeprecatedConstRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ class FetchingDeprecatedConstRule implements Rule
/** @var ReflectionProvider */
private $reflectionProvider;

/** @var DeprecatedScopeHelper */
private $deprecatedScopeHelper;

/** @var array<string,string> */
private $deprecatedConstants = [];

public function __construct(ReflectionProvider $reflectionProvider)
public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper)
{
$this->reflectionProvider = $reflectionProvider;
$this->deprecatedScopeHelper = $deprecatedScopeHelper;

// phpcs:ignore SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed
if (PHP_VERSION_ID >= 70300) {
Expand All @@ -40,7 +44,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
return [];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ class ImplementationOfDeprecatedInterfaceRule implements Rule
/** @var ReflectionProvider */
private $reflectionProvider;

public function __construct(ReflectionProvider $reflectionProvider)
/** @var DeprecatedScopeHelper */
private $deprecatedScopeHelper;

public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper)
{
$this->reflectionProvider = $reflectionProvider;
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
}

public function getNodeType(): string
Expand All @@ -31,7 +35,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
return [];
}

Expand Down
8 changes: 6 additions & 2 deletions src/Rules/Deprecations/InheritanceOfDeprecatedClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ class InheritanceOfDeprecatedClassRule implements Rule
/** @var ReflectionProvider */
private $reflectionProvider;

public function __construct(ReflectionProvider $reflectionProvider)
/** @var DeprecatedScopeHelper */
private $deprecatedScopeHelper;

public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper)
{
$this->reflectionProvider = $reflectionProvider;
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
}

public function getNodeType(): string
Expand All @@ -31,7 +35,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
return [];
}

Expand Down
8 changes: 6 additions & 2 deletions src/Rules/Deprecations/InstantiationOfDeprecatedClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ class InstantiationOfDeprecatedClassRule implements Rule
/** @var RuleLevelHelper */
private $ruleLevelHelper;

public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper)
/** @var DeprecatedScopeHelper */
private $deprecatedScopeHelper;

public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper, DeprecatedScopeHelper $deprecatedScopeHelper)
{
$this->reflectionProvider = $reflectionProvider;
$this->ruleLevelHelper = $ruleLevelHelper;
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
}

public function getNodeType(): string
Expand All @@ -39,7 +43,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
return [];
}

Expand Down

0 comments on commit 6d416c7

Please sign in to comment.