ClassLike.php 2.9 KB

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