Skip to content

Enhancing PhpStan's capabilities through an extension (container singleton) #4274

Answered by ondrejmirtes
gnutix asked this question in Support
Discussion options

You must be logged in to vote

Hi, to describe what's returned by Container::getInstance()['someService'], you can write a dynamic return type extension for ArrayAccess::offsetGet() method (where ArrayAccess should be narrowed by the type returned by Container::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['…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@gnutix
Comment options

@ondrejmirtes
Comment options

Answer selected by ondrejmirtes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Support
Labels
None yet
2 participants