|
| 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 | +} |
0 commit comments