本文整理汇总了PHP中PhpParser\Node::getAttributes方法的典型用法代码示例。如果您正苦于以下问题:PHP Node::getAttributes方法的具体用法?PHP Node::getAttributes怎么用?PHP Node::getAttributes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PhpParser\Node
的用法示例。
在下文中一共展示了Node::getAttributes方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: enterNode
/**
* Swap out __DIR__ and __FILE__ constants, because the default ones when
* calling eval() don't make sense.
*
* @param Node $node
*
* @return null|FuncCall|StringNode
*/
public function enterNode(Node $node)
{
if ($node instanceof Dir) {
return new FuncCall(new Name('getcwd'), array(), $node->getAttributes());
} elseif ($node instanceof File) {
return new StringNode('', $node->getAttributes());
}
}
示例2: resolveStack
protected function resolveStack(Node $node, array $stack)
{
if (!$node->num) {
return new Goto_(end($stack), $node->getAttributes());
}
if ($node->num instanceof LNumber) {
$num = $node->num->value - 1;
if ($num >= count($stack)) {
throw new \LogicException("Too high of a count for " . $node->getType());
}
$loc = array_slice($stack, -1 * $num, 1);
return new Goto_($loc[0], $node->getAttributes());
}
throw new \LogicException("Unimplemented Node Value Type");
}
示例3: leaveNode
/**
* {@inheritdoc}
*/
public function leaveNode(Node $node)
{
if (!$node instanceof Spaceship) {
return;
}
/*
* Replacing
* $a <=> $b
* with
* $a < $b ? -1 : ($a == $b ? 0 : 1)
*/
$attributes = $node->getAttributes();
$smaller = new UnaryMinus(new LNumber(1, $attributes), $attributes);
$equal = new LNumber(0, $attributes);
$larger = new LNumber(1, $attributes);
$isEqual = new Equal($node->left, $node->right, $attributes);
$isSmaller = new Smaller($node->left, $node->right, $attributes);
$else = new Ternary($isEqual, $equal, $larger, $attributes);
return new Ternary($isSmaller, $smaller, $else, $attributes);
}
示例4: enterNode
public function enterNode(Node $node)
{
/*if ($node instanceof Node\Name)
{
//echo "Node nameD:". $node."\n\n";
return new Node\Name($node->toString('_'));
}
|| $node instanceof Stmt\Function_
*/
if ($node instanceof Stmt\Function_) {
array_push($this->functions, array("name" => $node->name, "position" => $node->getAttributes()));
} else {
if ($node instanceof Stmt\Interface_) {
if (IS_DEBUG) {
echo "\nNode name:" . $node->name . "\n\n";
}
//print_r($node);
$this->addItem($node, 'interface');
//$node->name = $node->namespacedName->toString('_');
} elseif ($node instanceof Stmt\Trait_) {
$this->addItem($node, 'trait');
//List all implements
//List all classes
//This class can use those functions as own
//Parse and get those functions also
if (IS_DEBUG) {
echo "\\Trait name:" . $node->name . "\n\n";
}
} elseif ($node instanceof Stmt\TraitUse) {
/* echo "\Trait name:". $node->traits."\n\n";*/
///FIXME implement it here
foreach ($node->traits as $trait) {
//print_r($trait);
}
} elseif ($node instanceof Stmt\Class_) {
//List all implements
//List all classes
//This class can use those functions as own
//Parse and get those functions also
if (IS_DEBUG) {
echo "\nClass name:" . $node->name . "\n\n";
}
$this->addItem($node, 'class');
/* $this->result['class'] = $node->name;*/
} elseif ($node instanceof Stmt\ClassMethod) {
if (IS_DEBUG) {
echo "\nClass method:" . $node->name . "\n\n";
}
//$this->result[$this->className]['methods'][] = $node->name;
array_push($this->objects[$this->className]['methods'], array("name" => $node->name, "position" => $node->getAttributes()));
//$this->classMethods[] = $node->name;
//print_r($node);
} elseif ($node instanceof Stmt\Const_) {
/*foreach ($node->consts as $const) {
$const->name = $const->namespacedName->toString('_');
}*/
} elseif ($node instanceof Stmt\PropertyProperty) {
//print_r($node);
array_push($this->objects[$this->className]['variables'], array("name" => $node->name, "position" => $node->getAttributes()));
/*foreach ($node->items as $prop) {
echo "Prop: ";
print_r($prop);
}*/
} elseif ($node instanceof Stmt\Namespace_) {
$this->namespace_ = array("name" => str_replace("\\", "/", $node->name), "position" => $node->getAttributes());
/* $this->namespace_ = $this->namespace_ );*/
if (IS_DEBUG) {
echo "\nNode Namespace_ name:" . $node->name . "\n\n";
}
// returning an array merges is into the parent array
//return $node->stmts;
} elseif ($node instanceof Stmt\Use_) {
//print_r($node);
if (IS_DEBUG) {
echo "\nNode Use_ Namespace_ name:";
}
//. $node->uses[0]->name."\n\n";
foreach ($node->uses as $use) {
//Name is using _ as seperator
if (IS_DEBUG) {
echo $use->name . " | " . $use->alias . "\n\n";
echo "\nClass method:" . $use->name . "\n\n";
}
//$this->result[$this->className]['methods'][] = $node->name;
array_push($this->use_, array("name" => $use->name->parts, "alias" => $use->alias, "position" => $node->getAttributes()));
}
// returning false removed the node altogether
//return false;
}
}
}
示例5: addNamespacedName
protected function addNamespacedName(Node $node)
{
if (null !== $this->namespace) {
$node->namespacedName = clone $this->namespace;
$node->namespacedName->append($node->name);
} else {
$node->namespacedName = new Name($node->name, $node->getAttributes());
}
}
示例6: exchange
/**
* @param Node\Name $target
* @param Node $source
*/
private function exchange(Node\Name $target, Node $source)
{
$attributes = $source->getAttributes();
foreach ($attributes as $attr => $value) {
$target->setAttribute($attr, $value);
}
}
示例7: leaveNode
/** Examine the current PhpParser_Node node against the PHPSandbox configuration for validating sandboxed code
*
* @param Node $node The sandboxed $node to validate
*
* @throws Error Throws an exception if validation fails
*
* @return Node|bool|null Return rewritten node, false if node must be removed, or null if no changes to the node are made
*/
public function leaveNode(Node $node)
{
if ($node instanceof Node\Arg) {
return new Node\Expr\FuncCall(new Node\Name\FullyQualified($node->value instanceof Node\Expr\Variable ? 'PHPSandbox\\wrapByRef' : 'PHPSandbox\\wrap'), [$node, new Node\Expr\StaticCall(new Node\Name\FullyQualified("PHPSandbox\\PHPSandbox"), 'getSandbox', [new Node\Scalar\String_($this->sandbox->name)])], $node->getAttributes());
} else {
if ($node instanceof Node\Stmt\InlineHTML) {
if (!$this->sandbox->allow_escaping) {
$this->sandbox->validationError("Sandboxed code attempted to escape to HTML!", Error::ESCAPE_ERROR, $node);
}
} else {
if ($node instanceof Node\Expr\Cast) {
if (!$this->sandbox->allow_casting) {
$this->sandbox->validationError("Sandboxed code attempted to cast!", Error::CAST_ERROR, $node);
}
if ($node instanceof Node\Expr\Cast\Int_) {
return new Node\Expr\MethodCall(new Node\Expr\StaticCall(new Node\Name\FullyQualified("PHPSandbox\\PHPSandbox"), 'getSandbox', [new Node\Scalar\String_($this->sandbox->name)]), '_intval', [new Node\Arg($node->expr)], $node->getAttributes());
} else {
if ($node instanceof Node\Expr\Cast\Double) {
return new Node\Expr\MethodCall(new Node\Expr\StaticCall(new Node\Name\FullyQualified("PHPSandbox\\PHPSandbox"), 'getSandbox', [new Node\Scalar\String_($this->sandbox->name)]), '_floatval', [new Node\Arg($node->expr)], $node->getAttributes());
} else {
if ($node instanceof Node\Expr\Cast\Bool_) {
return new Node\Expr\MethodCall(new Node\Expr\StaticCall(new Node\Name\FullyQualified("PHPSandbox\\PHPSandbox"), 'getSandbox', [new Node\Scalar\String_($this->sandbox->name)]), '_boolval', [new Node\Arg($node->expr)], $node->getAttributes());
} else {
if ($node instanceof Node\Expr\Cast\Array_) {
return new Node\Expr\MethodCall(new Node\Expr\StaticCall(new Node\Name\FullyQualified("PHPSandbox\\PHPSandbox"), 'getSandbox', [new Node\Scalar\String_($this->sandbox->name)]), '_arrayval', [new Node\Arg($node->expr)], $node->getAttributes());
} else {
if ($node instanceof Node\Expr\Cast\Object_) {
return new Node\Expr\MethodCall(new Node\Expr\StaticCall(new Node\Name\FullyQualified("PHPSandbox\\PHPSandbox"), 'getSandbox', [new Node\Scalar\String_($this->sandbox->name)]), '_objectval', [new Node\Arg($node->expr)], $node->getAttributes());
}
}
}
}
}
} else {
if ($node instanceof Node\Expr\FuncCall) {
if ($node->name instanceof Node\Name) {
$name = strtolower($node->name->toString());
if (!$this->sandbox->checkFunc($name)) {
$this->sandbox->validationError("Function failed custom validation!", Error::VALID_FUNC_ERROR, $node);
}
if ($this->sandbox->isDefinedFunc($name)) {
$args = $node->args;
array_unshift($args, new Node\Arg(new Node\Scalar\String_($name)));
return new Node\Expr\MethodCall(new Node\Expr\StaticCall(new Node\Name\FullyQualified("PHPSandbox\\PHPSandbox"), 'getSandbox', [new Node\Scalar\String_($this->sandbox->name)]), 'call_func', $args, $node->getAttributes());
}
if ($this->sandbox->overwrite_defined_funcs && in_array($name, PHPSandbox::$defined_funcs)) {
return new Node\Expr\MethodCall(new Node\Expr\StaticCall(new Node\Name\FullyQualified("PHPSandbox\\PHPSandbox"), 'getSandbox', [new Node\Scalar\String_($this->sandbox->name)]), '_' . $name, [new Node\Arg(new Node\Expr\FuncCall(new Node\Name([$name])))], $node->getAttributes());
}
if ($this->sandbox->overwrite_sandboxed_string_funcs && in_array($name, PHPSandbox::$sandboxed_string_funcs)) {
$args = $node->args;
return new Node\Expr\MethodCall(new Node\Expr\StaticCall(new Node\Name\FullyQualified("PHPSandbox\\PHPSandbox"), 'getSandbox', [new Node\Scalar\String_($this->sandbox->name)]), '_' . $name, $args, $node->getAttributes());
}
if ($this->sandbox->overwrite_func_get_args && in_array($name, PHPSandbox::$arg_funcs)) {
if ($name == 'func_get_arg') {
$index = new Node\Arg(new Node\Scalar\LNumber(0));
if (isset($node->args[0]) && $node->args[0] instanceof Node\Arg) {
$index = $node->args[0];
}
return new Node\Expr\MethodCall(new Node\Expr\StaticCall(new Node\Name\FullyQualified("PHPSandbox\\PHPSandbox"), 'getSandbox', [new Node\Scalar\String_($this->sandbox->name)]), '_' . $name, [new Node\Arg(new Node\Expr\FuncCall(new Node\Name(['func_get_args']))), $index], $node->getAttributes());
}
return new Node\Expr\MethodCall(new Node\Expr\StaticCall(new Node\Name\FullyQualified("PHPSandbox\\PHPSandbox"), 'getSandbox', [new Node\Scalar\String_($this->sandbox->name)]), '_' . $name, [new Node\Arg(new Node\Expr\FuncCall(new Node\Name(['func_get_args'])))], $node->getAttributes());
}
} else {
return new Node\Expr\Ternary(new Node\Expr\MethodCall(new Node\Expr\StaticCall(new Node\Name\FullyQualified("PHPSandbox\\PHPSandbox"), 'getSandbox', [new Node\Scalar\String_($this->sandbox->name)]), 'check_func', [new Node\Arg($node->name)], $node->getAttributes()), $node, new Node\Expr\ConstFetch(new Node\Name('null')));
}
} else {
if ($node instanceof Node\Stmt\Function_) {
if (!$this->sandbox->allow_functions) {
$this->sandbox->validationError("Sandboxed code attempted to define function!", Error::DEFINE_FUNC_ERROR, $node);
}
if (!$this->sandbox->checkKeyword('function')) {
$this->sandbox->validationError("Keyword failed custom validation!", Error::VALID_KEYWORD_ERROR, $node, 'function');
}
if (!$node->name) {
$this->sandbox->validationError("Sandboxed code attempted to define unnamed function!", Error::DEFINE_FUNC_ERROR, $node, '');
}
if ($this->sandbox->isDefinedFunc($node->name)) {
$this->sandbox->validationError("Sandboxed code attempted to redefine function!", Error::DEFINE_FUNC_ERROR, $node, $node->name);
}
if ($node->byRef && !$this->sandbox->allow_references) {
$this->sandbox->validationError("Sandboxed code attempted to define function return by reference!", Error::BYREF_ERROR, $node);
}
} else {
if ($node instanceof Node\Expr\Closure) {
if (!$this->sandbox->allow_closures) {
$this->sandbox->validationError("Sandboxed code attempted to create a closure!", Error::CLOSURE_ERROR, $node);
}
} else {
if ($node instanceof Node\Stmt\Class_) {
if (!$this->sandbox->allow_classes) {
$this->sandbox->validationError("Sandboxed code attempted to define class!", Error::DEFINE_CLASS_ERROR, $node);
}
//.........这里部分代码省略.........
示例8: mapAttributes
private function mapAttributes(Node $expr)
{
return array_merge(["filename" => $this->fileName, "doccomment" => $expr->getDocComment()], $expr->getAttributes());
}
示例9: fetchNode
/**
* Fetch Node line contents
*
* @param Node $node
* @return string[]
*/
public function fetchNode(Node $node)
{
$attr = $node->getAttributes();
return $this->fetchLines($attr['startLine'], $attr['endLine']);
}
示例10: mapAttributes
private function mapAttributes(Node $expr)
{
return array_merge(["filename" => $this->fileName], $expr->getAttributes());
}
示例11: mapAttributes
private function mapAttributes(Node $expr)
{
return $expr->getAttributes();
}