1<?php declare(strict_types=1);
2
3namespace PhpParser\Node;
4
5use PhpParser\NodeAbstract;
6
7/**
8 * Represents a non-namespaced name. Namespaced names are represented using Name nodes.
9 */
10class Identifier extends NodeAbstract {
11    /**
12     * @psalm-var non-empty-string
13     * @var string Identifier as string
14     */
15    public string $name;
16
17    /** @var array<string, bool> */
18    private static array $specialClassNames = [
19        'self'   => true,
20        'parent' => true,
21        'static' => true,
22    ];
23
24    /**
25     * Constructs an identifier node.
26     *
27     * @param string $name Identifier as string
28     * @param array<string, mixed> $attributes Additional attributes
29     */
30    public function __construct(string $name, array $attributes = []) {
31        if ($name === '') {
32            throw new \InvalidArgumentException('Identifier name cannot be empty');
33        }
34
35        $this->attributes = $attributes;
36        $this->name = $name;
37    }
38
39    public function getSubNodeNames(): array {
40        return ['name'];
41    }
42
43    /**
44     * Get identifier as string.
45     *
46     * @psalm-return non-empty-string
47     * @return string Identifier as string.
48     */
49    public function toString(): string {
50        return $this->name;
51    }
52
53    /**
54     * Get lowercased identifier as string.
55     *
56     * @psalm-return non-empty-string&lowercase-string
57     * @return string Lowercased identifier as string
58     */
59    public function toLowerString(): string {
60        return strtolower($this->name);
61    }
62
63    /**
64     * Checks whether the identifier is a special class name (self, parent or static).
65     *
66     * @return bool Whether identifier is a special class name
67     */
68    public function isSpecialClassName(): bool {
69        return isset(self::$specialClassNames[strtolower($this->name)]);
70    }
71
72    /**
73     * Get identifier as string.
74     *
75     * @psalm-return non-empty-string
76     * @return string Identifier as string
77     */
78    public function __toString(): string {
79        return $this->name;
80    }
81
82    public function getType(): string {
83        return 'Identifier';
84    }
85}
86