1: <?php declare(strict_types=1);
2:
3: namespace PhpParser\Node\Stmt;
4:
5: use PhpParser\Node;
6: use PhpParser\Node\PropertyItem;
7:
8: abstract class ClassLike extends Node\Stmt {
9: /** @var Node\Identifier|null Name */
10: public ?Node\Identifier $name;
11: /** @var Node\Stmt[] Statements */
12: public array $stmts;
13: /** @var Node\AttributeGroup[] PHP attribute groups */
14: public array $attrGroups;
15:
16: /** @var Node\Name|null Namespaced name (if using NameResolver) */
17: public ?Node\Name $namespacedName;
18:
19: /**
20: * @return TraitUse[]
21: */
22: public function getTraitUses(): array {
23: $traitUses = [];
24: foreach ($this->stmts as $stmt) {
25: if ($stmt instanceof TraitUse) {
26: $traitUses[] = $stmt;
27: }
28: }
29: return $traitUses;
30: }
31:
32: /**
33: * @return ClassConst[]
34: */
35: public function getConstants(): array {
36: $constants = [];
37: foreach ($this->stmts as $stmt) {
38: if ($stmt instanceof ClassConst) {
39: $constants[] = $stmt;
40: }
41: }
42: return $constants;
43: }
44:
45: /**
46: * @return Property[]
47: */
48: public function getProperties(): array {
49: $properties = [];
50: foreach ($this->stmts as $stmt) {
51: if ($stmt instanceof Property) {
52: $properties[] = $stmt;
53: }
54: }
55: return $properties;
56: }
57:
58: /**
59: * Gets property with the given name defined directly in this class/interface/trait.
60: *
61: * @param string $name Name of the property
62: *
63: * @return Property|null Property node or null if the property does not exist
64: */
65: public function getProperty(string $name): ?Property {
66: foreach ($this->stmts as $stmt) {
67: if ($stmt instanceof Property) {
68: foreach ($stmt->props as $prop) {
69: if ($prop instanceof PropertyItem && $name === $prop->name->toString()) {
70: return $stmt;
71: }
72: }
73: }
74: }
75: return null;
76: }
77:
78: /**
79: * Gets all methods defined directly in this class/interface/trait
80: *
81: * @return ClassMethod[]
82: */
83: public function getMethods(): array {
84: $methods = [];
85: foreach ($this->stmts as $stmt) {
86: if ($stmt instanceof ClassMethod) {
87: $methods[] = $stmt;
88: }
89: }
90: return $methods;
91: }
92:
93: /**
94: * Gets method with the given name defined directly in this class/interface/trait.
95: *
96: * @param string $name Name of the method (compared case-insensitively)
97: *
98: * @return ClassMethod|null Method node or null if the method does not exist
99: */
100: public function getMethod(string $name): ?ClassMethod {
101: $lowerName = strtolower($name);
102: foreach ($this->stmts as $stmt) {
103: if ($stmt instanceof ClassMethod && $lowerName === $stmt->name->toLowerString()) {
104: return $stmt;
105: }
106: }
107: return null;
108: }
109: }
110: