8000 Use last version of reflection dockblock, avoid extra dependancy if l… · symfony/symfony@d642eae · GitHub
[go: up one dir, main page]

Skip to content

Commit d642eae

Browse files
committed
Use last version of reflection dockblock, avoid extra dependancy if library needed
1 parent ff87f30 commit d642eae

File tree

3 files changed

+62
-96
lines changed

3 files changed

+62
-96
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@
8686
"egulias/email-validator": "~1.2",
8787
"symfony/polyfill-apcu": "~1.1",
8888
"symfony/security-acl": "~2.8|~3.0",
89-
"phpdocumentor/reflection": "^1.0.7"
89+
"phpdocumentor/reflection-docblock": "^3.0"
9090
},
9191
"conflict": {
92-
"phpdocumentor/reflection": "<1.0.7"
92+
"phpdocumentor/reflection-docblock": "<3.0"
9393
},
9494
"autoload": {
9595
"psr-4": {

src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php

Lines changed: 57 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
namespace Symfony\Component\PropertyInfo\Extractor;
1313

14-
use phpDocumentor\Reflection\ClassReflector;
1514
use phpDocumentor\Reflection\DocBlock;
16-
use phpDocumentor\Reflection\FileReflector;
15+
use phpDocumentor\Reflection\DocBlockFactory;
16+
use phpDocumentor\Reflection\Types\Compound;
17+
use phpDocumentor\Reflection\Types\ContextFactory;
18+
use phpDocumentor\Reflection\Types\Null_;
1719
use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface;
1820
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
1921
use Symfony\Component\PropertyInfo\Type;
@@ -30,35 +32,48 @@ class PhpDocExtractor implements PropertyDescriptionExtractorInterface, Property
3032
const MUTATOR = 2;
3133

3234
/**
33-
* @var FileReflector[]
35+
* @var DocBlock[]
3436
*/
35-
private $fileReflectors = array();
37+
private $docBlocks = array();
3638

3739
/**
38-
* @var DocBlock[]
40+
* @var DocBlockFactory
3941
*/
40-
private $docBlocks = array();
42+
private $docBlockFactory;
43+
44+
/**
45+
* @var ContextFactory
46+
*/
47+
private $contextFactory;
48+
49+
public function __construct()
50+
{
51+
$this->docBlockFactory = DocBlockFactory::createInstance();
52+
$this->contextFactory = new ContextFactory();
53+
}
4154

4255
/**
4356
* {@inheritdoc}
4457
*/
4558
public function getShortDescription($class, $property, array $context = array())
4659
{
60+
/** @var $docBlock DocBlock */
4761
list($docBlock) = $this->getDocBlock($class, $property);
4862
if (!$docBlock) {
4963
return;
5064
}
5165

52-
$shortDescription = $docBlock->getShortDescription();
53-
if ($shortDescription) {
66+
$shortDescription = $docBlock->getSummary();
67+
68+
if (!empty($shortDescription)) {
5469
return $shortDescription;
5570
}
5671

5772
foreach ($docBlock->getTagsByName('var') as $var) {
58-
$parsedDescription = $var->getParsedDescription();
73+
$varDescription = $var->getDescription()->render();
5974

60-
if (isset($parsedDescription[0]) && '' !== $parsedDescription[0]) {
61-
return $parsedDescription[0];
75+
if (!empty($varDescription)) {
76+
return $varDescription;
6277
}
6378
}
6479
}
@@ -68,12 +83,13 @@ public function getShortDescription($class, $property, array $context = array())
6883
*/
6984
public function getLongDescription($class, $property, array $context = array())
7085
{
86+
/** @var $docBlock DocBlock */
7187
list($docBlock) = $this->getDocBlock($class, $property);
7288
if (!$docBlock) {
7389
return;
7490
}
7591

76-
$contents = $docBlock->getLongDescription()->getContents();
92+
$contents = $docBlock->getDescription()->render();
7793

7894
return '' === $contents ? null : $contents;
7995
}
@@ -83,6 +99,7 @@ public function getLongDescription($class, $property, array $context = array())
8399
*/
84100
public function getTypes($class, $property, array $context = array())
85101
{
102+
/** @var $docBlock DocBlock */
86103
list($docBlock, $source, $prefix) = $this->getDocBlock($class, $property);
87104
if (!$docBlock) {
88105
return;
@@ -103,8 +120,31 @@ public function getTypes($class, $property, array $context = array())
103120
}
104121

105122
$types = array();
123+
/** @var DocBlock\Tags\Var_|DocBlock\Tags\Return_|DocBlock\Tags\Param $tag */
106124
foreach ($docBlock->getTagsByName($tag) as $tag) {
107-
$varTypes = $tag->getTypes();
125+
$varType = $tag->getType();
126+
$nullable = false;
127+
128+
if (!$varType instanceof Compound) {
129+
if ($varType instanceof Null_) {
130+
$nullable = true;
131+
}
132+
133+
$type = $this->createType((string) $varType, $nullable);
134+
135+
if (null !== $type) {
136+
$types[] = $type;
137+
}
138+
139+
continue;
140+
}
141+
142+
$typeIndex = 0;
143+
$varTypes = array();
144+
while ($varType->has($typeIndex)) {
145+
$varTypes[] = (string) $varType->get($typeIndex);
146+
++$typeIndex;
147+
}
108148

109149
// If null is present, all types are nullable
110150
$nullKey = array_search(Type::BUILTIN_TYPE_NULL, $varTypes);
@@ -134,29 +174,6 @@ public function getTypes($class, $property, array $context = array())
134174
return array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), $types[0]));
135175
}
136176

137-
/**
138-
* Gets the FileReflector associated with the class.
139-
*
140-
* @param \ReflectionClass $reflectionClass
141-
*
142-
* @return FileReflector|null
143-
*/
144-
private function getFileReflector(\ReflectionClass $reflectionClass)
145-
{
146-
if (!($fileName = $reflectionClass->getFileName()) || 'hh' === pathinfo($fileName, PATHINFO_EXTENSION)) {
147-
return;
148-
}
149-
150-
if (isset($this->fileReflectors[$fileName])) {
151-
return $this->fileReflectors[$fileName];
152-
}
153-
154-
$this->fileReflectors[$fileName] = new FileReflector($fileName);
155-
$this->fileReflectors[$fileName]->process();
156-
157-
return $this->fileReflectors[$fileName];
158-
}
159-
160177
/**
161178
* Gets the DocBlock for this property.
162179
*
@@ -212,27 +229,7 @@ private function getDocBlockFromProperty($class, $property)
212229
return;
213230
}
214231

215-
$reflectionCLass = $reflectionProperty->getDeclaringClass();
216-
217-
$fileReflector = $this->getFileReflector($reflectionCLass);
218-
if (!$fileReflector) {
219-
return;
220-
}
221-
222-
foreach ($fileReflector->getClasses() as $classReflector) {
223-
$className = $this->getClassName($classReflector);
224-
225-
if ($className === $reflectionCLass->name) {
226-
foreach ($classReflector->getProperties() as $propertyReflector) {
227-
// strip the $ prefix
228-
$propertyName = substr($propertyReflector->getName(), 1);
229-
230-
if ($propertyName === $property) {
231-
return $propertyReflector->getDocBlock();
232-
}
233-
}
234-
}
235-
}
232+
return $this->docBlockFactory->create($reflectionProperty, $this->contextFactory->createFromReflector($reflectionProperty));
236233
}
237234

238235
/**
@@ -242,11 +239,12 @@ private function getDocBlockFromProperty($class, $property)
242239
* @param string $ucFirstProperty
243240
* @param int $type
244241
*
245-
* @return DocBlock|null
242+
* @return array
246243
*/
247244
private function getDocBlockFromMethod($class, $ucFirstProperty, $type)
248245
{
249246
$prefixes = $type === self::ACCESSOR ? ReflectionExtractor::$accessorPrefixes : ReflectionExtractor::$mutatorPrefixes;
247+
$prefix = null;
250248

251249
foreach ($prefixes as $prefix) {
252250
$methodName = $prefix.$ucFirstProperty;
@@ -269,39 +267,7 @@ private function getDocBlockFromMethod($class, $ucFirstProperty, $type)
269267
return;
270268
}
271269

272-
$reflectionClass = $reflectionMethod->getDeclaringClass();
273-
$fileReflector = $this->getFileReflector($reflectionClass);
274-
275-
if (!$fileReflector) {
276-
return;
277-
}
278-
279-
foreach ($fileReflector->getClasses() as $classReflector) {
280-
$className = $this->getClassName($classReflector);
281-
282-
if ($className === $reflectionClass->name) {
283-
if ($methodReflector = $classReflector->getMethod($methodName)) {
284-
return array($methodReflector->getDocBlock(), $prefix);
285-
}
286-
}
287-
}
288-
}
289-
290-
/**
291-
* Gets the normalized class name (without trailing backslash).
292-
*
293-
* @param ClassReflector $classReflector
294-
*
295-
* @return string
296-
*/
297-
private function getClassName(ClassReflector $classReflector)
298-
{
299-
$className = $classReflector->getName();
300-
if ('\\' === $className[0]) {
301-
return substr($className, 1);
302-
}
303-
304-
return $className;
270+
return array($this->docBlockFactory->create($reflectionMethod, $this->contextFactory->createFromReflector($reflectionMethod)), $prefix);
305271
}
306272

307273
/**

src/Symfony/Component/PropertyInfo/composer.json

Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@
2828
"require-dev": {
2929
"symfony/serializer": "~2.8|~3.0",
3030
"symfony/cache": "~3.1",
31-
"phpdocumentor/reflection": "^1.0.7",
31+
"phpdocumentor/reflection-docblock": "^3.0",
3232
"doctrine/annotations": "~1.0"
3333
},
3434
"conflict": {
35-
"phpdocumentor/reflection": "<1.0.7"
35+
"phpdocumentor/reflection-docblock": "<3.0"
3636
},
3737
"suggest": {
3838
"psr/cache-implementation": "To cache results",
3939
"symfony/doctrine-bridge": "To use Doctrine metadata",
40-
"phpdocumentor/reflection": "To use the PHPDoc",
40+
"phpdocumentor/reflection-docblock": "To use the PHPDoc",
4141
"symfony/serializer": "To use Serializer metadata"
4242
},
4343
"autoload": {

0 commit comments

Comments
 (0)
0