Skip to content

Commit a874b4b

Browse files
committed
Fix top-level use statements
Given a namespaced file which uses a top-level name like namespace mindplay; use PDO; the parser will incorrectly generate the following uses information due to an off-by-one error: array('DO' => 'PDO') This commit adjusts the parser to handle use statements which import a name without a namespace separator.
1 parent b6a32ca commit a874b4b

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

src/annotations/AnnotationParser.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,12 @@ public function parse($source, $path)
122122
$use .= $str;
123123
} elseif ($type === self::CHAR) {
124124
if ($str === ',' || $str === ';') {
125-
$uses[substr($use, 1 + strrpos($use, '\\'))] = $use;
125+
if (strpos($use, '\\') !== false) {
126+
$uses[substr($use, 1 + strrpos($use, '\\'))] = $use;
127+
}
128+
else {
129+
$uses[$use] = $use;
130+
}
126131

127132
if ($str === ',') {
128133
$state = self::USE_CLAUSE;

test/suite/Annotations.test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ protected function testCanGetAnnotationFile()
111111
$this->check($file->path === $file_path, 'should reflect path to class-file');
112112
$this->check($file->namespace === 'mindplay\test\Sample', 'should reflect namespace');
113113
$this->check(
114-
$file->uses === array('SampleAlias' => 'mindplay\annotations\Annotation'),
114+
$file->uses === array('Test' => 'Test', 'SampleAlias' => 'mindplay\annotations\Annotation'),
115115
'should reflect use-clause'
116116
);
117117
}

test/suite/Sample/SampleClass.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace mindplay\test\Sample;
44

5-
use mindplay\annotations\Annotation as SampleAlias; // for AnnotationsTest::testCanGetAnnotationFile()
5+
// for AnnotationsTest::testCanGetAnnotationFile()
6+
use Test;
7+
use mindplay\annotations\Annotation as SampleAlias;
68

79
/**
810
* @mindplay\test\Sample\Sample

0 commit comments

Comments
 (0)