| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace PHPStan\BetterReflection\Identifier; |
| 6: | |
| 7: | use PHPStan\BetterReflection\Identifier\Exception\InvalidIdentifierName; |
| 8: | use PHPStan\BetterReflection\Reflection\ReflectionClass; |
| 9: | use PHPStan\BetterReflection\Reflection\ReflectionFunction; |
| 10: | |
| 11: | use function ltrim; |
| 12: | use function preg_match; |
| 13: | use function strpos; |
| 14: | |
| 15: | class Identifier |
| 16: | { |
| 17: | public const WILDCARD = '*'; |
| 18: | |
| 19: | private const VALID_NAME_REGEXP = '/([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*/'; |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | private $name; |
| 25: | |
| 26: | |
| 27: | |
| 28: | private $type; |
| 29: | |
| 30: | |
| 31: | public function __construct(string $name, IdentifierType $type) |
| 32: | { |
| 33: | $this->type = $type; |
| 34: | if ( |
| 35: | $name === self::WILDCARD |
| 36: | || $name === ReflectionFunction::CLOSURE_NAME |
| 37: | || strpos($name, ReflectionClass::ANONYMOUS_CLASS_NAME_PREFIX) === 0 |
| 38: | ) { |
| 39: | $this->name = $name; |
| 40: | |
| 41: | return; |
| 42: | } |
| 43: | |
| 44: | $name = ltrim($name, '\\'); |
| 45: | |
| 46: | if (! preg_match(self::VALID_NAME_REGEXP, $name)) { |
| 47: | throw InvalidIdentifierName::fromInvalidName($name); |
| 48: | } |
| 49: | |
| 50: | $this->name = $name; |
| 51: | } |
| 52: | |
| 53: | public function getName(): string |
| 54: | { |
| 55: | return $this->name; |
| 56: | } |
| 57: | |
| 58: | public function getType(): IdentifierType |
| 59: | { |
| 60: | return $this->type; |
| 61: | } |
| 62: | |
| 63: | public function isClass(): bool |
| 64: | { |
| 65: | return $this->type->isClass(); |
| 66: | } |
| 67: | |
| 68: | public function isFunction(): bool |
| 69: | { |
| 70: | return $this->type->isFunction(); |
| 71: | } |
| 72: | |
| 73: | public function isConstant(): bool |
| 74: | { |
| 75: | return $this->type->isConstant(); |
| 76: | } |
| 77: | } |
| 78: | |