r/PHPhelp • u/i-hate-in-n-out • Aug 02 '26
Sharing Array Shapes Across Files?
Is it possible to share array shapes across files? I am working in a very legacy code base so don't have an easy way to turn this into a class, and thus am kind of stuck with arrays.
Say in file a.php we have something like:
/**
* @phpstan-type User array{
* user_id: int,
* username: string,
* email: string
*/
The in file b.php we have something like:
/**
* @phpstan-import-type User
*/
/**
* @param User $user
*/
function foo($user) {}
While my Intellisense in my IDE recognizes this, at the moment, phpstan at level 2 and above flags this as an unknown type.
Is there a way I can properly share array shapes across files?
2
u/gaborj Aug 02 '26
1
u/i-hate-in-n-out Aug 02 '26
Thanks. I played with this and while it works with phpstan, it doesn't work with intellisense. I found another way to do it but it has to be attached to classes. Though interestingly, the imported type doesn't have to be attached to a class when using mago.
1
u/itemluminouswadison Aug 02 '26
great question. i tried a few things in phpstorm but i'm not coming up with anything better.
what you could do is maybe try a union in the type annotation?
1
u/DevelopmentScary3844 Aug 02 '26
We use phpstan at level 9 and it does not complain. I cannot look at the code atm but this works no problems.
-8
Aug 02 '26
[removed] — view removed comment
2
u/PHPhelp-ModTeam Aug 04 '26
Do not post comments that simply tell the user to ask an AI like ChatGPT. The point of this sub is to provide human responses.
-1
u/OppieT Aug 02 '26
Google says:
Add to phpstan.neon:
parameters:
TypeAliases:
User: ‘’’
array{
user_id: int;
username: string;
email: string
}
‘’’
2
u/jmp_ones Aug 02 '26
The Star-Interop packages use an empty interface file with PHPStan types. For one example, see https://github.com/response-interop/interface/blob/1.x/src/ResponseTypeAliases.php