Skip to content

Commit 6db1212

Browse files
guilhermeblancoOcramius
authored andcommitted
Merge pull request #308 from dawehner/bmaster
fix the optimize regex and adapt the tests to actually test classAnnotat...
1 parent c11bde7 commit 6db1212

File tree

4 files changed

+71
-27
lines changed

4 files changed

+71
-27
lines changed

lib/Doctrine/Common/Reflection/StaticReflectionParser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ protected function parse()
131131
$this->parsed = true;
132132
$contents = file_get_contents($fileName);
133133
if ($this->classAnnotationOptimize) {
134-
if (preg_match("/(\A.*)^\s+(abstract|final)?\s+class\s+{$this->shortClassName}\s+{/sm", $contents, $matches)) {
135-
$contents = $matches[1];
134+
if (preg_match("/\A.*^\s*((abstract|final)\s+)?class\s+{$this->shortClassName}\s+/sm", $contents, $matches)) {
135+
$contents = $matches[0];
136136
}
137137
}
138138
$tokenParser = new TokenParser($contents);

tests/Doctrine/Tests/Common/Reflection/DeeperNamespaceParent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
namespace Doctrine\Tests\Common\Reflection;
44

5-
class SameNamespaceParent extends Dummies\NoParent
5+
class DeeperNamespaceParent extends Dummies\NoParent
66
{
77
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Doctrine\Tests\Common\Reflection;
4+
5+
use Doctrine\Common\Annotations\Annotation;
6+
7+
/**
8+
* @Annotation(
9+
* key = "value"
10+
* )
11+
*/
12+
class ExampleAnnotationClass {
13+
14+
}

tests/Doctrine/Tests/Common/Reflection/StaticReflectionParserTest.php

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,75 @@
99
class StaticReflectionParserTest extends DoctrineTestCase
1010
{
1111
/**
12-
* @dataProvider classAnnotationOptimize
12+
* @dataProvider parentClassData
1313
*
1414
* @param bool $classAnnotationOptimize
15+
* @param string $parsedClassName
16+
* @param string $expectedClassName
1517
*
1618
* @return void
1719
*/
18-
public function testParentClass($classAnnotationOptimize)
20+
public function testParentClass($classAnnotationOptimize, $parsedClassName, $expectedClassName)
1921
{
22+
// If classed annotation optimization is enabled the properties tested
23+
// below cannot be found.
24+
if ($classAnnotationOptimize) {
25+
$this->setExpectedException('ReflectionException');
26+
}
27+
2028
$testsRoot = substr(__DIR__, 0, -strlen(__NAMESPACE__) - 1);
2129
$paths = array(
2230
'Doctrine\\Tests' => array($testsRoot),
2331
);
24-
$noParentClassName = 'Doctrine\\Tests\\Common\\Reflection\\NoParent';
25-
$staticReflectionParser = new StaticReflectionParser($noParentClassName, new Psr0FindFile($paths), $classAnnotationOptimize);
32+
$staticReflectionParser = new StaticReflectionParser($parsedClassName, new Psr0FindFile($paths), $classAnnotationOptimize);
2633
$declaringClassName = $staticReflectionParser->getStaticReflectionParserForDeclaringClass('property', 'test')->getClassName();
27-
$this->assertEquals($noParentClassName, $declaringClassName);
34+
$this->assertEquals($expectedClassName, $declaringClassName);
2835

29-
$className = 'Doctrine\\Tests\\Common\\Reflection\\FullyClassifiedParent';
30-
$staticReflectionParser = new StaticReflectionParser($className, new Psr0FindFile($paths), $classAnnotationOptimize);
31-
$declaringClassName = $staticReflectionParser->getStaticReflectionParserForDeclaringClass('property', 'test')->getClassName();
32-
$this->assertEquals($noParentClassName, $declaringClassName);
33-
34-
$className = 'Doctrine\\Tests\\Common\\Reflection\\SameNamespaceParent';
35-
$staticReflectionParser = new StaticReflectionParser($className, new Psr0FindFile($paths), $classAnnotationOptimize);
36-
$declaringClassName = $staticReflectionParser->getStaticReflectionParserForDeclaringClass('property', 'test')->getClassName();
37-
$this->assertEquals($noParentClassName, $declaringClassName);
36+
}
3837

38+
/**
39+
* @return array
40+
*/
41+
public function parentClassData()
42+
{
43+
$data = array();
44+
$noParentClassName = 'Doctrine\\Tests\\Common\\Reflection\\NoParent';
3945
$dummyParentClassName = 'Doctrine\\Tests\\Common\\Reflection\\Dummies\\NoParent';
46+
foreach (array(false, true) as $classAnnotationOptimize) {
47+
$data[] = array(
48+
$classAnnotationOptimize, $noParentClassName, $noParentClassName,
49+
);
50+
$data[] = array(
51+
$classAnnotationOptimize, 'Doctrine\\Tests\\Common\\Reflection\\FullyClassifiedParent', $noParentClassName,
52+
);
53+
$data[] = array(
54+
$classAnnotationOptimize, 'Doctrine\\Tests\\Common\\Reflection\\SameNamespaceParent', $noParentClassName,
55+
);
56+
$data[] = array(
57+
$classAnnotationOptimize, 'Doctrine\\Tests\\Common\\Reflection\\DeeperNamespaceParent', $dummyParentClassName,
58+
);
59+
$data[] = array(
60+
$classAnnotationOptimize, 'Doctrine\\Tests\\Common\\Reflection\\UseParent', $dummyParentClassName,
61+
);
62+
}
63+
return $data;
64+
}
4065

41-
$className = 'Doctrine\\Tests\\Common\\Reflection\\DeeperNamespaceParent';
42-
$staticReflectionParser = new StaticReflectionParser($className, new Psr0FindFile($paths), $classAnnotationOptimize);
43-
$declaringClassName = $staticReflectionParser->getStaticReflectionParserForDeclaringClass('property', 'test')->getClassName();
44-
$this->assertEquals($dummyParentClassName, $declaringClassName);
45-
46-
$className = 'Doctrine\\Tests\\Common\\Reflection\\UseParent';
47-
$staticReflectionParser = new StaticReflectionParser($className, new Psr0FindFile($paths), $classAnnotationOptimize);
48-
$declaringClassName = $staticReflectionParser->getStaticReflectionParserForDeclaringClass('property', 'test')->getClassName();
49-
$this->assertEquals($dummyParentClassName, $declaringClassName);
50-
66+
/**
67+
* @dataProvider classAnnotationOptimize
68+
*/
69+
public function testClassAnnotationOptimizedParsing($classAnnotationOptimize) {
70+
$testsRoot = substr(__DIR__, 0, -strlen(__NAMESPACE__) - 1);
71+
$paths = array(
72+
'Doctrine\\Tests' => array($testsRoot),
73+
);
74+
$staticReflectionParser = new StaticReflectionParser('Doctrine\\Tests\\Common\\Reflection\\ExampleAnnotationClass', new Psr0FindFile($paths), $classAnnotationOptimize);
75+
$expectedDocComment = '/**
76+
* @Annotation(
77+
* key = "value"
78+
* )
79+
*/';
80+
$this->assertEquals($expectedDocComment, $staticReflectionParser->getDocComment('class'));
5181
}
5282

5383
/**

0 commit comments

Comments
 (0)