예제 #1
0
 /**
  * @param string $name
  * @param string $text
  */
 protected function __construct($name, $text)
 {
     parent::__construct($name, $text);
     $text = trim($text);
     $parts = preg_split('~\\s+~', trim($text), 2);
     $this->type = Type::fromString($parts[0]);
     if (count($parts) >= 2) {
         $this->description = $parts[1] ?: null;
     }
 }
예제 #2
0
 /**
  * @param Name|string $type
  *
  * @return Type
  */
 private function getType($type)
 {
     return Type::fromString(Type::nameToString($type));
 }
예제 #3
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[] = [];
     }
 }
예제 #4
0
 protected function getType($typeString)
 {
     if (array_key_exists($typeString, static::TYPE_ALIASES)) {
         $typeString = static::TYPE_ALIASES[$typeString];
     }
     return empty($typeString) ? Type::mixed_() : Type::fromString($typeString);
 }