|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\InternalTag; |
| 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 array_slice; |
| 10 | +use function explode; |
| 11 | +use function sprintf; |
| 12 | +use function strtolower; |
| 13 | + |
| 14 | +final class RestrictedInternalPropertyUsageExtension implements RestrictedPropertyUsageExtension |
| 15 | +{ |
| 16 | + |
| 17 | + public function __construct(private RestrictedInternalUsageHelper $helper) |
| 18 | + { |
| 19 | + } |
| 20 | + |
| 21 | + public function isRestrictedPropertyUsage( |
| 22 | + ExtendedPropertyReflection $propertyReflection, |
| 23 | + Scope $scope, |
| 24 | + ): ?RestrictedUsage |
| 25 | + { |
| 26 | + $isPropertyInternal = $propertyReflection->isInternal()->yes(); |
| 27 | + $declaringClass = $propertyReflection->getDeclaringClass(); |
| 28 | + $isDeclaringClassInternal = $declaringClass->isInternal(); |
| 29 | + if (!$isPropertyInternal && !$isDeclaringClassInternal) { |
| 30 | + return null; |
| 31 | + } |
| 32 | + |
| 33 | + $declaringClassName = $declaringClass->getName(); |
| 34 | + if (!$this->helper->shouldBeReported($scope, $declaringClassName)) { |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + $namespace = array_slice(explode('\\', $declaringClassName), 0, -1)[0] ?? null; |
| 39 | + if ($namespace === null) { |
| 40 | + if (!$isPropertyInternal) { |
| 41 | + return RestrictedUsage::create( |
| 42 | + sprintf( |
| 43 | + 'Access to %sproperty $%s of internal %s %s.', |
| 44 | + $propertyReflection->isStatic() ? 'static ' : '', |
| 45 | + $propertyReflection->getName(), |
| 46 | + strtolower($propertyReflection->getDeclaringClass()->getClassTypeDescription()), |
| 47 | + $propertyReflection->getDeclaringClass()->getDisplayName(), |
| 48 | + ), |
| 49 | + sprintf( |
| 50 | + '%s.internal%s', |
| 51 | + $propertyReflection->isStatic() ? 'staticProperty' : 'property', |
| 52 | + $propertyReflection->getDeclaringClass()->getClassTypeDescription(), |
| 53 | + ), |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + return RestrictedUsage::create( |
| 58 | + sprintf( |
| 59 | + 'Access to internal %sproperty %s::$%s.', |
| 60 | + $propertyReflection->isStatic() ? 'static ' : '', |
| 61 | + $propertyReflection->getDeclaringClass()->getDisplayName(), |
| 62 | + $propertyReflection->getName(), |
| 63 | + ), |
| 64 | + sprintf('%s.internal', $propertyReflection->isStatic() ? 'staticProperty' : 'property'), |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + if (!$isPropertyInternal) { |
| 69 | + return RestrictedUsage::create( |
| 70 | + sprintf( |
| 71 | + 'Access to %sproperty $%s of internal %s %s from outside its root namespace %s.', |
| 72 | + $propertyReflection->isStatic() ? 'static ' : '', |
| 73 | + $propertyReflection->getName(), |
| 74 | + strtolower($propertyReflection->getDeclaringClass()->getClassTypeDescription()), |
| 75 | + $propertyReflection->getDeclaringClass()->getDisplayName(), |
| 76 | + $namespace, |
| 77 | + ), |
| 78 | + sprintf( |
| 79 | + '%s.internal%s', |
| 80 | + $propertyReflection->isStatic() ? 'staticProperty' : 'property', |
| 81 | + $propertyReflection->getDeclaringClass()->getClassTypeDescription(), |
| 82 | + ), |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + return RestrictedUsage::create( |
| 87 | + sprintf( |
| 88 | + 'Access to internal %sproperty %s::$%s from outside its root namespace %s.', |
| 89 | + $propertyReflection->isStatic() ? 'static ' : '', |
| 90 | + $propertyReflection->getDeclaringClass()->getDisplayName(), |
| 91 | + $propertyReflection->getName(), |
| 92 | + $namespace, |
| 93 | + ), |
| 94 | + sprintf('%s.internal', $propertyReflection->isStatic() ? 'staticProperty' : 'property'), |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments