Skip to content

Commit

Permalink
inline docblock is now supported: /** @Alpha @beta @gama */
Browse files Browse the repository at this point in the history
more parser optimizations
closes #15
  • Loading branch information
marcioAlmada committed Dec 3, 2013
1 parent c440592 commit aba3c56
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/Minime/Annotations/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,28 @@ public function parse()
$identifier = $this->rules->getAnnotationIdentifier();
$pattern = '/\\'.$identifier.$this->rules->getAnnotationNameRegex().'/';
$types_pattern = '/('.implode('|', $this->types).')/';
$lines = array_map('rtrim', explode("\n", $this->raw_doc_block));
$lines = array_map('trim', explode("\n", $this->raw_doc_block));
array_walk(
$lines,
function ($line) use (&$parameters, $identifier, $pattern, $types_pattern) {
$line = new StringScanner($line);
$line->skip('/\s+\*\s+/');
function ($line_string) use (&$parameters, $identifier, $pattern, $types_pattern) {
$line = new StringScanner($line_string);
if (! $line->skip('/(\*\s*)/')) { // tries to skip *s
if ($line->skip('/(\/\*{2}\s*)/')) { // tries to skip /**s
$remainder = trim(str_replace('*/', '', $line->getRemainder()));
$line = new StringScanner($remainder);
} else {
$line->terminate(); // terminates earlier in case none of the skip strategies work
}
}
while (! $line->hasTerminated()) {

$key = $line->scan($pattern);
if (! $key) { // next line when no annotation is found
if (null === $key) { // next line when no annotation is found
$line->terminate();
continue;
}

$key = substr($key, strlen($identifier));
if (! array_key_exists($key, $parameters)) {
$parameters[$key] = [];
}

$line->skip('/\s+/');
if ('' == $line->peek() || $line->check('/\\'.$identifier.'/')) { // if implicit boolean
Expand All @@ -83,10 +88,11 @@ function ($line) use (&$parameters, $identifier, $pattern, $types_pattern) {
}

$type = 'dynamic';
if ($line->check($types_pattern)) { //if strong typed
if ($line->check($types_pattern)) { // if strong typed
$type = $line->scan('/\w+/');
$line->skip('/\s+/');
}

$parameters[$key][] = self::parseValue($line->getRemainder(), $type);
}
}
Expand Down Expand Up @@ -144,7 +150,7 @@ protected static function parseDynamic($value)
}

/**
* Parse a given value
* Parse a given valueas string
* @param string $value
*
* @return scalar|object
Expand Down
9 changes: 9 additions & 0 deletions test/lib/Minime/Annotations/Fixtures/AnnotationsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ class AnnotationsFixture
*/
private $namespaced_fixture;

/** @value foo */
private $inline_docblock_fixture;

/** @alpha */
private $inline_docblock_implicit_boolean_fixture;

/** @alpha @beta @gama */
private $inline_docblock_multiple_implicit_boolean_fixture;

/**
* @get @post @ajax
* @postParam x
Expand Down
17 changes: 17 additions & 0 deletions test/suite/Minime/Annotations/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,23 @@ public function tolerateUnrecognizedTypes()
$this->assertEquals("footype Tolerate me. DockBlocks can't be evaluated rigidly.", $annotations['value']);
}

/**
* @test
*/
public function parseInlineDocblocks()
{
$annotations = $this->getParser('inline_docblock_fixture')->parse();
$this->assertSame('foo', $annotations['value']);

$annotations = $this->getParser('inline_docblock_implicit_boolean_fixture')->parse();
$this->assertSame(true, $annotations['alpha']);

$annotations = $this->getParser('inline_docblock_multiple_implicit_boolean_fixture')->parse();
$this->assertSame(true, $annotations['alpha']);
$this->assertSame(true, $annotations['beta']);
$this->assertSame(true, $annotations['gama']);
}

/**
* @test
* @expectedException Minime\Annotations\ParserException
Expand Down

0 comments on commit aba3c56

Please sign in to comment.