Skip to content

Commit f8e6ecc

Browse files
committed
Attempt to discover deprecations on constants
1 parent 740944e commit f8e6ecc

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

extension.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ rules:
2121
- PHPStan\Rules\Drupal\Coder\DiscouragedFunctionsRule
2222
- PHPStan\Rules\Drupal\GlobalDrupalDependencyInjectionRule
2323
- PHPStan\Rules\Drupal\PluginManager\PluginManagerSetsCacheBackendRule
24+
- PHPStan\Rules\Deprecations\AccessDeprecatedConstant
2425
services:
2526
drupal.serviceMapFactory:
2627
class: PHPStan\Drupal\ServiceMapFactory
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Deprecations;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Broker\Broker;
8+
use PHPStan\Rules\RuleLevelHelper;
9+
10+
class AccessDeprecatedConstant implements \PHPStan\Rules\Rule
11+
{
12+
/** @var Broker */
13+
private $broker;
14+
/** @var RuleLevelHelper */
15+
private $ruleLevelHelper;
16+
public function __construct(Broker $broker, RuleLevelHelper $ruleLevelHelper)
17+
{
18+
$this->broker = $broker;
19+
$this->ruleLevelHelper = $ruleLevelHelper;
20+
}
21+
22+
public function getNodeType(): string
23+
{
24+
return Node\Expr\ConstFetch::class;
25+
}
26+
27+
public function processNode(Node $node, Scope $scope): array
28+
{
29+
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
30+
return [];
31+
}
32+
33+
// comment is not resolved.
34+
$comment = $node->getDocComment();
35+
$constnatName = $this->broker->resolveConstantName($node->name, $scope);
36+
return [];
37+
}
38+
39+
}

tests/src/DeprecationRulesTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ public function testDeprecationRules(string $path, int $count, array $errorMessa
1919

2020
public function dataDeprecatedSamples(): \Generator
2121
{
22+
// yield [
23+
// __DIR__ . '/../fixtures/drupal/modules/phpstan_fixtures/src/UsesDeprecatedUrlFunction.php',
24+
// 2,
25+
// [
26+
// '\Drupal calls should be avoided in classes, use dependency injection instead',
27+
// 'Call to deprecated method url() of class Drupal.'
28+
// ]
29+
// ];
30+
// yield [
31+
// __DIR__ . '/../fixtures/drupal/core/lib/Drupal/Core/Entity/EntityManager.php',
32+
// 1,
33+
// [
34+
// 'Class Drupal\Core\Entity\EntityManager implements deprecated interface Drupal\Core\Entity\EntityManagerInterface.'
35+
// ]
36+
// ];
2237
yield [
2338
__DIR__ . '/../fixtures/drupal/modules/phpstan_fixtures/src/UsesDeprecatedUrlFunction.php',
2439
2,

0 commit comments

Comments
 (0)