-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[PropertyInfo] Fix edge cases in ReflectionExtractor #20154
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 |
---|---|---|
|
@@ -44,6 +44,13 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp | |
*/ | ||
public static $arrayMutatorPrefixes = array('add', 'remove'); | ||
|
||
private $supportsParameterType; | ||
|
||
public function __construct() | ||
{ | ||
$this->supportsParameterType = method_exists('ReflectionParameter', 'getType'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
@@ -134,68 +141,33 @@ private function extractFromMutator($class, $property) | |
$reflectionParameters = $reflectionMethod->getParameters(); | ||
$reflectionParameter = $reflectionParameters[0]; | ||
|
||
$arrayMutator = in_array($prefix, self::$arrayMutatorPrefixes); | ||
|
||
if (method_exists($reflectionParameter, 'getType') && $reflectionType = $reflectionParameter->getType()) { | ||
$fromReflectionType = $this->extractFromReflectionType($reflectionType); | ||
|
||
if (!$arrayMutator) { | ||
return array($fromReflectionType); | ||
if ($this->supportsParameterType) { | ||
if (!$reflectionType = $reflectionParameter->getType()) { | ||
return; | ||
} | ||
$type = $this->extractFromReflectionType($reflectionType); | ||
|
||
$phpType = Type::BUILTIN_TYPE_ARRAY; | ||
$collectionKeyType = new Type(Type::BUILTIN_TYPE_INT); | ||
$collectionValueType = $fromReflectionType; | ||
} | ||
|
||
if ($reflectionParameter->isArray()) { | ||
$phpType = Type::BUILTIN_TYPE_ARRAY; | ||
$collection = true; | ||
} | ||
|
||
if ($arrayMutator) { | ||
$collection = true; | ||
$nullable = false; | ||
$collectionNullable = $reflectionParameter->allowsNull(); | ||
} else { | ||
$nullable = $reflectionParameter->allowsNull(); | ||
$collectionNullable = false; | ||
} | ||
|
||
if (!isset($collection)) { | ||
$collection = false; | ||
} | ||
|
||
if (method_exists($reflectionParameter, 'isCallable') && $reflectionParameter->isCallable()) { | ||
$phpType = Type::BUILTIN_TYPE_CALLABLE; | ||
} | ||
|
||
if ($typeHint = $reflectionParameter->getClass()) { | ||
if ($collection) { | ||
$phpType = Type::BUILTIN_TYPE_ARRAY; | ||
$collectionKeyType = new Type(Type::BUILTIN_TYPE_INT); | ||
$collectionValueType = new Type(Type::BUILTIN_TYPE_OBJECT, $collectionNullable, $typeHint->name); | ||
// HHVM reports variadics with "array" but not builtin type hints | ||
if (!$reflectionType->isBuiltin() && Type::BUILTIN_TYPE_ARRAY === $type->getBuiltinType()) { | ||
return; | ||
} | ||
} elseif (preg_match('/^(?:[^ ]++ ){4}([a-zA-Z_\x7F-\xFF][^ ]++)/', $reflectionParameter, $info)) { | ||
if (Type::BUILTIN_TYPE_ARRAY === $info[1]) { | ||
$type = new Type(Type::BUILTIN_TYPE_ARRAY, $reflectionParameter->allowsNull(), null, true); | ||
} elseif (Type::BUILTIN_TYPE_CALLABLE === $info[1]) { | ||
$type = new Type(Type::BUILTIN_TYPE_CALLABLE, $reflectionParameter->allowsNull()); | ||
} else { | ||
$phpType = Type::BUILTIN_TYPE_OBJECT; | ||
$typeClass = $typeHint->name; | ||
$type = new Type(Type::BUILTIN_TYPE_OBJECT, $reflectionParameter->allowsNull(), $info[1]); | ||
} | ||
} else { | ||
return; | ||
} | ||
|
||
// Nothing useful extracted | ||
if (!isset($phpType)) { | ||
return; | ||
if (in_array($prefix, self::$arrayMutatorPrefixes)) { | ||
$type = new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), $type); | ||
} | ||
|
||
return array( | ||
new Type( | ||
$phpType, | ||
$nullable, | ||
isset($typeClass) ? $typeClass : null, | ||
$collection, | ||
isset($collectionKeyType) ? $collectionKeyType : null, | ||
isset($collectionValueType) ? $collectionValueType : null | ||
), | ||
); | ||
return array($type); | ||
} | ||
|
||
/** | ||
|
@@ -213,7 +185,7 @@ private function extractFromAccessor($class, $property) | |
return; | ||
} | ||
|
||
if (method_exists($reflectionMethod, 'getReturnType') && $reflectionType = $reflectionMethod->getReturnType()) { | ||
if ($this->supportsParameterType && $reflectionType = $reflectionMethod->getReturnType()) { | ||
return array($this->extractFromReflectionType($reflectionType)); | ||
} | ||
|
||
|
@@ -231,15 +203,15 @@ private function extractFromAccessor($class, $property) | |
*/ | ||
private function extractFromReflectionType(\ReflectionType $reflectionType) | ||
{ | ||
$phpTypeOrClass = method_exists($reflectionType, 'getName') ? $reflectionType->getName() : (string) $reflectionType; | ||
$phpTypeOrClass = $reflectionType instanceof \ReflectionNamedType ? $reflectionType->getName() : $reflectionType->__toString(); | ||
$nullable = $reflectionType->allowsNull(); | ||
|
||
if ($reflectionType->isBuiltin()) { | ||
if (Type::BUILTIN_TYPE_ARRAY === $phpTypeOrClass) { | ||
$type = new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true); | ||
} else { | ||
$type = new Type($phpTypeOrClass, $nullable); | ||
} | ||
if (Type::BUILTIN_TYPE_ARRAY === $phpTypeOrClass) { | ||
$type = new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true); | ||
} elseif ('void' === $phpTypeOrClass) { | ||
$type = new Type(Type::BUILTIN_TYPE_NULL, $nullable); | ||
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. missing "void" handling added here also 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. Can you add a test for this new one? 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. Test added |
||
} elseif ($reflectionType->isBuiltin()) { | ||
$type = new Type($phpTypeOrClass, $nullable); | ||
} else { | ||
$type = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $phpTypeOrClass); | ||
} | ||
|
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.
By using the string representation of the parameter to get the type hint (on PHP5 only of course), we prevent autoloading of the corresponding class.