-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpKernel] Added nullable support for php 7.1 and below #19785
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 |
---|---|---|
|
@@ -34,7 +34,7 @@ public function createArgumentMetadata($controller) | |
} | ||
|
||
foreach ($reflection->getParameters() as $param) { | ||
$arguments[] = new ArgumentMetadata($param->getName(), $this->getType($param), $this->isVariadic($param), $this->hasDefaultValue($param), $this->getDefaultValue($param)); | ||
$arguments[] = new ArgumentMetadata($param->getName(), $this->getType($param), $this->isVariadic($param), $this->hasDefaultValue($param), $this->getDefaultValue($param), $this->isNullable($param)); | ||
} | ||
|
||
return $arguments; | ||
|
@@ -64,6 +64,23 @@ private function hasDefaultValue(\ReflectionParameter $parameter) | |
return $parameter->isDefaultValueAvailable(); | ||
} | ||
|
||
/** | ||
* Returns if the argument is allowed to be null but is still mandatory. | ||
* | ||
* @param \ReflectionParameter $parameter | ||
* | ||
* @return bool | ||
*/ | ||
private function isNullable(\ReflectionParameter $parameter) | ||
{ | ||
if (PHP_VERSION_ID >= 70000) { | ||
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't you use 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. isn't hhvm supposed to correspond with a certain php version? 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. Not necessarilly, for example hhvm can support some php7 features and set this constant to php5 (see this comment) and as we don't know how they are gonna support php7.1 features, i think it is safer to use 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. https://github.com/iltar/symfony/blob/d4466c0d58c41d9da4fd118f1e8973f375d6bc97/src/Symfony/Component/HttpKernel/ControllerMetadata/ArgumentMetadataFactory.php#L52 Already doing here as well. Tbh I rather don't make hacks for hhvm. 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 don't think that's a hack as it actually checks that the feature you need is available (it's imo less hacky than depending on a version). For the same reason, I think we should update the examples you're pointing as it could create unexpected behaviors. But I'm not using hvvm so it won't impact me anyway, I just think it would be weird if it doesn't work sometimes when it should. Maybe we could ask someone of the hvvm team if it's safe to depend on this constant for this feature? 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. method_exists is fast, let's use it imho 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. Should I update this for the other cases in this class as well while I'm at it? We do a lose a bit of verbosity of why this check is being done. The version fallbacks should definitely be removed in the future. How would you suggest it should be traced back? I'm not familiar with HHVM or how they version it. Maybe this is a generic problem which should be solved framework wide? 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. It's on a case by case basis I'd say. Logically, HHVM should expose a consistent value for PHP_VERSION_ID when it is compatible enough with it. But this is a moving target and sometimes (like here) we can use feature tests for better portability. So yes, let's check the existing places and see how this applies. 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've been poking around on
I would really love to see a generic feature detection system which is determined either during container compilation or kernel boot-up 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. What do you mean? This checks are only needed for php7 features and higher but we can't have something generic as hhvm allows to enable some features but not necessary all of them. |
||
return null !== ($type = $parameter->getType()) && $type->allowsNull(); | ||
} | ||
|
||
// fallback for supported php 5.x versions | ||
return $this->hasDefaultValue($parameter) && null === $this->getDefaultValue($parameter); | ||
} | ||
|
||
/** | ||
* Returns a default value if available. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\HttpKernel\Tests\Fixtures\Controller; | ||
|
||
class NullableController | ||
{ | ||
public function action(? string $foo, ? \stdClass $bar, ? string $baz = 'value', $mandatory) | ||
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. Sorry for my minor comment but I don't know if a syntax has been defined yet for this, because I don't really like having a space after the 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. Intuitively, I tend to agree with not having a space. 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. That's exactly how I had it, but this was fixed by fabbot.io :( 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. then fabbot needs a fix and we should remove the space here |
||
{ | ||
} | ||
} |
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.
Minor but: I think both
$isNullable
and%s
should be enclosed with double quotes (like in theArgumentResolver
exception you've tweaked for instance). :)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.
Challenging the deprecation again: wouldn't it make sense to have $isNullable default to false and remove the deprecation, thus merge on 3.1?
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.
Actually, one option could be to add it like this on 3.1 and deprecate it in 3.2 by changing the default value. The
isNullable()
method will still be a new feature though.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.
But why deprecate anything now or later? Let's keep the param optional, always. Or would that hurt?
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.
Oh, btw, forward compat with newer php versions should be considered as a bug fix (as done previously)
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.
No it wouldn't hurt actually, I'll switch active branches in that case and re-open the other PR ^^