本文整理汇总了PHP中PhpParser\Node类的典型用法代码示例。如果您正苦于以下问题:PHP Node类的具体用法?PHP Node怎么用?PHP Node使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Node类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: leaveNode
/**
* @link http://www.slideshare.net/rdohms/your-code-sucks-lets-fix-it-15471808
* @link http://www.slideshare.net/guilhermeblanco/object-calisthenics-applied-to-php
*
* @param Node $node
*
* @return void
*/
public function leaveNode(Node $node)
{
if (!$node instanceof Node\Stmt\Else_ && !$node instanceof Node\Stmt\ElseIf_) {
return;
}
$this->addError(sprintf('Object Calisthenics error: Do not use the "%s" keyword!', $node instanceof Node\Stmt\ElseIf_ ? 'elseif' : 'else'), $node->getLine(), ParseError::TYPE_ERROR);
}
示例2: enterNode
/**
* {@inheritdoc}
*/
public function enterNode(Node $node)
{
if ($node instanceof Node\Param && $node->type instanceof Node\Name) {
$typeHintUsage = new TypeHintUsage($node->type->toString(), $node->getLine());
$this->phpFileInfo->addTypeHintUsage($typeHintUsage);
}
}
示例3: isTooLong
/**
* Checks if a function, class or method is too long according to its lines of code
*
* @param Node $node
* @param int $threshold
* @return bool
*/
protected function isTooLong(Node $node, $threshold)
{
$startLine = $node->getAttribute('startLine');
$endLine = $node->getAttribute('endLine');
$loc = $endLine - $startLine;
return $loc > $threshold;
}
示例4: push
/**
* @param Node|string|array|\Traversable|array $symbol
* @return Node|string|array|\Traversable|array
*/
public function push($symbol)
{
if ($symbol === false || $symbol === null || $symbol === '') {
return $symbol;
}
if ($symbol instanceof Buffer) {
$this->push($symbol->toValue());
} elseif ($symbol instanceof Node) {
$this->parts[] = $symbol;
$this->length++;
} elseif (is_string($symbol)) {
$len = count($this->parts);
if ($len > 0 && is_string($this->parts[$len - 1])) {
$this->parts[$len - 1] .= $symbol;
} else {
$this->parts[] = $symbol;
}
$this->length += strlen($symbol);
$this->strLength += strlen($symbol);
} elseif ($symbol instanceof \Traversable || is_array($symbol)) {
$ret = [];
foreach ($symbol as $part) {
$ret[] = $this->push($part);
}
return $ret;
} else {
throw new \InvalidArgumentException(sprintf('Part type unsupported: %s', get_class($symbol)));
}
return $symbol;
}
示例5: leaveNode
public function leaveNode(Node $node)
{
$type = $node->getType();
if ($type == "Expr_BinaryOp_Concat") {
if ($node->right) {
//转为symbol
$right_symbol = SymbolUtils::getSymbolByNode($node->right);
$right_symbol != null && array_push($this->items, $right_symbol);
}
if ($node->left->getType() != "Expr_BinaryOp_Concat") {
$left_symbol = SymbolUtils::getSymbolByNode($node->left);
$left_symbol != null && array_push($this->items, $left_symbol);
}
} else {
if ($type == "Scalar_Encapsed") {
foreach ($node->parts as $item) {
if (!is_object($item)) {
$valueSymbol = new ValueSymbol();
$valueSymbol->setValue($item);
$valueSymbol != null && array_push($this->items, $valueSymbol);
} else {
$setItem = SymbolUtils::getSymbolByNode($item);
$setItem != null && array_push($this->items, $setItem);
}
}
}
}
}
示例6: enterNode
public function enterNode(PhpParser\Node $node)
{
if ($node instanceof Stmt\Function_) {
// create new context, keep parent
$this->stack->start(new FunctionContext($node->name, $node->getLine(), $node->getAttribute('endLine')));
}
}
示例7: leaveNode
/**
* @param Node $node
*
* @return void
*/
public function leaveNode(Node $node)
{
if (!$node instanceof Node\Expr\Exit_) {
return;
}
$this->addError(sprintf('Found a forbidden exit statement.'), $node->getLine(), ParseError::TYPE_ERROR);
}
示例8: enterNode
public function enterNode(Node $node)
{
if ($node->getType() == 'Stmt_Use') {
foreach ($node->uses as $use) {
$this->uses[] = $use;
}
}
// echo $node->getType() . "\n";
if (isset($node->class) && $node->class) {
if ($node->class->getType() != 'Name_FullyQualified') {
$this->names[] = $node->class;
}
}
if (isset($node->extends) && $node->extends) {
if ($node->extends->getType() != 'Name_FullyQualified') {
$this->names[] = $node->extends;
}
}
if (isset($node->implements) && $node->implements) {
foreach ($node->implements as $implements) {
if ($implements->getType() != 'Name_FullyQualified') {
$this->names[] = $implements;
}
}
}
if (isset($node->params)) {
foreach ($node->params as $param) {
if ($param->type && !is_string($param->type)) {
if ($param->type->getType() != 'Name_FullyQualified') {
$this->names[] = $param->type;
}
}
}
}
}
示例9: enterNode
public function enterNode(Node $node)
{
if (!empty($this->stack)) {
$node->setAttribute('parent', $this->stack[count($this->stack) - 1]);
}
$this->stack[] = $node;
}
示例10: enterNode
/**
* {@inheritdoc}
*/
public function enterNode(Node $node)
{
if ($node instanceof Node\Expr\New_ && $node->class instanceof Node\Name) {
$classUsage = new ClassUsage($node->class->toString(), $node->getLine());
$this->phpFileInfo->addClassUsage($classUsage);
}
}
示例11: leaveNode
/**
* {@inheritdoc}
*/
public function leaveNode(Node $node)
{
// Keep traces of global variable
if ($node instanceof Node\Stmt\Global_) {
foreach ($node->vars as $variable) {
$this->globalsInCurrentLocalScope[] = $variable->name;
}
}
// Check if the variable is marked as global or used in the global scope
if ($node instanceof Node\Expr\Variable) {
if ($this->isInGlobalScope || in_array($node->name, $this->globalsInCurrentLocalScope)) {
$this->checkIfGlobalVariableWasRemoved($node->name, $node->getLine());
}
}
// Check if the variable is used from the $GLOBALS variable
if ($node instanceof Node\Expr\ArrayDimFetch) {
if ($node->var instanceof Node\Expr\Variable && $node->var->name === 'GLOBALS' && $node->dim instanceof Node\Scalar\String_) {
$this->checkIfGlobalVariableWasRemoved($node->dim->value, $node->dim->getLine());
}
}
// Check if we re-enter in the global scope
if ($node instanceof Node\FunctionLike) {
$this->isInGlobalScope = true;
$this->globalsInCurrentLocalScope = array();
}
}
示例12: enterNode
public function enterNode(Node $node)
{
if (empty($this->node) && $this->line == $node->getAttribute('endLine')) {
$this->node = $node;
}
parent::enterNode($node);
}
示例13: leaveNode
public function leaveNode(Node $node)
{
if ($node instanceof Node\Scalar\String_ || $node instanceof Node\Stmt\InlineHTML) {
$new_data = array('filename' => $this->filename, 'line' => $node->getLine(), 'value' => (string) $node->value);
array_push($this->data, $new_data);
}
}
示例14: enterNode
/**
* Called when entering a node.
*
* Return value semantics:
* * null: $node stays as-is
* * otherwise: $node is set to the return value
*
* @param Node $node Node
*
* @return null|Node Node
*/
public function enterNode(Node $node)
{
if (isset($node->returnType)) {
var_dump($node->returnType);
die;
} elseif ($node instanceof Node\Param) {
if ($node->hasAttribute("generic_name")) {
$type = $node->getAttribute("generic_name");
if (isset($this->genericTypes[$type])) {
$node->type = new Node\Name\FullyQualified($this->genericTypes[$type]);
} else {
throw new \LogicException("Bad generic found");
}
} elseif ($node->type instanceof Node\Name && $node->type->hasAttribute("generics") && $node->type->getAttribute("generics")) {
$type = $node->getAttribute("original_type")->parts;
foreach ($node->type->getAttribute("generics") as $generic) {
if (isset($this->genericTypes[$generic])) {
$value = str_replace("\\", Engine::NS_TOKEN, $this->genericTypes[$generic]);
$type[] = Engine::CLASS_TOKEN . $value . Engine::CLASS_TOKEN;
} else {
throw new \LogicException("Bad generic found");
}
}
$node->type = new Node\Name\FullyQualified($type);
} elseif ((string) $node->name == "item") {
var_dump($node);
die;
}
}
}
示例15: enterNode
/**
* Check all nodes
*
* @param Node $node
* @return void
**/
public function enterNode(Node $node)
{
// Skip nodes without comments.
if (!$node->hasAttribute("comments")) {
return;
}
// Check if annotations should be preserved. Only nodes with actual
// doc comment blocks are processed.
$comments = [];
if ($this->preserveAnnotations) {
$docComment = $node->getDocComment();
if ($docComment) {
$text = $docComment->getText();
// Check if it is a doc comment.
if (strpos($text, "/**") !== false) {
$text = $this->stripComment($text);
if ($text) {
$comments = [new Comment($text)];
}
}
}
}
// Remove (or set) comments.
$node->setAttribute("comments", $comments);
return $node;
}