public function testParseHome() { $homeanno = AnnotationParser::parse($this->homeAnno); foreach ($homeanno as $name => &$params) { $params = array_map(function ($p) { return $p ? $p() : null; }, $params); } $this->assertEquals(['get' => [['/path']], 'auth' => [null], 'bad' => [[]]], $homeanno); }
/** * * * @param string $docBlock * @return Annotation[] * @throws \InvalidArgumentException */ public function parse($docBlock) { $this->init($docBlock); $lines = preg_split('/[\\r\\n|\\r|\\n]/', $docBlock, null, \PREG_SPLIT_NO_EMPTY); $annotatedLines = array(); foreach ($lines as $line) { $line = ltrim($line, "\t *"); if (0 !== strpos($line, '@')) { continue; } $annotatedLines[] = $line; } if (count($annotatedLines) < 1) { return array(); } $annotations = array(); foreach ($annotatedLines as $annotatedLine) { $annotations[] = $this->annotationParser->parse($annotatedLine); } return $annotations; }
private function advance() { // Hacky. Whenever we advance we check for a doc comment, and if it's there // we parse it for annotations and stash them. I'd previously had this check // in the s() method for skipping ignored tokens but that didn't work outwith // a class definition because phpx is essentially dumb and just passes through // any tokens it's not interested in. if (is_array($this->curr) && $this->curr[0] == T_DOC_COMMENT) { $this->last_annotation = null; try { $parser = new AnnotationParser(); $annote = $parser->parse($this->current_text()); if (count($annote)) { $this->last_annotation = $annote; } } catch (\Exception $e) { // TODO: some form of error reporting would be good } } $this->ix++; if ($this->ix >= count($this->tokens)) { $this->curr = null; } else { $this->curr = $this->tokens[$this->ix]; } }
function getAnnotations() { if ($this->annotations == null) { $this->annotations = AnnotationParser::parse($this); } return $this->annotations; }
public function testAnnotationWithZeroInNumber() { $parser = new AnnotationParser(); $annotation = $parser->parse('@TestAnnotation(ratio = 0.15)'); $this->assertIdentical($annotation->ratio, 0.15); }