-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[PropertyInfo] Implement "Collection" types in PhpDocExtractor #26300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
namespace Symfony\Component\PropertyInfo\Util; | ||
|
||
use phpDocumentor\Reflection\Type as DocType; | ||
use phpDocumentor\Reflection\Types\Collection; | ||
use phpDocumentor\Reflection\Types\Compound; | ||
use phpDocumentor\Reflection\Types\Null_; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
@@ -39,7 +40,7 @@ public function getTypes(DocType $varType): array | |
$nullable = true; | ||
} | ||
|
||
$type = $this->createType((string) $varType, $nullable); | ||
$type = $this->createType($varType, $nullable); | ||
if (null !== $type) { | ||
$types[] = $type; | ||
} | ||
|
@@ -49,16 +50,15 @@ public function getTypes(DocType $varType): array | |
|
||
$varTypes = array(); | ||
for ($typeIndex = 0; $varType->has($typeIndex); ++$typeIndex) { | ||
$varTypes[] = (string) $varType->get($typeIndex); | ||
} | ||
$type = $varType->get($typeIndex); | ||
|
||
// If null is present, all types are nullable | ||
$nullKey = array_search(Type::BUILTIN_TYPE_NULL, $varTypes); | ||
$nullable = false !== $nullKey; | ||
// If null is present, all types are nullable | ||
if ($type instanceof Null_) { | ||
$nullable = true; | ||
continue; | ||
} | ||
|
||
// Remove the null type from the type if other types are defined | ||
if ($nullable && count($varTypes) > 1) { | ||
unset($varTypes[$nullKey]); | ||
$varTypes[] = $type; | ||
} | ||
|
||
foreach ($varTypes as $varType) { | ||
|
@@ -74,8 +74,24 @@ public function getTypes(DocType $varType): array | |
/** | ||
* Creates a {@see Type} from a PHPDoc type. | ||
*/ | ||
private function createType(string $docType, bool $nullable): ?Type | ||
private function createType(DocType $type, bool $nullable): ?Type | ||
{ | ||
$docType = (string) $type; | ||
|
||
if ($type instanceof Collection) { | ||
list($phpType, $class) = $this->getPhpTypeAndClass((string) $type->getFqsen()); | ||
|
||
$key = $this->getTypes($type->getKeyType()); | ||
$value = $this->getTypes($type->getValueType()); | ||
|
||
// More than 1 type returned means it is a Compound type, which is | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think we can improve the type VO (it's a final class exactly for this reason) to support this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it could be done without much shenanigans (adding a counpound internal type, and a property holding composing types). But i'm afraid that would be a breaking change, as the component will start to return non-null types where it previously did. |
||
// not handled by Type, so better use a null value. | ||
$key = 1 === \count($key) ? $key[0] : null; | ||
$value = 1 === \count($value) ? $value[0] : null; | ||
|
||
return new Type($phpType, $nullable, $class, true, $key, $value); | ||
} | ||
|
||
// Cannot guess | ||
if (!$docType || 'mixed' === $docType) { | ||
return null; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can inline this IMO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah since you are not modifying it, forgot about this ;). It will be harder to merge things back to master.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I tried to modify as few lines as possible, to keep diffs shorts.