Skip to content

Commit ec592ec

Browse files
committed
Rebase and manually check
1 parent f8e6ecc commit ec592ec

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

src/Rules/Deprecations/AccessDeprecatedConstant.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@
55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Broker\Broker;
8-
use PHPStan\Rules\RuleLevelHelper;
8+
use PHPStan\Reflection\DeprecatableReflection;
99

1010
class AccessDeprecatedConstant implements \PHPStan\Rules\Rule
1111
{
1212
/** @var Broker */
1313
private $broker;
14-
/** @var RuleLevelHelper */
15-
private $ruleLevelHelper;
16-
public function __construct(Broker $broker, RuleLevelHelper $ruleLevelHelper)
14+
public function __construct(Broker $broker)
1715
{
1816
$this->broker = $broker;
19-
$this->ruleLevelHelper = $ruleLevelHelper;
2017
}
2118

2219
public function getNodeType(): string
@@ -26,13 +23,33 @@ public function getNodeType(): string
2623

2724
public function processNode(Node $node, Scope $scope): array
2825
{
29-
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
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()) {
3037
return [];
3138
}
3239

33-
// comment is not resolved.
34-
$comment = $node->getDocComment();
35-
$constnatName = $this->broker->resolveConstantName($node->name, $scope);
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+
}
3653
return [];
3754
}
3855

tests/src/DeprecationRulesTest.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,6 @@ 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-
// ];
3722
yield [
3823
__DIR__ . '/../fixtures/drupal/modules/phpstan_fixtures/src/UsesDeprecatedUrlFunction.php',
3924
2,
@@ -57,8 +42,8 @@ public function dataDeprecatedSamples(): \Generator
5742
__DIR__ . '/../fixtures/drupal/modules/phpstan_fixtures/src/DeprecatedGlobalConstants.php',
5843
2,
5944
[
60-
'Call to deprecated constant DATETIME_STORAGE_TIMEZONE.',
61-
'Call to deprecated constant DATETIME_DATE_STORAGE_FORMAT.',
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.',
6247
]
6348
];
6449
}

0 commit comments

Comments
 (0)