ClassLike.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /**
  14. * @return TraitUse[]
  15. */
  16. public function getTraitUses() : array {
  17. $traitUses = [];
  18. foreach ($this->stmts as $stmt) {
  19. if ($stmt instanceof TraitUse) {
  20. $traitUses[] = $stmt;
  21. }
  22. }
  23. return $traitUses;
  24. }
  25. /**
  26. * @return ClassConst[]
  27. */
  28. public function getConstants() : array {
  29. $constants = [];
  30. foreach ($this->stmts as $stmt) {
  31. if ($stmt instanceof ClassConst) {
  32. $constants[] = $stmt;
  33. }
  34. }
  35. return $constants;
  36. }
  37. /**
  38. * @return Property[]
  39. */
  40. public function getProperties() : array {
  41. $properties = [];
  42. foreach ($this->stmts as $stmt) {
  43. if ($stmt instanceof Property) {
  44. $properties[] = $stmt;
  45. }
  46. }
  47. return $properties;
  48. }
  49. /**
  50. * Gets all methods defined directly in this class/interface/trait
  51. *
  52. * @return ClassMethod[]
  53. */
  54. public function getMethods() : array {
  55. $methods = [];
  56. foreach ($this->stmts as $stmt) {
  57. if ($stmt instanceof ClassMethod) {
  58. $methods[] = $stmt;
  59. }
  60. }
  61. return $methods;
  62. }
  63. /**
  64. * Gets method with the given name defined directly in this class/interface/trait.
  65. *
  66. * @param string $name Name of the method (compared case-insensitively)
  67. *
  68. * @return ClassMethod|null Method node or null if the method does not exist
  69. */
  70. public function getMethod(string $name) {
  71. $lowerName = strtolower($name);
  72. foreach ($this->stmts as $stmt) {
  73. if ($stmt instanceof ClassMethod && $lowerName === $stmt->name->toLowerString()) {
  74. return $stmt;
  75. }
  76. }
  77. return null;
  78. }
  79. }