public function test_Variable_for() { $refl = $this->getMockBuilder(Reflection::class)->disableOriginalConstructor()->getMock(); $for = new Stmt\For_(['init' => new Expr\Assign(new Expr\Variable('a'), new Expr\Variable('b'))], ['annotations' => ['var' => [Tag::get('var', 'int')]]]); $var1 = new Expr\Variable('a'); $this->infer([$for, $var1], $refl); $this->assertTrue($var1->getAttribute('type')->equals(Type::int_())); }
/** * @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; } }
/** * @param string $docComment * * @return array [short description string, long description string, Tag[][]] */ protected function parse($docComment) { $shortDescription = ''; $longDescription = ''; $annotations = []; $docBlock = trim(preg_replace(['~^/\\*\\*~', '~\\*/$~'], '', $docComment)); $current =& $shortDescription; $inShortDescription = true; foreach (explode("\n", $docBlock) as $line) { $line = preg_replace('~^\\*\\s?~', '', trim($line)); // End of short description. if ($line === '' && $inShortDescription) { $current =& $longDescription; $inShortDescription = false; continue; } // @annotation if (preg_match('~^\\s*@([\\S]+)\\s*(.*)~', $line, $matches)) { $name = $matches[1]; $current =& $annotations[$name][]; $current = $matches[2]; $inShortDescription = false; continue; } // Continuation. $current .= "\n" . $line; } array_walk_recursive($annotations, function (&$value) { $value = trim($value); }); foreach ($annotations as $aname => &$alist) { foreach ($alist as &$annot) { $annot = Tag::get($aname, $annot); unset($annot); } unset($alist); } return [trim($shortDescription) ?: null, trim($longDescription) ?: null, $annotations]; }