Rewrote overly magic code to make it readable (#906)

New code inspired by 950bf8f1d1/lib/PhpParser/Builder/Trait_.php (L43)
This commit is contained in:
Markus Staab 2022-12-14 22:58:37 +01:00 committed by GitHub
parent 21a3e8cac5
commit 4c4af21df8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 22 deletions

View File

@ -111,20 +111,18 @@ class Class_ extends Declaration {
public function addStmt($stmt) { public function addStmt($stmt) {
$stmt = BuilderHelpers::normalizeNode($stmt); $stmt = BuilderHelpers::normalizeNode($stmt);
$targets = [ if ($stmt instanceof Stmt\Property) {
Stmt\TraitUse::class => &$this->uses, $this->properties[] = $stmt;
Stmt\ClassConst::class => &$this->constants, } elseif ($stmt instanceof Stmt\ClassMethod) {
Stmt\Property::class => &$this->properties, $this->methods[] = $stmt;
Stmt\ClassMethod::class => &$this->methods, } elseif ($stmt instanceof Stmt\TraitUse) {
]; $this->uses[] = $stmt;
} elseif ($stmt instanceof Stmt\ClassConst) {
$class = \get_class($stmt); $this->constants[] = $stmt;
if (!isset($targets[$class])) { } else {
throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType())); throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
} }
$targets[$class][] = $stmt;
return $this; return $this;
} }

View File

@ -74,20 +74,18 @@ class Enum_ extends Declaration {
public function addStmt($stmt) { public function addStmt($stmt) {
$stmt = BuilderHelpers::normalizeNode($stmt); $stmt = BuilderHelpers::normalizeNode($stmt);
$targets = [ if ($stmt instanceof Stmt\EnumCase) {
Stmt\TraitUse::class => &$this->uses, $this->enumCases[] = $stmt;
Stmt\EnumCase::class => &$this->enumCases, } elseif ($stmt instanceof Stmt\ClassMethod) {
Stmt\ClassConst::class => &$this->constants, $this->methods[] = $stmt;
Stmt\ClassMethod::class => &$this->methods, } elseif ($stmt instanceof Stmt\TraitUse) {
]; $this->uses[] = $stmt;
} elseif ($stmt instanceof Stmt\ClassConst) {
$class = \get_class($stmt); $this->constants[] = $stmt;
if (!isset($targets[$class])) { } else {
throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType())); throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
} }
$targets[$class][] = $stmt;
return $this; return $this;
} }