Inheritance: extends PhpParser\NodeAbstract
Exemple #1
0
 protected function resolveOtherName(Name $name, $type)
 {
     if (!$name instanceof FullyQualified) {
         $name->setAttribute('resolvedFqcn', parent::resolveOtherName($name, $type));
     }
     return $name;
 }
Exemple #2
0
 /**
  * Constructs an alias (use) node.
  *
  * @param Node\Name   $name       Namespace/Class to alias
  * @param null|string $alias      Alias
  * @param array       $attributes Additional attributes
  */
 public function __construct(Node\Name $name, $alias = null, array $attributes = array())
 {
     if (null === $alias) {
         $alias = $name->getLast();
     }
     if ('self' == $alias || 'parent' == $alias) {
         throw new Error(sprintf('Cannot use %s as %s because \'%2$s\' is a special class name', $name, $alias));
     }
     parent::__construct(array('name' => $name, 'alias' => $alias), $attributes);
 }
Exemple #3
0
 /**
  * Constructs an alias (use) node.
  *
  * @param Node\Name   $name       Namespace/Class to alias
  * @param null|string $alias      Alias
  * @param int         $type       Type of the use element (for mixed group use declarations only)
  * @param array       $attributes Additional attributes
  */
 public function __construct(Node\Name $name, $alias = null, $type = Use_::TYPE_UNKNOWN, array $attributes = array())
 {
     if (null === $alias) {
         $alias = $name->getLast();
     }
     parent::__construct($attributes);
     $this->type = $type;
     $this->name = $name;
     $this->alias = $alias;
 }
 protected function resolveClassName(PhpParser\Node\Name $name)
 {
     if ($name->isFullyQualified()) {
         return $name;
     }
     $old = $name->toString();
     $resolved = parent::resolveClassName($name);
     $this->resolvedClasses[$old] = $resolved->toString();
     return $resolved;
 }
 /**
  * {@inheritdoc}
  */
 public function isValidBetween(Name $from, Name $to)
 {
     $this->lastResult = $this->inspector->isAllowed($from->toString(), $to->toString());
     if (!is_null($this->collector) && $this->lastResult->isDenied()) {
         $this->collector->major(new PolicyViolation($from, $to, $this->lastResult));
     }
     if (!is_null($this->collector) && $this->lastResult->isUndefined()) {
         $this->collector->undefined(new PolicyViolation($from, $to, $this->lastResult));
     }
     return $this->lastResult->isAllowed();
 }
 public function leaveNode(Node $node)
 {
     if ($node->hasAttribute(NameResolver::TAG_NAMES_ATTRIBUTE)) {
         $tags = $node->getAttribute(NameResolver::TAG_NAMES_ATTRIBUTE);
         foreach ($tags as $tagName) {
             $name = new Node\Name($tagName);
             $name->setAttribute('isComment', true);
             $this->collect($name, $node);
         }
     }
 }
Exemple #7
0
 /**
  * Constructs an alias (use) node.
  *
  * @param Node\Name $name Namespace/Class to alias
  * @param null|string $alias Alias
  * @param array $attributes Additional attributes
  */
 public function __construct(Node\Name $name, $alias = null, array $attributes = array())
 {
     if (null === $alias) {
         $alias = $name->getLast();
     }
     if ('self' == strtolower($alias) || 'parent' == strtolower($alias)) {
         throw new Error(sprintf('Cannot use %s as %s because \'%2$s\' is a special class name', $name, $alias));
     }
     parent::__construct(null, $attributes);
     $this->name = $name;
     $this->alias = $alias;
 }
 /**
  * @param SplFileInfo $file
  * @param Name        $name
  * @throws \DomainException
  */
 public function __construct(SplFileInfo $file, Name $name)
 {
     $attributes = $name->getAttributes();
     if (!isset($attributes['startLine']) || !isset($attributes['endLine'])) {
         throw new \DomainException('Values for startLine and/or endLine are not set in attributes');
     }
     $this->file = $file;
     $this->startLine = (int) $attributes['startLine'];
     $this->endline = (int) $attributes['endLine'];
     if (isset($attributes['isComment'])) {
         $this->isComment = (bool) $attributes['isComment'];
     }
 }
 private function checkIfTypeIsAnInterface(Node $node, Name $type)
 {
     $name = (string) $type;
     if ($type->isFullyQualified() === false) {
         $name = $this->currentNamespace . '\\' . $name;
     }
     if (isset($this->types[$name])) {
         $objectTypeType = $this->types[$name];
         if ($objectTypeType !== ObjectType::TYPE_INTERFACE) {
             $defect = new DependencyUponImplementation($node, $name, $objectTypeType, $this->currentMethod->name);
             $defect->setContext($this->currentMethod);
             $this->dispatch($defect);
         }
     }
 }
Exemple #10
0
 private function checkFunction(Name $name, FuncCall $node)
 {
     // check for a function inside the class namespace as well as imported
     // functions first.
     $func = $this->getContext()->getClassName($name->toString());
     if (!function_exists($func)) {
         // get the plain function name without namespaces
         $func = $name->toString();
     }
     // check if the function exists in the global namespace
     if (!function_exists($func)) {
         $this->addError($this->createUndefinedFunctionError($func, $node));
         return;
     }
     $refl = new ReflectionFunction($func);
     $params = $refl->getParameters();
     // it's not possible to check for default values/optionalness etc. on
     // internal functions
     if (!$refl->isInternal()) {
         // verify number of arguments
         if (count($node->args) > count($params)) {
             // cannot error on this as php functions can use func_get_args()
         }
         $requiredParams = 0;
         foreach ($params as $param) {
             if ($param->isOptional() || $param->isDefaultValueAvailable()) {
                 break;
             }
             $requiredParams++;
         }
         if (count($node->args) < $requiredParams) {
             $this->addError($this->createNotEnoughParamsError($node, $func, $requiredParams));
         }
     }
     // look for function parameters passed by reference
     foreach ($params as $param) {
         if ($param->isPassedByReference()) {
             $pos = $param->getPosition();
             if (isset($node->args[$pos])) {
                 $var = $node->args[$pos]->value;
                 if ($var instanceof Variable) {
                     $this->getContext()->setVariable($var->name, $var);
                 }
             }
         }
     }
 }
 protected function addAlias(Stmt\UseUse $use, $type, Name $prefix = null)
 {
     parent::addAlias($use, $type, $prefix);
     if ($type == Stmt\Use_::TYPE_NORMAL) {
         // Add prefix for group uses
         $name = strval($prefix ? Name::concat($prefix, $use->name) : $use->name);
         $this->classAliases[$use->alias] = $name;
     }
 }
Exemple #12
0
 public function getFQCN(Name $node = null)
 {
     if ($node === null) {
         return $node;
     }
     if ($node->isFullyQualified()) {
         return $this->parseFQCN($node->toString());
     }
     $fqcn = $this->uses->find($node->getFirst());
     if ($fqcn) {
         return $fqcn;
     }
     return $this->createFQCN($node->toString());
 }
Exemple #13
0
 /**
  * @param Name|string $name
  *
  * @return string
  */
 private function getFullClassName($name)
 {
     if ($name instanceof Name) {
         if ($name->isFullyQualified()) {
             return $name->toString();
         }
     }
     $name = (string) $name;
     if (isset($this->namespace->uses[$name])) {
         return $this->namespace->uses[$name];
     }
     if ($this->namespace->name) {
         return $this->namespace->name . '\\' . $name;
     }
     return $name;
 }
 protected function addNamespacedName(Node $node)
 {
     if (null !== $this->namespace) {
         $node->namespacedName = Name::concat($this->namespace, $node->name);
     } else {
         $node->namespacedName = new Name($node->name);
     }
 }
Exemple #15
0
 protected function parseAbsoluteName(Node\Name $node)
 {
     return ($node->isFullyQualified() ? '\\' : '') . (string) $node;
 }
Exemple #16
0
 /**
  * @param Node\Name $expr
  * @return CompiledExpression
  */
 public function getNodeName(Node\Name $expr)
 {
     $nodeString = $expr->toString();
     if ($nodeString === 'null') {
         return new CompiledExpression(CompiledExpression::NULL);
     }
     if (in_array($nodeString, ['parent'], true)) {
         /** @var ClassDefinition $scope */
         $scope = $this->context->scope;
         assert($scope instanceof ClassDefinition);
         if ($scope->getExtendsClass()) {
             $definition = $scope->getExtendsClassDefinition();
             if ($definition) {
                 return new CompiledExpression(CompiledExpression::OBJECT, $definition);
             }
         } else {
             $this->context->notice('language_error', 'Cannot access parent:: when current class scope has no parent', $expr);
         }
     }
     if (in_array($nodeString, ['self', 'static'], true)) {
         return CompiledExpression::fromZvalValue($this->context->scope);
     }
     if (defined($nodeString)) {
         return CompiledExpression::fromZvalValue(constant($expr));
     }
     return new CompiledExpression(CompiledExpression::STRING, $expr->toString());
 }
 public function enterNode(Node $node)
 {
     if ($node instanceof UseUse) {
         $node->name = Name::concat($this->prefix, $node->name);
     }
 }
 protected function resolveOtherName(Name $name, $type)
 {
     // fully qualified names are already resolved
     if ($name->isFullyQualified()) {
         return $name;
     }
     // resolve aliases for qualified names
     $aliasName = strtolower($name->getFirst());
     if ($name->isQualified() && isset($this->aliases[Stmt\Use_::TYPE_NORMAL][$aliasName])) {
         $name->setFirst($this->aliases[Stmt\Use_::TYPE_NORMAL][$aliasName]);
     } elseif ($name->isUnqualified()) {
         if ($type === Stmt\Use_::TYPE_CONSTANT) {
             // constant aliases are case-sensitive, function aliases case-insensitive
             $aliasName = $name->getFirst();
         }
         if (isset($this->aliases[$type][$aliasName])) {
             // resolve unqualified aliases
             $name->set($this->aliases[$type][$aliasName]);
         } else {
             // unqualified, unaliased names cannot be resolved at compile-time
             return $name;
         }
     } elseif (null !== $this->namespace) {
         // if no alias exists prepend current namespace
         $name->prepend($this->namespace);
     }
     return new Name\FullyQualified($name->parts, $name->getAttributes());
 }
 /**
  * @param Node\Name $namespacedString
  */
 public function addNamespacedString(Node\Name $namespacedString)
 {
     $this->namespacedStrings[$namespacedString->toString()] = $namespacedString;
 }
 /**
  * Utility method to fetch reflection class instance by name
  *
  * Supports:
  *   'self' keyword
  *   'parent' keyword
  *    not-FQN class names
  *
  * @param Node\Name $node Class name node
  *
  * @return bool|\ReflectionClass
  *
  * @throws ReflectionException
  */
 private function fetchReflectionClass(Node\Name $node)
 {
     $className = $node->toString();
     $isFQNClass = $node instanceof Node\Name\FullyQualified;
     if ($isFQNClass) {
         return new ReflectionClass($className);
     }
     if ('self' === $className) {
         if ($this->context instanceof \ReflectionClass) {
             return $this->context;
         } elseif (method_exists($this->context, 'getDeclaringClass')) {
             return $this->context->getDeclaringClass();
         }
     }
     if ('parent' === $className) {
         if ($this->context instanceof \ReflectionClass) {
             return $this->context->getParentClass();
         } elseif (method_exists($this->context, 'getDeclaringClass')) {
             return $this->context->getDeclaringClass()->getParentClass();
         }
     }
     if (method_exists($this->context, 'getFileName')) {
         /** @var ReflectionFileNamespace|null $fileNamespace */
         $fileName = $this->context->getFileName();
         $namespaceName = $this->context->getNamespaceName();
         $fileNamespace = new ReflectionFileNamespace($fileName, $namespaceName);
         return $fileNamespace->getClass($className);
     }
     throw new ReflectionException("Can not resolve class {$className}");
 }
Exemple #21
0
 /**
  * @param Name $name
  * @return string
  */
 protected function resolveName(Name $name)
 {
     $parts = $name->parts;
     if (!$name->isFullyQualified()) {
         /** @var NULL|UseUse $lastMatch */
         $lastMatch = NULL;
         foreach ($this->uses as $use) {
             foreach ($use->uses as $useuse) {
                 if ($useuse->alias === $parts[0]) {
                     $lastMatch = $useuse;
                 }
             }
         }
         if (NULL !== $lastMatch) {
             $parts[0] = join('\\', $lastMatch->name->parts);
         } else {
             if (!empty($this->namespace)) {
                 # No alias in 'use' matching, it's in the current namespace, if present
                 array_unshift($parts, $this->namespace);
             }
         }
     }
     return join('\\', $parts);
 }
Exemple #22
0
 /**
  * Prepare a name
  *
  * If the first argument is an instance of `PhpParser\Node\Name` its string representation
  * will be returned as first element of the result array. The value retrieved via `PhpParser\Node\Name::getLine()`
  * will be used as second element.
  * If the first parameter is not an instance of `PhpParser\Node\Name` it will be casted to string and returned alongside
  * with the value given for the second parameter as line
  *
  * @param \PhpParser\Node\Name|string $name
  * @param int $line Only used if the $name parameter is not an instance of `PhpParser\Node\Name`
  * @return array
  */
 private function prepareNameAndLine($name, $line = -1)
 {
     if ($name instanceof Node\Name) {
         $line = $name->getLine();
         $name = $name->toString();
     } else {
         $name = (string) $name;
     }
     return [$name, $line];
 }
 /**
  * @param NodeInterface\Name $trait
  */
 public function addTraitUseAlias(TraitUseAliasNode $adaptation, $trait)
 {
     if (is_null($adaptation->trait)) {
         $this->context->getReflection()->addTraitMethodAlias((string) $trait->toString(), (string) $adaptation->method, (string) $adaptation->newName);
     } else {
         $this->context->getReflection()->addTraitMethodAlias((string) $adaptation->trait->toString(), (string) $adaptation->method, (string) $adaptation->newName);
     }
 }
Exemple #24
0
 /**
  * @param Node\Name $usedTraitNamespace
  */
 public function addUsedTraitNamespace(Node\Name $usedTraitNamespace)
 {
     $this->usedTraitNamespaces[$usedTraitNamespace->toString()] = $usedTraitNamespace;
 }
 /**
  * @param Name $name
  * @return Vertex
  */
 private function createVertexBy(Name $name)
 {
     $vertex = $this->getGraph()->createVertex($name->toString(), true);
     if ($groupId = $this->groupGenerator->getIdFor($name)) {
         $vertex->setGroup($groupId);
         $vertex->setAttribute('graphviz.group', $groupId);
     }
     $this->bindLayoutTo($vertex, $this->layout->getVertex());
     return $vertex;
 }
Exemple #26
0
 protected function addNamespacedName(Node $node) {
     $node->namespacedName = Name::concat($this->namespace, $node->name);
 }
 /**
  * @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);
     }
 }
Exemple #28
0
 protected function parseExpr_FuncCall(Expr\FuncCall $expr)
 {
     $args = $this->parseExprList($expr->args, self::MODE_READ);
     $name = $this->parseExprNode($expr->name);
     if ($this->currentNamespace && $expr->name instanceof Node\Name && $expr->name->isUnqualified()) {
         $op = new Op\Expr\NsFuncCall($name, $this->parseExprNode(Node\Name::concat($this->currentNamespace, $expr->name)), $args, $this->mapAttributes($expr));
     } else {
         $op = new Op\Expr\FuncCall($name, $args, $this->mapAttributes($expr));
     }
     if ($name instanceof Operand\Literal) {
         static $assertionFunctions = ['is_array' => 'array', 'is_bool' => 'bool', 'is_callable' => 'callable', 'is_double' => 'float', 'is_float' => 'float', 'is_int' => 'int', 'is_integer' => 'int', 'is_long' => 'int', 'is_null' => 'null', 'is_numeric' => 'numeric', 'is_object' => 'object', 'is_real' => 'float', 'is_string' => 'string', 'is_resource' => 'resource'];
         $lname = strtolower($name->value);
         if (isset($assertionFunctions[$lname])) {
             $op->result->addAssertion($args[0], new Assertion\TypeAssertion(new Operand\Literal($assertionFunctions[$lname])));
         }
     }
     return $op;
 }
 protected function handleScalarTypes(Name $name)
 {
     $scalarTypes = ['bool' => true, 'int' => true, 'float' => true, 'string' => true];
     if (!$name->isUnqualified()) {
         return $name;
     }
     $lowerName = strtolower($name->toString());
     return isset($scalarTypes[$lowerName]) ? $lowerName : $name;
 }
Exemple #30
0
 /**
  * @param \PhpParser\Node\Name $nameNode
  * @param \PHPStan\Analyser\Scope|null $scope
  * @return string|null
  */
 public function resolveFunctionName(\PhpParser\Node\Name $nameNode, Scope $scope = null)
 {
     $name = (string) $nameNode;
     if ($scope !== null && $scope->getNamespace() !== null && !$nameNode->isFullyQualified()) {
         $namespacedName = sprintf('%s\\%s', $scope->getNamespace(), $name);
         if (function_exists($namespacedName)) {
             return $namespacedName;
         }
     }
     if (function_exists($name)) {
         return $name;
     }
     return null;
 }