-
-
Notifications
You must be signed in to change notification settings - Fork 188
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
FEATURE: Type declaration support for property injection #2782
FEATURE: Type declaration support for property injection #2782
Conversation
This change introduces basic support for PHP 7.4 class property types for property injection. It allows for using types as follows: ```php /** * @flow\Inject **/ protected ?LoggerInterface $logger; ``` In order to allow this syntax, Lazy Property Injection was disabled completely. It may be re-implemented as part of a future release. neos#2114
This change is a shortcut / alternative to #2701 |
Awesome! I would suggest, not to communicate the nullable version though but: /**
* @Flow\Inject
*/
protected LoggerInterface $logger; to make code more resilient and clarify that this is currently not lazily injected. This is great, and will bring me back to using inject annotations in application code. Thanks! <3 |
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.
+1 by manual testing (not so much by reading yet)
Neos.Flow/Classes/ObjectManagement/Configuration/ConfigurationBuilder.php
Outdated
Show resolved
Hide resolved
I am not sure why we would sacrifice one feature (lazy injections) for another (PHP 7.4 property type hint support). |
Hang on. I expected this to work as before if I decide to omit the type hint (which is still allowed in all PHP versions) |
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.
Cool! This seems like a sensible tradeoff I guess, will be interesting to compare performance of a code base written with property type hints and one without.
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.
Looks good by reading. Only left a comment for type-hinting the new ReflectionService method
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.
Awesome step into the right direction!
@@ -1315,7 +1331,7 @@ public function reflectClassProperty($className, PropertyReflection $property) | |||
$this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES][$propertyName][self::DATA_PROPERTY_TAGS_VALUES][$tagName] = $tagValues; | |||
} | |||
|
|||
foreach ($this->annotationReader->getPropertyAnnotations($property, $propertyName) as $annotation) { |
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.
Nice one… that parameter was passed since 2013 but not used? Ever? 🤪
Another question:
What was the exact reason the nullability was required? It wasn't just the lazyness, because that would mean we don't need it for typed properties since the injection is eager (for now). |
I adjusted the PR description, is that fine for you, @bwaidelich and @robertlemke? |
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.
Love it, and I can confirm that
/**
* @Flow\Inject
* @var LoggerInterface
*/
protected $logger;
injects the DependencyProxy
and
/**
* @Flow\Inject
* @var LoggerInterface
*/
protected LoggerInterface $logger;
(or without the @var
annotation) injects the actual logger instance!
Thank you so much! <3
That's not correct though. When the type hint is specified, we now always fall back to eager injection. |
The error should only occur with lazy loading, so nullability is not required at the moment. And when we have support for union types, it will probably not even be allowed or at least does not make sense. |
@kdambekalns @bwaidelich I updated the description to correct that. |
@albe asked
It was needed for |
I added a hint on using type and |
So, we're all set? |
So who wants to have the honor of pushing the merge button on this PR we have been all longing for? :) @robertlemke maybe, as he has put in all the hard grunt work. |
LOL, this PR is just a couple of minute's work ;-) Anyway, I'll take the chance. |
It sometimes requires a lifetime of work to finally come to the solution that only takes 5 minutes worth of work. That doesn't devalue the lifetime spent before :) |
This change introduces basic support for PHP 7.4 class property types
for property injection.
It allows for using types as follows:
In order to allow this syntax, Lazy Property Injection is disabled
when a type is declared. To still use Lazy Property Injection, don't
use a PHP type declaration but a
@var
annotation:If using both (type declaration and
@var
), it is handled non-lazily.Lazy Property Injection on properties with type declarations may
be re-implemented as part of a future release.
#2114