| 1: | <?php declare(strict_types = 1); |
| 2: | |
| 3: | namespace PHPStan\Rules; |
| 4: | |
| 5: | use PHPStan\Reflection\ClassConstantReflection; |
| 6: | use PHPStan\Reflection\ExtendedMethodReflection; |
| 7: | use PHPStan\Reflection\ExtendedPropertyReflection; |
| 8: | use PHPStan\Reflection\FunctionReflection; |
| 9: | use PHPStan\Rules\RestrictedUsage\ClassNameConstantReflection; |
| 10: | use function sprintf; |
| 11: | use function ucfirst; |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | final class ClassNameUsageLocation |
| 17: | { |
| 18: | |
| 19: | public const TRAIT_USE = 'traitUse'; |
| 20: | public const STATIC_PROPERTY_ACCESS = 'staticProperty'; |
| 21: | public const PHPDOC_TAG_ASSERT = 'assert'; |
| 22: | public const ATTRIBUTE = 'attribute'; |
| 23: | public const EXCEPTION_CATCH = 'catch'; |
| 24: | public const CLASS_CONSTANT_ACCESS = 'classConstant'; |
| 25: | public const CLASS_IMPLEMENTS = 'classImplements'; |
| 26: | public const ENUM_IMPLEMENTS = 'enumImplements'; |
| 27: | public const INTERFACE_EXTENDS = 'interfaceExtends'; |
| 28: | public const CLASS_EXTENDS = 'classExtends'; |
| 29: | public const INSTANCEOF = 'instanceof'; |
| 30: | public const PROPERTY_TYPE = 'property'; |
| 31: | public const PARAMETER_TYPE = 'parameter'; |
| 32: | public const RETURN_TYPE = 'return'; |
| 33: | public const PHPDOC_TAG_SELF_OUT = 'selfOut'; |
| 34: | public const PHPDOC_TAG_VAR = 'varTag'; |
| 35: | public const INSTANTIATION = 'new'; |
| 36: | public const TYPE_ALIAS = 'typeAlias'; |
| 37: | public const PHPDOC_TAG_METHOD = 'methodTag'; |
| 38: | public const PHPDOC_TAG_MIXIN = 'mixin'; |
| 39: | public const PHPDOC_TAG_PROPERTY = 'propertyTag'; |
| 40: | public const PHPDOC_TAG_REQUIRE_EXTENDS = 'requireExtends'; |
| 41: | public const PHPDOC_TAG_REQUIRE_IMPLEMENTS = 'requireImplements'; |
| 42: | public const PHPDOC_TAG_SEALED = 'sealed'; |
| 43: | public const STATIC_METHOD_CALL = 'staticMethod'; |
| 44: | public const PHPDOC_TAG_TEMPLATE_BOUND = 'templateBound'; |
| 45: | public const PHPDOC_TAG_TEMPLATE_DEFAULT = 'templateDefault'; |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | private function __construct(public readonly string $value, public readonly array $data) |
| 52: | { |
| 53: | } |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | public static function from(string $value, array $data = []): self |
| 60: | { |
| 61: | return new self($value, $data); |
| 62: | } |
| 63: | |
| 64: | public function getMethod(): ?ExtendedMethodReflection |
| 65: | { |
| 66: | return $this->data['method'] ?? null; |
| 67: | } |
| 68: | |
| 69: | public function getProperty(): ?ExtendedPropertyReflection |
| 70: | { |
| 71: | return $this->data['property'] ?? null; |
| 72: | } |
| 73: | |
| 74: | public function getFunction(): ?FunctionReflection |
| 75: | { |
| 76: | return $this->data['function'] ?? null; |
| 77: | } |
| 78: | |
| 79: | public function getPhpDocTagName(): ?string |
| 80: | { |
| 81: | return $this->data['phpDocTagName'] ?? null; |
| 82: | } |
| 83: | |
| 84: | public function getAssertedExprString(): ?string |
| 85: | { |
| 86: | return $this->data['assertedExprString'] ?? null; |
| 87: | } |
| 88: | |
| 89: | public function getClassConstant(): ?ClassConstantReflection |
| 90: | { |
| 91: | return $this->data['classConstant'] ?? null; |
| 92: | } |
| 93: | |
| 94: | public function getCurrentClassName(): ?string |
| 95: | { |
| 96: | return $this->data['currentClassName'] ?? null; |
| 97: | } |
| 98: | |
| 99: | public function getParameterName(): ?string |
| 100: | { |
| 101: | return $this->data['parameterName'] ?? null; |
| 102: | } |
| 103: | |
| 104: | public function getTypeAliasName(): ?string |
| 105: | { |
| 106: | return $this->data['typeAliasName'] ?? null; |
| 107: | } |
| 108: | |
| 109: | public function getMethodTagName(): ?string |
| 110: | { |
| 111: | return $this->data['methodTagName'] ?? null; |
| 112: | } |
| 113: | |
| 114: | public function getPropertyTagName(): ?string |
| 115: | { |
| 116: | return $this->data['propertyTagName'] ?? null; |
| 117: | } |
| 118: | |
| 119: | public function getTemplateTagName(): ?string |
| 120: | { |
| 121: | return $this->data['templateTagName'] ?? null; |
| 122: | } |
| 123: | |
| 124: | public function isInAnomyousFunction(): bool |
| 125: | { |
| 126: | return $this->data['isInAnonymousFunction'] ?? false; |
| 127: | } |
| 128: | |
| 129: | public function createMessage(string $part): string |
| 130: | { |
| 131: | switch ($this->value) { |
| 132: | case self::TRAIT_USE: |
| 133: | if ($this->getCurrentClassName() !== null) { |
| 134: | return sprintf('Usage of %s in class %s.', $part, $this->getCurrentClassName()); |
| 135: | } |
| 136: | return sprintf('Usage of %s.', $part); |
| 137: | case self::STATIC_PROPERTY_ACCESS: |
| 138: | $property = $this->getProperty(); |
| 139: | if ($property !== null) { |
| 140: | return sprintf('Access to static property $%s on %s.', $property->getName(), $part); |
| 141: | } |
| 142: | |
| 143: | return sprintf('Access to static property on %s.', $part); |
| 144: | case self::PHPDOC_TAG_ASSERT: |
| 145: | $phpDocTagName = $this->getPhpDocTagName(); |
| 146: | $assertExprString = $this->getAssertedExprString(); |
| 147: | if ($phpDocTagName !== null && $assertExprString !== null) { |
| 148: | return sprintf('PHPDoc tag %s for %s references %s.', $phpDocTagName, $assertExprString, $part); |
| 149: | } |
| 150: | |
| 151: | return sprintf('Assert tag references %s.', $part); |
| 152: | case self::ATTRIBUTE: |
| 153: | return sprintf('Attribute references %s.', $part); |
| 154: | case self::EXCEPTION_CATCH: |
| 155: | return sprintf('Catching %s.', $part); |
| 156: | case self::CLASS_CONSTANT_ACCESS: |
| 157: | |
| 158: | if ($this->getClassConstant() !== null && !$this->getClassConstant() instanceof ClassNameConstantReflection) { |
| 159: | return sprintf('Access to constant %s on %s.', $this->getClassConstant()->getName(), $part); |
| 160: | } |
| 161: | return sprintf('Access to constant on %s.', $part); |
| 162: | case self::CLASS_IMPLEMENTS: |
| 163: | if ($this->getCurrentClassName() !== null) { |
| 164: | return sprintf('Class %s implements %s.', $this->getCurrentClassName(), $part); |
| 165: | } |
| 166: | |
| 167: | return sprintf('Anonymous class implements %s.', $part); |
| 168: | case self::ENUM_IMPLEMENTS: |
| 169: | if ($this->getCurrentClassName() !== null) { |
| 170: | return sprintf('Enum %s implements %s.', $this->getCurrentClassName(), $part); |
| 171: | } |
| 172: | |
| 173: | return sprintf('Enum implements %s.', $part); |
| 174: | case self::INTERFACE_EXTENDS: |
| 175: | if ($this->getCurrentClassName() !== null) { |
| 176: | return sprintf('Interface %s extends %s.', $this->getCurrentClassName(), $part); |
| 177: | } |
| 178: | |
| 179: | return sprintf('Interface extends %s.', $part); |
| 180: | case self::CLASS_EXTENDS: |
| 181: | if ($this->getCurrentClassName() !== null) { |
| 182: | return sprintf('Class %s extends %s.', $this->getCurrentClassName(), $part); |
| 183: | } |
| 184: | |
| 185: | return sprintf('Anonymous class extends %s.', $part); |
| 186: | case self::INSTANCEOF: |
| 187: | return sprintf('Instanceof references %s.', $part); |
| 188: | case self::PROPERTY_TYPE: |
| 189: | $property = $this->getProperty(); |
| 190: | if ($property !== null) { |
| 191: | return sprintf('Property $%s references %s in its type.', $property->getName(), $part); |
| 192: | } |
| 193: | return sprintf('Property references %s in its type.', $part); |
| 194: | case self::PARAMETER_TYPE: |
| 195: | $parameterName = $this->getParameterName(); |
| 196: | if ($parameterName !== null) { |
| 197: | if ($this->isInAnomyousFunction()) { |
| 198: | return sprintf('Parameter $%s of anonymous function has typehint with %s.', $parameterName, $part); |
| 199: | } |
| 200: | if ($this->getMethod() !== null) { |
| 201: | if ($this->getCurrentClassName() !== null) { |
| 202: | return sprintf('Parameter $%s of method %s::%s() has typehint with %s.', $parameterName, $this->getCurrentClassName(), $this->getMethod()->getName(), $part); |
| 203: | } |
| 204: | |
| 205: | return sprintf('Parameter $%s of method %s() in anonymous class has typehint with %s.', $parameterName, $this->getMethod()->getName(), $part); |
| 206: | } |
| 207: | |
| 208: | if ($this->getFunction() !== null) { |
| 209: | return sprintf('Parameter $%s of function %s() has typehint with %s.', $parameterName, $this->getFunction()->getName(), $part); |
| 210: | } |
| 211: | |
| 212: | return sprintf('Parameter $%s has typehint with %s.', $parameterName, $part); |
| 213: | } |
| 214: | |
| 215: | return sprintf('Parameter has typehint with %s.', $part); |
| 216: | case self::RETURN_TYPE: |
| 217: | if ($this->isInAnomyousFunction()) { |
| 218: | return sprintf('Return type of anonymous function has typehint with %s.', $part); |
| 219: | } |
| 220: | if ($this->getMethod() !== null) { |
| 221: | if ($this->getCurrentClassName() !== null) { |
| 222: | return sprintf('Return type of method %s::%s() has typehint with %s.', $this->getCurrentClassName(), $this->getMethod()->getName(), $part); |
| 223: | } |
| 224: | |
| 225: | return sprintf('Return type of method %s() in anonymous class has typehint with %s.', $this->getMethod()->getName(), $part); |
| 226: | } |
| 227: | |
| 228: | if ($this->getFunction() !== null) { |
| 229: | return sprintf('Return type of function %s() has typehint with %s.', $this->getFunction()->getName(), $part); |
| 230: | } |
| 231: | |
| 232: | return sprintf('Return type has typehint with %s.', $part); |
| 233: | case self::PHPDOC_TAG_SELF_OUT: |
| 234: | return sprintf('PHPDoc tag @phpstan-self-out references %s.', $part); |
| 235: | case self::PHPDOC_TAG_VAR: |
| 236: | return sprintf('PHPDoc tag @var references %s.', $part); |
| 237: | case self::INSTANTIATION: |
| 238: | return sprintf('Instantiation of %s.', $part); |
| 239: | case self::TYPE_ALIAS: |
| 240: | if ($this->getTypeAliasName() !== null) { |
| 241: | return sprintf('Type alias %s references %s.', $this->getTypeAliasName(), $part); |
| 242: | } |
| 243: | |
| 244: | return sprintf('Type alias references %s.', $part); |
| 245: | case self::PHPDOC_TAG_METHOD: |
| 246: | if ($this->getMethodTagName() !== null) { |
| 247: | return sprintf('PHPDoc tag @method for %s() references %s.', $this->getMethodTagName(), $part); |
| 248: | } |
| 249: | return sprintf('PHPDoc tag @method references %s.', $part); |
| 250: | case self::PHPDOC_TAG_MIXIN: |
| 251: | return sprintf('PHPDoc tag @mixin references %s.', $part); |
| 252: | case self::PHPDOC_TAG_PROPERTY: |
| 253: | if ($this->getPropertyTagName() !== null) { |
| 254: | return sprintf('PHPDoc tag @property for $%s references %s.', $this->getPropertyTagName(), $part); |
| 255: | } |
| 256: | return sprintf('PHPDoc tag @property references %s.', $part); |
| 257: | case self::PHPDOC_TAG_REQUIRE_EXTENDS: |
| 258: | return sprintf('PHPDoc tag @phpstan-require-extends references %s.', $part); |
| 259: | case self::PHPDOC_TAG_REQUIRE_IMPLEMENTS: |
| 260: | return sprintf('PHPDoc tag @phpstan-require-implements references %s.', $part); |
| 261: | case self::PHPDOC_TAG_SEALED: |
| 262: | return sprintf('PHPDoc tag @phpstan-sealed references %s.', $part); |
| 263: | case self::STATIC_METHOD_CALL: |
| 264: | $method = $this->getMethod(); |
| 265: | if ($method !== null) { |
| 266: | return sprintf('Call to static method %s() on %s.', $method->getName(), $part); |
| 267: | } |
| 268: | |
| 269: | return sprintf('Call to static method on %s.', $part); |
| 270: | case self::PHPDOC_TAG_TEMPLATE_BOUND: |
| 271: | if ($this->getTemplateTagName() !== null) { |
| 272: | return sprintf('PHPDoc tag @template %s bound references %s.', $this->getTemplateTagName(), $part); |
| 273: | } |
| 274: | |
| 275: | return sprintf('PHPDoc tag @template bound references %s.', $part); |
| 276: | case self::PHPDOC_TAG_TEMPLATE_DEFAULT: |
| 277: | if ($this->getTemplateTagName() !== null) { |
| 278: | return sprintf('PHPDoc tag @template %s default references %s.', $this->getTemplateTagName(), $part); |
| 279: | } |
| 280: | |
| 281: | return sprintf('PHPDoc tag @template default references %s.', $part); |
| 282: | } |
| 283: | } |
| 284: | |
| 285: | public function createIdentifier(string $secondPart): string |
| 286: | { |
| 287: | if ($this->value === self::CLASS_IMPLEMENTS) { |
| 288: | return sprintf('class.implements%s', ucfirst($secondPart)); |
| 289: | } |
| 290: | if ($this->value === self::ENUM_IMPLEMENTS) { |
| 291: | return sprintf('enum.implements%s', ucfirst($secondPart)); |
| 292: | } |
| 293: | if ($this->value === self::INTERFACE_EXTENDS) { |
| 294: | return sprintf('interface.extends%s', ucfirst($secondPart)); |
| 295: | } |
| 296: | if ($this->value === self::CLASS_EXTENDS) { |
| 297: | return sprintf('class.extends%s', ucfirst($secondPart)); |
| 298: | } |
| 299: | if ($this->value === self::PHPDOC_TAG_TEMPLATE_BOUND) { |
| 300: | return sprintf('generics.%sBound', $secondPart); |
| 301: | } |
| 302: | if ($this->value === self::PHPDOC_TAG_TEMPLATE_DEFAULT) { |
| 303: | return sprintf('generics.%sDefault', $secondPart); |
| 304: | } |
| 305: | |
| 306: | return sprintf('%s.%s', $this->value, $secondPart); |
| 307: | } |
| 308: | |
| 309: | } |
| 310: | |