-
-
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
TASK: Use generics via @template instead of PHPSTORM_META #3222
Conversation
Im not sure i got all the places where generics make sense. But its a good start :D |
61c53af
to
9548d29
Compare
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.
Yeah, why not…
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, I like!
Adds typings to: \Neos\Flow\ObjectManagement\ObjectManagerInterface::get() and \Neos\Flow\Core\Bootstrap::getEarlyInstance() by usage of the @template tag: https://phpstan.org/writing-php-code/phpdocs-basics#generics
9548d29
to
433e6af
Compare
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.
I generally like this a lot and probably am an explanation about virtual objects away from aproving.
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.
I added suggestions to support virtual objects using the PHPStan notation for Conditional return types
See https://phpstan.org/r/1017a495-661c-4929-9f33-8f83065ac1f4
@bwaidelich i was about to say: good point with virtual objects but we should avoid that ^^ - but your solution seems quite clever! Question is if phpstorm/psalm also supports this or do we have to use |
Fyi it has to be /**
* @template T of object
* @param class-string<T>|string $objectName The object name
* @return ($objectName is class-string<T> ? T : object) The object instance
*/ your previous code would always say object (also for actual passed classes) - will test this also in phpstorm ^^ |
Hmm @bwaidelich and @mficzel it seems that the proposed super magic cow powder syntax is not supported by phpstorm yet: (newGetInstance works but not crazyNewGetInstance) /**
* @template T of object
* @param class-string<T> $objectName The object name
* @return T The object instance
*/
public function newGetInstance($objectName)
{
return $this->objects[$objectName]['i'] ?? throw new \Exception();
}
/**
* @template T of object
* @param class-string<T>|string $objectName The object name
* @return ($objectName is class-string<T> ? T : object) The object instance
*/
public function crazyNewGetInstance($objectName)
{
return $this->objects[$objectName]['i'] ?? throw new \Exception();
} i would propose that people have to write this in the super rare cases, when they access an actual virtual object: /**
* @var MyVirtualObject $virtual
* @phpstan-ignore-next-line
*/
$virtual = $objectManager->get('My.Virtual'); |
…ject > Parameter #1 $objectName of method ObjectManager::get() expects class-string<Some.Package:VirtualObject>, string given. instead we will return `object` Phpstorm doesnt support his conditional return type so we have a simple `@return T` in place which allows for autocompletion
With d4ed048 it will also work with virtual object by just returning |
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.
Works like a charm, thanks for taking care!
PHPStan:
https://phpstan.org/r/795a1071-4d3f-43bc-b578-6b5c53fd116c
PHPStorm:
Note: For virtual objects, the IDE interprets the string as class name, but that's not worse than before I think:
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.
very nice!
Since the php universe evolved im gonna try #2753 again ;)
Adds typings to:
\Neos\Flow\ObjectManagement\ObjectManagerInterface::get()
and
\Neos\Flow\Core\Bootstrap::getEarlyInstance()
by usage of the @template tag: https://phpstan.org/writing-php-code/phpdocs-basics#generics
This feature is supported by phpstorm, psalm and phpstan and also used widely in Neos 9
Upgrade instructions
Review instructions
Checklist
FEATURE|TASK|BUGFIX
!!!
and have upgrade-instructions