Skip to content

PhpReflection::getParameterType() can handle PHP 7.1 nullable types#126

Merged
dg merged 1 commit into
nette:masterfrom
MartinMajor:nullable-types
Sep 21, 2016
Merged

PhpReflection::getParameterType() can handle PHP 7.1 nullable types#126
dg merged 1 commit into
nette:masterfrom
MartinMajor:nullable-types

Conversation

@MartinMajor
Copy link
Copy Markdown
Contributor

Nullable parameter types are prefixed by ? in Reflection in PHP 7.1 which causes that Nette autowiring doesn't work. Even though this behaviour has changed many times in PHP 7.1 ( php/php-src@622d2f4 , php/php-src@8855a2c , php/php-src@f4e68a3 ) this fix uses new preferred way ReflectionNamedType::getName() which looks pretty stable.

Comment thread src/DI/PhpReflection.php Outdated
if ($param->hasType()) {
return (PHP_VERSION_ID >= 70100) ? $param->getType()->getName() : (string) $param->getType();
}
return NULL;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What about using more shallow condition structure?

if (PHP_VERSION_ID >= 70100) {
    return $param->hasType() ? $param->getType()->getName() : NULL;
} elseif (PHP_VERSION_ID >= 70000) {
    return $param->hasType() ? (string) $param->getType() : NULL;
}

Assert::same('array', PhpReflection::getParameterType($params[2]));
Assert::same('callable', PhpReflection::getParameterType($params[3]));
Assert::null(PhpReflection::getParameterType($params[4]));
Assert::same('Test\B', PhpReflection::getParameterType($params[5]));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You should add tests/DI/PhpReflection.getParameterType.php71.phpt (with @phpversion >= 7.1 annotation) with real nullable types.

@MartinMajor
Copy link
Copy Markdown
Contributor Author

Updated by JanTvrdik's suggestions. Thanks!

@dg
Copy link
Copy Markdown
Member

dg commented Sep 21, 2016

Thank you

@dg dg merged commit e62128f into nette:master Sep 21, 2016
@dg
Copy link
Copy Markdown
Member

dg commented Sep 21, 2016

What about return type?

@MartinMajor MartinMajor deleted the nullable-types branch September 21, 2016 12:11
@MartinMajor
Copy link
Copy Markdown
Contributor Author

Yep, it should be updated as well. I'll prepare another PR.

dg added a commit that referenced this pull request Dec 21, 2016
…pes"

Revert "PhpReflection::getParameterType() can handle PHP 7.1 nullable types (#126)"

This reverts commit 7d1385a and 9fcfe47.

Behavior in 7.1.0 stable has been changed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants