Skip to content

Commit

Permalink
Merge pull request #100 from php-annotations/bug/99
Browse files Browse the repository at this point in the history
Fix parsing of PHP file with annotations without context
  • Loading branch information
Alexander Obuhovich committed Apr 15, 2015
2 parents 912bb9e + 928cc69 commit 3e6d5ea
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/annotations/AnnotationParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function parse($source, $path)
{
$index = array();

$annotations = array();
$docblocks = array();
$state = self::SCAN;
$nesting = 0;
$class = null;
Expand Down Expand Up @@ -154,8 +154,8 @@ public function parse($source, $path)
case self::CLASS_NAME:
if ($type == T_STRING) {
$class = ($namespace ? $namespace . '\\' : '') . $str;
$index[$class] = $annotations;
$annotations = array();
$index[$class] = $docblocks;
$docblocks = array();
$state = self::SCAN_CLASS;
}
break;
Expand All @@ -171,8 +171,8 @@ public function parse($source, $path)

case self::MEMBER:
if ($type == T_VARIABLE) {
$index[$class . '::' . $str] = $annotations;
$annotations = array();
$index[$class . '::' . $str] = $docblocks;
$docblocks = array();
$state = self::SCAN_CLASS;
}
if ($type == T_FUNCTION) {
Expand All @@ -182,8 +182,8 @@ public function parse($source, $path)

case self::METHOD_NAME:
if ($type == T_STRING) {
$index[$class . '::' . $str] = $annotations;
$annotations = array();
$index[$class . '::' . $str] = $docblocks;
$docblocks = array();
$state = self::SCAN_CLASS;
}
break;
Expand All @@ -206,7 +206,7 @@ public function parse($source, $path)
}

if ($type == T_COMMENT || $type == T_DOC_COMMENT) {
$annotations = array_merge($annotations, $this->findAnnotations($str));
$docblocks[] = $str;
}

if ($type == T_CURLY_OPEN) {
Expand All @@ -223,14 +223,16 @@ public function parse($source, $path)
echo '</table>';
}

if (count($annotations)) {
throw new AnnotationException("Orphaned annotation(s) found at end of a file {$path}: " . implode(",\r", $annotations));
}
unset($docblocks);

$code = "return array(\n";
$code .= " '#namespace' => " . var_export($namespace, true) . ",\n";
$code .= " '#uses' => " . var_export($uses, true) . ",\n";
foreach ($index as $key => $array) {
foreach ($index as $key => $docblocks) {
$array = array();
foreach ($docblocks as $str) {
$array = array_merge($array, $this->findAnnotations($str));
}
if (count($array)) {
$code .= " " . trim(var_export($key, true)) . " => array(\n " . implode(
",\n ",
Expand Down
17 changes: 17 additions & 0 deletions test/suite/Annotations.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use mindplay\annotations\AnnotationManager;
use mindplay\annotations\Annotations;
use mindplay\annotations\Annotation;
use mindplay\annotations\standard\ReturnAnnotation;
use mindplay\test\annotations\Package;
use mindplay\test\lib\xTest;
use mindplay\test\lib\xTestRunner;
Expand Down Expand Up @@ -811,6 +812,22 @@ public function testMalformedParamAnnotationThrowsException()
Annotations::ofMethod('BrokenParamAnnotationClass', 'brokenParamAnnotation');
}

protected function testOrphanedAnnotationsAreIgnored()
{
$manager = new AnnotationManager();
$manager->namespace = 'mindplay\test\Sample';
$manager->cache = false;

/** @var Annotation[] $annotations */
$annotations = $manager->getMethodAnnotations('mindplay\test\Sample\OrphanedAnnotations', 'someMethod');

$this->check(count($annotations) == 1, 'the @return annotation was found');
$this->check(
$annotations[0] instanceof ReturnAnnotation,
'the @return annotation has correct type'
);
}

}

return new AnnotationsTest;
26 changes: 26 additions & 0 deletions test/suite/Sample/OrphanedAnnotations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace mindplay\test\Sample;

class OrphanedAnnotations
{

/**
* Some method.
*
* @return void
*/
public function someMethod()
{
$a = 5;

// @codeCoverageIgnoreStart
if (false) {
$a = 6;
}
// @codeCoverageIgnoreEnd

$a = 5;
}

}

0 comments on commit 3e6d5ea

Please sign in to comment.