Skip to content

[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

Merged
merged 1 commit into from
Oct 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down Expand Up @@ -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)) {
Copy link
Member Author

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.

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);
}

/**
Expand All @@ -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));
}

Expand All @@ -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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing "void" handling added here also

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test for this new one?

Copy link
Member Author

Choose a reason for hiding this comment

The 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public function php71TypesProvider()
{
return array(
array('foo', array(new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true))),
array('buz', array(new Type(Type::BUILTIN_TYPE_NULL))),
array('bar', array(new Type(Type::BUILTIN_TYPE_INT, true))),
array('baz', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING)))),
array('donotexist', null),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public function getFoo(): ?array
{
}

public function getBuz(): void
{
}

public function setBar(?int $bar)
{
}
Expand Down