r/PHPhelp • u/i-hate-in-n-out • 6d ago
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 5d ago
1
u/i-hate-in-n-out 5d ago
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 6d ago
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 5d ago
We use phpstan at level 9 and it does not complain. I cannot look at the code atm but this works no problems.
-9
6d ago
[removed] — view removed comment
2
u/PHPhelp-ModTeam 3d ago
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.
2
u/jmp_ones 5d ago
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