Drop two more getType() usages in builders

This commit is contained in:
Nikita Popov 2017-11-13 00:24:01 +01:00
parent 8d3f48ab75
commit 0bb74e03aa
2 changed files with 16 additions and 22 deletions

View File

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

View File

@ -48,20 +48,14 @@ class Interface_ extends Declaration
public function addStmt($stmt) { public function addStmt($stmt) {
$stmt = BuilderHelpers::normalizeNode($stmt); $stmt = BuilderHelpers::normalizeNode($stmt);
$type = $stmt->getType(); if ($stmt instanceof Stmt\ClassConst) {
switch ($type) { $this->constants[] = $stmt;
case 'Stmt_ClassConst': } else if ($stmt instanceof Stmt\ClassMethod) {
$this->constants[] = $stmt; // we erase all statements in the body of an interface method
break; $stmt->stmts = null;
$this->methods[] = $stmt;
case 'Stmt_ClassMethod': } else {
// we erase all statements in the body of an interface method throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
$stmt->stmts = null;
$this->methods[] = $stmt;
break;
default:
throw new \LogicException(sprintf('Unexpected node of type "%s"', $type));
} }
return $this; return $this;