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 str_starts_with;
14:
15: class Identifier
16: {
17: private IdentifierType $type;
18: public const WILDCARD = '*';
19:
20: 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]*)*/';
21:
22: private string $name;
23:
24: /** @throws InvalidIdentifierName */
25: public function __construct(string $name, IdentifierType $type)
26: {
27: $this->type = $type;
28: if (
29: $name === self::WILDCARD
30: || $name === ReflectionFunction::CLOSURE_NAME
31: || strncmp($name, ReflectionClass::ANONYMOUS_CLASS_NAME_PREFIX, strlen(ReflectionClass::ANONYMOUS_CLASS_NAME_PREFIX)) === 0
32: ) {
33: $this->name = $name;
34:
35: return;
36: }
37:
38: $name = ltrim($name, '\\');
39:
40: if (! preg_match(self::VALID_NAME_REGEXP, $name)) {
41: throw InvalidIdentifierName::fromInvalidName($name);
42: }
43:
44: $this->name = $name;
45: }
46:
47: public function getName(): string
48: {
49: return $this->name;
50: }
51:
52: public function getType(): IdentifierType
53: {
54: return $this->type;
55: }
56:
57: public function isClass(): bool
58: {
59: return $this->type->isClass();
60: }
61:
62: public function isFunction(): bool
63: {
64: return $this->type->isFunction();
65: }
66:
67: public function isConstant(): bool
68: {
69: return $this->type->isConstant();
70: }
71: }
72: