Enhancing PhpStan's capabilities through an extension (container singleton) #4274
-
Hello there, I am working on a big legacy project which uses a singleton container I'm wondering if there would be a way to develop a plugin that would allow PhpStan to recognize "Container::getInstance()" calls, track whether it is followed immediately by an array access or the variable on which it's stored, and complete the inspections with the parsing of the Container.php file which defines all the indexed and the services' types. WDYT ? Is that even remotely possible ? Or is the only solution to add Thanks for your insights. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi, to describe what's returned by It specifically works thanks to this piece of code: https://github.com/phpstan/phpstan-src/blob/0af0399a9c877b3a40a8ad89f62b623c87538a90/src/Analyser/MutatingScope.php#L2165-L2175 There's already a similar extension: https://github.com/phpstan/phpstan-nette/blob/master/src/Type/Nette/ComponentModelArrayAccessDynamicReturnTypeExtension.php What it does: Nette's component model allows |
Beta Was this translation helpful? Give feedback.
Hi, to describe what's returned by
Container::getInstance()['someService']
, you can write a dynamic return type extension forArrayAccess::offsetGet()
method (where ArrayAccess should be narrowed by the type returned byContainer::getInstance()
).It specifically works thanks to this piece of code: https://github.com/phpstan/phpstan-src/blob/0af0399a9c877b3a40a8ad89f62b623c87538a90/src/Analyser/MutatingScope.php#L2165-L2175
There's already a similar extension: https://github.com/phpstan/phpstan-nette/blob/master/src/Type/Nette/ComponentModelArrayAccessDynamicReturnTypeExtension.php
What it does:
Nette's component model allows
$container->getComponent('name')
to be shortened as$container['…