ClassLike.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. * Gets all methods defined directly in this class/interface/trait
  15. *
  16. * @return ClassMethod[]
  17. */
  18. public function getMethods() : array {
  19. $methods = [];
  20. foreach ($this->stmts as $stmt) {
  21. if ($stmt instanceof ClassMethod) {
  22. $methods[] = $stmt;
  23. }
  24. }
  25. return $methods;
  26. }
  27. /**
  28. * Gets method with the given name defined directly in this class/interface/trait.
  29. *
  30. * @param string $name Name of the method (compared case-insensitively)
  31. *
  32. * @return ClassMethod|null Method node or null if the method does not exist
  33. */
  34. public function getMethod(string $name) {
  35. $lowerName = strtolower($name);
  36. foreach ($this->stmts as $stmt) {
  37. if ($stmt instanceof ClassMethod && $lowerName === $stmt->name->toLowerString()) {
  38. return $stmt;
  39. }
  40. }
  41. return null;
  42. }
  43. }