本文整理汇总了PHP中PhpParser\Node::getAttribute方法的典型用法代码示例。如果您正苦于以下问题:PHP Node::getAttribute方法的具体用法?PHP Node::getAttribute怎么用?PHP Node::getAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PhpParser\Node
的用法示例。
在下文中一共展示了Node::getAttribute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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;
}
}
}
示例2: enterNode
public function enterNode(AstNode $node)
{
// Determine information about the closure's location
if (!$this->closureNode) {
if ($node instanceof NamespaceNode) {
$namespace = $node->name && is_array($node->name->parts) ? implode('\\', $node->name->parts) : null;
$this->location['namespace'] = $namespace;
}
if ($node instanceof TraitNode) {
$this->location['trait'] = $node->name;
$this->location['class'] = null;
} elseif ($node instanceof ClassNode) {
$this->location['class'] = $node->name;
$this->location['trait'] = null;
}
}
// Locate the node of the closure
if ($node instanceof ClosureNode) {
if ($node->getAttribute('startLine') == $this->location['line']) {
if ($this->closureNode) {
$line = $this->location['file'] . ':' . $node->getAttribute('startLine');
throw new ClosureAnalysisException("Two closures were " . "declared on the same line ({$line}) of code. Cannot " . "determine which closure was the intended target.");
} else {
$this->closureNode = $node;
}
}
}
}
示例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: leaveNode
public function leaveNode(\PhpParser\Node $node)
{
if (($parentFile = $this->getCurrentFile()) !== null) {
if ($node instanceof \PhpParser\Node\Expr\Include_) {
$currentFile = self::getIncludeFile($node->expr, ['__DIR__' => dirname($parentFile), '__FILE__' => $parentFile]);
if ($node->type == \PhpParser\Node\Expr\Include_::TYPE_INCLUDE_ONCE || $node->type == \PhpParser\Node\Expr\Include_::TYPE_REQUIRE_ONCE) {
if ($this->getPhpFileCombine()->isParsed($currentFile)) {
return \PhpParser\NodeTraverser::REMOVE_NODE;
}
}
if ($this->getPhpFileCombine()->parseFile($currentFile, $parentFile) === false) {
return \PhpParser\NodeTraverser::REMOVE_NODE;
}
$stmts = $this->getPhpFileCombine()->traverseIncludeNodes()->getStmts();
if (isset($stmts[0])) {
if ($stmts[0] instanceof \PhpParser\Node\Stmt\Namespace_ === false) {
$stmts[0]->setAttribute('comments', array_merge($node->getAttribute('comments', []), $stmts[0]->getAttribute('comments', [])));
} else {
$stmts[0]->stmts[0]->setAttribute('comments', array_merge($node->getAttribute('comments', []), $stmts[0]->stmts[0]->getAttribute('comments', [])));
}
}
return $stmts;
}
}
}
示例5: __construct
public function __construct($message, AbstractRule $rule, Node $node, $file)
{
$this->message = $message;
$this->rule = $rule;
$this->file = $file;
$this->startLine = $node->getAttribute('startLine');
$this->endLine = $node->getAttribute('endLine');
}
示例6: getLineNumber
/**
* Get the line number of the error.
*
* @return string
*/
public function getLineNumber()
{
$sl = $this->node->getAttribute('startLine');
$el = $this->node->getAttribute('endLine');
if ($sl != $el) {
return "{$sl}-{$el}";
} else {
return (string) $sl;
}
}
示例7: enterNode
public function enterNode(Node $node)
{
if ($node instanceof Node\Expr\Yield_) {
$startTokenPosition = $node->getAttribute('startTokenPos');
$endTokenPosition = $node->getAttribute('endTokenPos');
if (!($this->tokenCollection->isTokenPrecededBy($startTokenPosition, '(') && $this->tokenCollection->isTokenFollowedBy($endTokenPosition, ')')) && !$this->expressionStack->isEmpty()) {
$this->addContextMessage('"yield" usage in expression context', $this->expressionStack->top());
}
} elseif ($node instanceof Node\Expr) {
$this->expressionStack->push($node);
}
}
示例8: enterNode
public function enterNode(Node $node)
{
if (empty($this->node) && $this->line == $node->getAttribute('endLine')) {
$this->node = $node;
}
parent::enterNode($node);
}
示例9: leaveNode
public function leaveNode(Node $node)
{
if (!$node instanceof ConstFetch) {
$callback = [$this, 'rewrite' . ucfirst($node->getType())];
if (is_callable($callback)) {
call_user_func_array($callback, [$node]);
}
return;
}
if ($this->disable_const_rewrite_level > 0) {
return;
}
if (!$node->name instanceof Name) {
return;
}
if (!$node->name->isUnqualified()) {
return;
}
if (!ConstantPatcher::isBlacklisted((string) $node->name)) {
$replacement = new FullyQualified(array());
$replacement->set('\\__ConstProxy__::get(\'' . (string) $node->name . '\')');
$pos = $node->getAttribute('startTokenPos');
ConstantPatcher::$replacement[$pos] = '\\__ConstProxy__::get(\'' . (string) $node->name . '\')';
$node->name = $replacement;
}
}
示例10: 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')));
}
}
示例11: resolveVariableType
/**
* @inheritDoc
*/
public function resolveVariableType(Node $node)
{
// $this->call()
if ($node instanceof Node\Expr\MethodCall && property_exists($node->var, 'name') && null === $node->getAttribute('guessedType', null)) {
if ('container' == $node->var->name && $this->isController($this->table->lookUp('this')->type())) {
$context = self::CONTAINER;
} else {
$context = $this->table->lookUp($node->var->name)->type();
}
$type = $this->getType($context, $node->name, $node);
if (null !== $type) {
$node->setAttribute('guessedType', $type);
}
}
// $x = $this->call()
if ($node instanceof Node\Expr\Assign && $node->var instanceof Node\Expr\Variable && $node->expr instanceof Node\Expr\MethodCall && property_exists($node->expr->var, 'name') && null === $node->getAttribute('guessedType', null)) {
if ('container' == $node->expr->var->name && $this->isController($this->table->lookUp('this')->type())) {
$context = self::CONTAINER;
} else {
$context = $this->table->lookUp($node->expr->var->name)->type();
}
$type = $this->getType($context, $node->expr->name, $node->expr);
if (null !== $type) {
$node->var->setAttribute('guessedType', $type);
$this->table->setSymbol($node->var->name, $type);
}
}
}
示例12: enterNode
public function enterNode(Node $node)
{
if (!$node instanceof Exit_) {
return;
}
$name = $node->getAttribute('kind') === Exit_::KIND_EXIT ? 'exit' : 'die';
$this->reporter->addViolation("Found {$name}() call", $this, $node);
}
示例13: leaveNode
public function leaveNode(Node $node)
{
if (!$node instanceof ClassMethod) {
return;
}
$pos = $node->getAttribute('startTokenPos');
MethodPatcher::$replacement[$pos] = true;
}
示例14: getCommentsText
private function getCommentsText(Node $node)
{
$text = '';
$comments = $node->getAttribute('comments');
foreach ($comments as $comment) {
$text .= $comment->getReformattedText();
}
return $text;
}
示例15: enterNode
/**
* {@inheritdoc}
*/
public function enterNode(Node $node)
{
if ($node instanceof Node\Scalar\LNumber) {
$originalValue = $node->getAttribute('originalValue');
if ($originalValue && $this->isInvalidOctalLiteral($originalValue)) {
$this->errorCollection->add(new Error($this->parserContext->getFilename(), $node->getLine(), sprintf('"%s" is an invalid octal literal and will now triggers a parse error.', $originalValue)));
}
}
}