Skip to content

Commit 3e6d5ea

Browse files
author
Alexander Obuhovich
committed
Merge pull request php-annotations#100 from php-annotations/bug/99
Fix parsing of PHP file with annotations without context
2 parents 912bb9e + 928cc69 commit 3e6d5ea

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

src/annotations/AnnotationParser.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function parse($source, $path)
6969
{
7070
$index = array();
7171

72-
$annotations = array();
72+
$docblocks = array();
7373
$state = self::SCAN;
7474
$nesting = 0;
7575
$class = null;
@@ -154,8 +154,8 @@ public function parse($source, $path)
154154
case self::CLASS_NAME:
155155
if ($type == T_STRING) {
156156
$class = ($namespace ? $namespace . '\\' : '') . $str;
157-
$index[$class] = $annotations;
158-
$annotations = array();
157+
$index[$class] = $docblocks;
158+
$docblocks = array();
159159
$state = self::SCAN_CLASS;
160160
}
161161
break;
@@ -171,8 +171,8 @@ public function parse($source, $path)
171171

172172
case self::MEMBER:
173173
if ($type == T_VARIABLE) {
174-
$index[$class . '::' . $str] = $annotations;
175-
$annotations = array();
174+
$index[$class . '::' . $str] = $docblocks;
175+
$docblocks = array();
176176
$state = self::SCAN_CLASS;
177177
}
178178
if ($type == T_FUNCTION) {
@@ -182,8 +182,8 @@ public function parse($source, $path)
182182

183183
case self::METHOD_NAME:
184184
if ($type == T_STRING) {
185-
$index[$class . '::' . $str] = $annotations;
186-
$annotations = array();
185+
$index[$class . '::' . $str] = $docblocks;
186+
$docblocks = array();
187187
$state = self::SCAN_CLASS;
188188
}
189189
break;
@@ -206,7 +206,7 @@ public function parse($source, $path)
206206
}
207207

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

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

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

230228
$code = "return array(\n";
231229
$code .= " '#namespace' => " . var_export($namespace, true) . ",\n";
232230
$code .= " '#uses' => " . var_export($uses, true) . ",\n";
233-
foreach ($index as $key => $array) {
231+
foreach ($index as $key => $docblocks) {
232+
$array = array();
233+
foreach ($docblocks as $str) {
234+
$array = array_merge($array, $this->findAnnotations($str));
235+
}
234236
if (count($array)) {
235237
$code .= " " . trim(var_export($key, true)) . " => array(\n " . implode(
236238
",\n ",

test/suite/Annotations.test.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use mindplay\annotations\AnnotationManager;
88
use mindplay\annotations\Annotations;
99
use mindplay\annotations\Annotation;
10+
use mindplay\annotations\standard\ReturnAnnotation;
1011
use mindplay\test\annotations\Package;
1112
use mindplay\test\lib\xTest;
1213
use mindplay\test\lib\xTestRunner;
@@ -811,6 +812,22 @@ public function testMalformedParamAnnotationThrowsException()
811812
Annotations::ofMethod('BrokenParamAnnotationClass', 'brokenParamAnnotation');
812813
}
813814

815+
protected function testOrphanedAnnotationsAreIgnored()
816+
{
817+
$manager = new AnnotationManager();
818+
$manager->namespace = 'mindplay\test\Sample';
819+
$manager->cache = false;
820+
821+
/** @var Annotation[] $annotations */
822+
$annotations = $manager->getMethodAnnotations('mindplay\test\Sample\OrphanedAnnotations', 'someMethod');
823+
824+
$this->check(count($annotations) == 1, 'the @return annotation was found');
825+
$this->check(
826+
$annotations[0] instanceof ReturnAnnotation,
827+
'the @return annotation has correct type'
828+
);
829+
}
830+
814831
}
815832

816833
return new AnnotationsTest;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace mindplay\test\Sample;
4+
5+
class OrphanedAnnotations
6+
{
7+
8+
/**
9+
* Some method.
10+
*
11+
* @return void
12+
*/
13+
public function someMethod()
14+
{
15+
$a = 5;
16+
17+
// @codeCoverageIgnoreStart
18+
if (false) {
19+
$a = 6;
20+
}
21+
// @codeCoverageIgnoreEnd
22+
23+
$a = 5;
24+
}
25+
26+
}

0 commit comments

Comments
 (0)