Skip to content

Commit 089392a

Browse files
authored
Merge pull request #69 from mglaman/deprecation-global-constants
Detection of deprecated constants
2 parents 5feaf70 + ec592ec commit 089392a

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\Reflection\DeprecatableReflection;
9+
10+
class AccessDeprecatedConstant implements \PHPStan\Rules\Rule
11+
{
12+
/** @var Broker */
13+
private $broker;
14+
public function __construct(Broker $broker)
15+
{
16+
$this->broker = $broker;
17+
}
18+
19+
public function getNodeType(): string
20+
{
21+
return Node\Expr\ConstFetch::class;
22+
}
23+
24+
public function processNode(Node $node, Scope $scope): array
25+
{
26+
assert($node instanceof Node\Expr\ConstFetch);
27+
$class = $scope->getClassReflection();
28+
if ($class !== null && $class->isDeprecated()) {
29+
return [];
30+
}
31+
$trait = $scope->getTraitReflection();
32+
if ($trait !== null && $trait->isDeprecated()) {
33+
return [];
34+
}
35+
$function = $scope->getFunction();
36+
if ($function instanceof DeprecatableReflection && $function->isDeprecated()) {
37+
return [];
38+
}
39+
40+
// nikic/php-parser does not have any comments above the comment.
41+
// possibly due to PHP's internal reflection capabilities?
42+
$deprecatedConstants = [
43+
'DATETIME_STORAGE_TIMEZONE' => 'Deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.x. Use \Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface::STORAGE_TIMEZONE instead.',
44+
'DATETIME_DATETIME_STORAGE_FORMAT' => 'Deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.x. Use \Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface::DATETIME_STORAGE_FORMAT instead.',
45+
'DATETIME_DATE_STORAGE_FORMAT' => 'Deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.x. Use \Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface::DATE_STORAGE_FORMAT instead.',
46+
];
47+
$constantName = $this->broker->resolveConstantName($node->name, $scope);
48+
if (isset($deprecatedConstants[$constantName])) {
49+
return [
50+
sprintf('Call to deprecated constant %s: %s', $constantName, $deprecatedConstants[$constantName])
51+
];
52+
}
53+
return [];
54+
}
55+
56+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Drupal\phpstan_fixtures;
4+
5+
class DeprecatedGlobalConstants {
6+
public function test() {
7+
$date = new \DateTime();
8+
$date->setTimezone(new \DateTimeZone(DATETIME_STORAGE_TIMEZONE));
9+
$date->format(DATETIME_DATE_STORAGE_FORMAT);
10+
}
11+
}

tests/src/DeprecationRulesTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,13 @@ public function dataDeprecatedSamples(): \Generator
3838
in drupal:8.0.0 and is removed from drupal:9.0.0.'
3939
]
4040
];
41+
yield [
42+
__DIR__ . '/../fixtures/drupal/modules/phpstan_fixtures/src/DeprecatedGlobalConstants.php',
43+
2,
44+
[
45+
'Call to deprecated constant DATETIME_STORAGE_TIMEZONE: Deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.x. Use \Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface::STORAGE_TIMEZONE instead.',
46+
'Call to deprecated constant DATETIME_DATE_STORAGE_FORMAT: Deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.x. Use \Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface::DATE_STORAGE_FORMAT instead.',
47+
]
48+
];
4149
}
4250
}

0 commit comments

Comments
 (0)