ClassLike.php 2.9 KB

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