Пример #1
0
 /**
  * @param Element $element
  * @param Node    $node
  */
 protected function init(Element $element, Node $node)
 {
     $element->setName($node->hasAttribute('namespacedName') ? Type::nameToString($node->getAttribute('namespacedName')) : $node->name);
     if ($node->hasAttribute('startFilePos')) {
         $element->setLocation(new OffsetLocation($this->path, $node->getAttribute('startFilePos')));
     }
 }
Пример #2
0
 public function leaveNode(Node $node)
 {
     if (!$node instanceof Expr) {
         return;
     }
     $type = null;
     if ($node instanceof Expr\Assign || $node instanceof Expr\AssignRef) {
         $type = $node->expr->getAttribute('type');
     } elseif ($node instanceof Expr\Ternary) {
         $exprType1 = $node->if === null ? $node->cond->getAttribute('type') : $node->if->getAttribute('type');
         $exprType2 = $node->else->getAttribute('type');
         $type = Type::alternatives([$exprType1, $exprType2]);
     } elseif ($node instanceof Expr\BinaryOp\Coalesce) {
         $exprType1 = $node->left->getAttribute('type');
         $exprType2 = $node->right->getAttribute('type');
         $type = Type::alternatives([$exprType1, $exprType2]);
     } elseif ($node instanceof Expr\ErrorSuppress) {
         $type = $node->expr->getAttribute('type');
     } elseif ($node instanceof Expr\Eval_) {
         $type = Type::mixed_();
     } elseif ($node instanceof Expr\Exit_) {
         $type = Type::null_();
     }
     if ($type !== null || !$node->hasAttribute('type')) {
         if ($type === null) {
             $type = Type::mixed_();
         }
         $node->setAttribute('type', $type);
     }
 }
Пример #3
0
 public function enterNode(Node $node)
 {
     if ($node->hasAttribute('comments')) {
         $lastDocComment = null;
         foreach ($node->getAttribute('comments') as $comment) {
             if ($comment instanceof Comment\Doc) {
                 $lastDocComment = $comment;
             }
         }
         if ($lastDocComment) {
             list($shortDescription, $longDescription, $annotations) = $this->parse($lastDocComment->getText());
             if (!empty($shortDescription)) {
                 $node->setAttribute('shortDescription', $shortDescription);
             }
             if (!empty($longDescription)) {
                 $node->setAttribute('longDescription', $longDescription);
             }
             if (!empty($annotations)) {
                 $node->setAttribute('annotations', $annotations);
             }
         }
     }
 }
Пример #4
0
 public function enterNode(Node $node)
 {
     if ($node->hasAttribute('annotations')) {
         foreach ($node->getAttribute('annotations') as $annotations) {
             foreach ($annotations as $docTag) {
                 if ($docTag instanceof TypedTag) {
                     $docTag->setType($this->resolveDocTagType($docTag->getType()));
                 }
             }
         }
     }
     $result = parent::enterNode($node);
     if ($node instanceof Stmt\Class_) {
         $this->currentClass = $node->getAttribute('namespacedName');
         $this->parentClass = null;
         if ($node->extends !== null) {
             $this->parentClass = $node->extends->getAttribute('resolved');
         }
     } elseif ($node instanceof Stmt\Interface_) {
         $this->currentClass = $node->getAttribute('namespacedName');
         $this->parentClass = null;
     }
     return $result;
 }
Пример #5
0
 public function enterNode(Node $node)
 {
     if (!$node instanceof Stmt\Property) {
         $annotations = [];
         if ($node->hasAttribute('annotations')) {
             $annotations = $node->getAttribute('annotations');
         }
         if (!empty($annotations['var'])) {
             foreach ($annotations['var'] as $varTag) {
                 $name = $varTag->getIdentifier();
                 if (empty($name)) {
                     $var = $node;
                     if ($var instanceof Stmt\Foreach_) {
                         $var = $var->valueVar;
                     } elseif ($var instanceof Stmt\For_) {
                         $var = $var->init;
                     } elseif ($var instanceof Stmt\Global_ && count($var->vars) === 1) {
                         $var = $var->vars[0];
                     }
                     if ($var instanceof Expr\Assign || $var instanceof Expr\AssignRef || $var instanceof Expr\AssignOp) {
                         $var = $var->var;
                     }
                     if ($var instanceof Expr\Variable && is_string($var->name)) {
                         $name = '$' . $var->name;
                     }
                 }
                 if (!empty($name)) {
                     $this->getCurrentFunctionScope()[$name] = $varTag->getType();
                 }
             }
         }
     }
     if ($node instanceof Name) {
         $self = $this->getCurrentClass();
         if (!empty($self) && !empty($self->getClass())) {
             $resolved = null;
             // Treating `static` as `self` is the best we can do here.
             if ($node->toString() === 'self' || $node->toString() === 'static') {
                 $resolved = $self->getClass();
             } elseif ($node->toString() === 'parent') {
                 $selfClass = $this->reflection->findClass($self->getClass());
                 if (!empty($selfClass) && $selfClass[0] instanceof Class_ && !empty($selfClass[0]->getExtends())) {
                     $resolved = $selfClass[0]->getExtends();
                 }
             }
             if (!empty($resolved)) {
                 $node->setAttribute('resolved', new Name\FullyQualified(ltrim($resolved, '\\'), $node->getAttributes()));
             }
         }
     } elseif ($node instanceof Stmt\Function_) {
         $scope = [];
         $functions = $this->reflection->findFunction($node->hasAttribute('namespacedName') ? Type::nameToString($node->getAttribute('namespacedName')) : $node->name);
         if (!empty($functions)) {
             foreach ($functions[0]->getParams() as $param) {
                 $scope[$param->getName()] = $param->getDocType();
             }
         }
         $this->functionScopeStack[] = $scope;
     } elseif ($node instanceof Stmt\ClassMethod) {
         $scope = [];
         $class = $this->getCurrentClass();
         if (!empty($class) && !empty($class->getClass())) {
             $method = $this->reflection->findMethod($class->getClass(), $node->name);
             if (!empty($method)) {
                 foreach ($method->getParams() as $param) {
                     $scope[$param->getName()] = $param->getDocType();
                 }
             }
         }
         $this->functionScopeStack[] = $scope;
     } elseif ($node instanceof Expr\Closure) {
         $scope = [];
         $parentScope =& $this->getCurrentFunctionScope();
         foreach ($node->uses as $use) {
             if (array_key_exists('$' . $use->var, $parentScope)) {
                 $scope['$' . $use->var] = $parentScope['$' . $use->var];
             }
         }
         foreach ($node->params as $param) {
             $scope['$' . $param->name] = Type::fromString(Type::nameToString($param->type));
         }
         $this->functionScopeStack[] = $scope;
     } elseif ($node instanceof Stmt\ClassLike) {
         $className = $node->hasAttribute('namespacedName') ? Type::nameToString($node->getAttribute('namespacedName')) : $node->name;
         $this->classStack[] = Type::object_($className);
         // for isolation, in case of illegal statements in class def:
         $this->functionScopeStack[] = [];
     }
 }