|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Deprecations; |
| 4 | + |
| 5 | +use PHPStan\Analyser\Scope; |
| 6 | +use PHPStan\Reflection\ExtendedPropertyReflection; |
| 7 | +use PHPStan\Rules\RestrictedUsage\RestrictedPropertyUsageExtension; |
| 8 | +use PHPStan\Rules\RestrictedUsage\RestrictedUsage; |
| 9 | +use function sprintf; |
| 10 | +use function strtolower; |
| 11 | + |
| 12 | +class RestrictedDeprecatedPropertyUsageExtension implements RestrictedPropertyUsageExtension |
| 13 | +{ |
| 14 | + |
| 15 | + private DeprecatedScopeHelper $deprecatedScopeHelper; |
| 16 | + |
| 17 | + public function __construct(DeprecatedScopeHelper $deprecatedScopeHelper) |
| 18 | + { |
| 19 | + $this->deprecatedScopeHelper = $deprecatedScopeHelper; |
| 20 | + } |
| 21 | + |
| 22 | + public function isRestrictedPropertyUsage( |
| 23 | + ExtendedPropertyReflection $propertyReflection, |
| 24 | + Scope $scope |
| 25 | + ): ?RestrictedUsage |
| 26 | + { |
| 27 | + if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { |
| 28 | + return null; |
| 29 | + } |
| 30 | + |
| 31 | + if ($propertyReflection->getDeclaringClass()->isDeprecated()) { |
| 32 | + $class = $propertyReflection->getDeclaringClass(); |
| 33 | + $classDescription = $class->getDeprecatedDescription(); |
| 34 | + if ($classDescription === null) { |
| 35 | + return RestrictedUsage::create( |
| 36 | + sprintf( |
| 37 | + 'Access to %sproperty $%s of deprecated %s %s.', |
| 38 | + $propertyReflection->isStatic() ? 'static ' : '', |
| 39 | + $propertyReflection->getName(), |
| 40 | + strtolower($propertyReflection->getDeclaringClass()->getClassTypeDescription()), |
| 41 | + $propertyReflection->getDeclaringClass()->getName(), |
| 42 | + ), |
| 43 | + sprintf( |
| 44 | + '%s.deprecated%s', |
| 45 | + $propertyReflection->isStatic() ? 'staticProperty' : 'property', |
| 46 | + $propertyReflection->getDeclaringClass()->getClassTypeDescription(), |
| 47 | + ), |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + return RestrictedUsage::create( |
| 52 | + sprintf( |
| 53 | + "Access to %sproperty $%s of deprecated %s %s:\n%s", |
| 54 | + $propertyReflection->isStatic() ? 'static ' : '', |
| 55 | + $propertyReflection->getName(), |
| 56 | + strtolower($propertyReflection->getDeclaringClass()->getClassTypeDescription()), |
| 57 | + $propertyReflection->getDeclaringClass()->getName(), |
| 58 | + $classDescription, |
| 59 | + ), |
| 60 | + sprintf( |
| 61 | + '%s.deprecated%s', |
| 62 | + $propertyReflection->isStatic() ? 'staticProperty' : 'property', |
| 63 | + $propertyReflection->getDeclaringClass()->getClassTypeDescription(), |
| 64 | + ), |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + if (!$propertyReflection->isDeprecated()->yes()) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + $description = $propertyReflection->getDeprecatedDescription(); |
| 73 | + if ($description === null) { |
| 74 | + return RestrictedUsage::create( |
| 75 | + sprintf( |
| 76 | + 'Access to deprecated %sproperty $%s of %s %s.', |
| 77 | + $propertyReflection->isStatic() ? 'static ' : '', |
| 78 | + $propertyReflection->getName(), |
| 79 | + strtolower($propertyReflection->getDeclaringClass()->getClassTypeDescription()), |
| 80 | + $propertyReflection->getDeclaringClass()->getName(), |
| 81 | + ), |
| 82 | + sprintf('%s.deprecated', $propertyReflection->isStatic() ? 'staticProperty' : 'property'), |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + return RestrictedUsage::create( |
| 87 | + sprintf( |
| 88 | + "Access to deprecated %sproperty $%s of %s %s:\n%s", |
| 89 | + $propertyReflection->isStatic() ? 'static ' : '', |
| 90 | + $propertyReflection->getName(), |
| 91 | + strtolower($propertyReflection->getDeclaringClass()->getClassTypeDescription()), |
| 92 | + $propertyReflection->getDeclaringClass()->getName(), |
| 93 | + $description, |
| 94 | + ), |
| 95 | + sprintf('%s.deprecated', $propertyReflection->isStatic() ? 'staticProperty' : 'property'), |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments