Skip to content

Add deprecated scope resolving #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -23,9 +23,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 @@ -35,7 +39,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 @@ -30,10 +30,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 @@ -43,7 +47,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 @@ -21,9 +21,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 @@ -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/CallToDeprecatedMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,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 @@ -35,7 +39,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 @@ -30,10 +30,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 @@ -43,7 +47,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 @@ -29,10 +29,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 @@ -42,7 +46,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 @@ -20,12 +20,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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,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 @@ -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/InheritanceOfDeprecatedClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,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 @@ -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/InstantiationOfDeprecatedClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,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 @@ -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
Loading