Files
apimacro/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/ClassLike.php

110 lines
3.0 KiB
PHP
Raw Normal View History

2024-05-07 12:17:25 +02:00
<?php declare(strict_types=1);
namespace PhpParser\Node\Stmt;
use PhpParser\Node;
2024-05-17 12:24:19 +00:00
use PhpParser\Node\PropertyItem;
2024-05-07 12:17:25 +02:00
2024-05-17 12:24:19 +00:00
abstract class ClassLike extends Node\Stmt {
2024-05-07 12:17:25 +02:00
/** @var Node\Identifier|null Name */
2024-05-17 12:24:19 +00:00
public ?Node\Identifier $name;
2024-05-07 12:17:25 +02:00
/** @var Node\Stmt[] Statements */
2024-05-17 12:24:19 +00:00
public array $stmts;
2024-05-07 12:17:25 +02:00
/** @var Node\AttributeGroup[] PHP attribute groups */
2024-05-17 12:24:19 +00:00
public array $attrGroups;
2024-05-07 12:17:25 +02:00
2024-05-17 12:24:19 +00:00
/** @var Node\Name|null Namespaced name (if using NameResolver) */
public ?Node\Name $namespacedName;
2024-05-07 12:17:25 +02:00
/**
* @return TraitUse[]
*/
2024-05-17 12:24:19 +00:00
public function getTraitUses(): array {
2024-05-07 12:17:25 +02:00
$traitUses = [];
foreach ($this->stmts as $stmt) {
if ($stmt instanceof TraitUse) {
$traitUses[] = $stmt;
}
}
return $traitUses;
}
/**
* @return ClassConst[]
*/
2024-05-17 12:24:19 +00:00
public function getConstants(): array {
2024-05-07 12:17:25 +02:00
$constants = [];
foreach ($this->stmts as $stmt) {
if ($stmt instanceof ClassConst) {
$constants[] = $stmt;
}
}
return $constants;
}
/**
* @return Property[]
*/
2024-05-17 12:24:19 +00:00
public function getProperties(): array {
2024-05-07 12:17:25 +02:00
$properties = [];
foreach ($this->stmts as $stmt) {
if ($stmt instanceof Property) {
$properties[] = $stmt;
}
}
return $properties;
}
/**
* Gets property with the given name defined directly in this class/interface/trait.
*
* @param string $name Name of the property
*
* @return Property|null Property node or null if the property does not exist
*/
2024-05-17 12:24:19 +00:00
public function getProperty(string $name): ?Property {
2024-05-07 12:17:25 +02:00
foreach ($this->stmts as $stmt) {
if ($stmt instanceof Property) {
foreach ($stmt->props as $prop) {
2024-05-17 12:24:19 +00:00
if ($prop instanceof PropertyItem && $name === $prop->name->toString()) {
2024-05-07 12:17:25 +02:00
return $stmt;
}
}
}
}
return null;
}
/**
* Gets all methods defined directly in this class/interface/trait
*
* @return ClassMethod[]
*/
2024-05-17 12:24:19 +00:00
public function getMethods(): array {
2024-05-07 12:17:25 +02:00
$methods = [];
foreach ($this->stmts as $stmt) {
if ($stmt instanceof ClassMethod) {
$methods[] = $stmt;
}
}
return $methods;
}
/**
* Gets method with the given name defined directly in this class/interface/trait.
*
* @param string $name Name of the method (compared case-insensitively)
*
* @return ClassMethod|null Method node or null if the method does not exist
*/
2024-05-17 12:24:19 +00:00
public function getMethod(string $name): ?ClassMethod {
2024-05-07 12:17:25 +02:00
$lowerName = strtolower($name);
foreach ($this->stmts as $stmt) {
if ($stmt instanceof ClassMethod && $lowerName === $stmt->name->toLowerString()) {
return $stmt;
}
}
return null;
}
}