public function enterNode(\PHPParser_Node $node) { $prettyPrinter = new \PHPParser_PrettyPrinter_Zend(); switch (get_class($node)) { case 'PHPParser_Node_Stmt_Use': /** @var \PHPParser_Node_Stmt_UseUse $use */ foreach ($node->uses as $use) { $this->namespace_aliases[$use->alias] = implode('\\', $use->name->parts); } break; case 'PHPParser_Node_Stmt_Namespace': $this->current_namespace = implode('\\', $node->name->parts); break; case 'PHPParser_Node_Stmt_Class': $class = new ClassReflector($node); $class->setNamespaceAliases($this->namespace_aliases); $class->parseSubElements(); $this->classes[] = $class; break; case 'PHPParser_Node_Stmt_Trait': $trait = new TraitReflector($node); $trait->setNamespaceAliases($this->namespace_aliases); $this->traits[] = $trait; break; case 'PHPParser_Node_Stmt_Interface': $interface = new InterfaceReflector($node); $interface->setNamespaceAliases($this->namespace_aliases); $interface->parseSubElements(); $this->interfaces[] = $interface; break; case 'PHPParser_Node_Stmt_Function': $function = new FunctionReflector($node); $function->setNamespaceAliases($this->namespace_aliases); $this->functions[] = $function; break; case 'PHPParser_Node_Stmt_Const': foreach ($node->consts as $constant) { $reflector = new ConstantReflector($node, $constant); $reflector->setNamespaceAliases($this->namespace_aliases); $this->constants[$reflector->getName()] = $reflector; } break; case 'PHPParser_Node_Expr_FuncCall': if ($node->name instanceof \PHPParser_Node_Name && $node->name == 'define') { $name = trim($prettyPrinter->prettyPrintExpr($node->args[0]->value), '\''); $constant = new \PHPParser_Node_Const($name, $node->args[1]->value, $node->getAttributes()); $constant->namespacedName = new \PHPParser_Node_Name(($this->current_namespace ? $this->current_namespace . '\\' : '') . $name); // we use $constant here both times since this is a // FuncCall, which combines properties that are otherwise // split over 2 objects $reflector = new ConstantReflector($constant, $constant); $reflector->setNamespaceAliases($this->namespace_aliases); $this->constants[$reflector->getName()] = $reflector; } break; case 'PHPParser_Node_Expr_Include': $include = new IncludeReflector($node); $include->setNamespaceAliases($this->namespace_aliases); $this->includes[] = $include; break; } }
public static function getStringRepr(\PHPParser_Node $node) { static $prettyPrinter; if (null === $prettyPrinter) { $prettyPrinter = new \PHPParser_PrettyPrinter_Zend(); } if ($node instanceof \PHPParser_Node_Stmt_If || $node instanceof \PHPParser_Node_Stmt_ElseIf) { $label = 'if ('; $label .= $prettyPrinter->prettyPrintExpr($node->cond); $label .= ')'; return $label; } if ($node instanceof \PHPParser_Node_Stmt_Label) { return 'Label ' . $node->name; } if ($node instanceof \PHPParser_Node_Stmt_Echo) { return 'echo ' . $prettyPrinter->prettyPrint($node->exprs); } if ($node instanceof \PHPParser_Node_Scalar_String) { return 'string(' . strlen($node->value) . ') ' . var_export($node->value, true); } if ($node instanceof \PHPParser_Node_Expr_Variable) { if (is_string($node->name)) { return '$' . $node->name; } return 'Variable'; } if ($node instanceof BlockNode) { $str = 'Block'; if ($parent = $node->getAttribute('parent')) { $str .= ' of ' . self::getStringRepr($parent); } else { $str .= ' (global)'; } return $str; } if ($node instanceof \PHPParser_Node_Expr_Assign) { return 'Assign (L' . $node->getLine() . ')'; } if ($node instanceof \PHPParser_Node_Stmt_Catch) { return 'catch ' . implode("\\", $node->type->parts); } return get_class($node); }