vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.59k stars 664 forks source link

Make @psalm-type defined inside a class accesible from outside #1363

Closed juanfernandoe closed 5 years ago

juanfernandoe commented 5 years ago

Please allow to access type alias from outside class

class Test1
{

    /**
     * @psalm-type _BB=array{code:int,name:string}
     */
}

/**
 * @psalm-param Test1::_BB $p
 */
function func1(array $p): void {
    echo $p['code'];
}

output error: Parameter $p has wrong type 'scalar-class-constant', should be 'array<array-key, mixe d>'

muglug commented 5 years ago

Constants aren't scoped to the class - they're available throughout the file:

https://psalm.dev/r/414a8c16fb

juanfernandoe commented 5 years ago

Hi,

I appreciate you taking the time to answer my question.

I have another doubt, how access class scoped psalm-type from another file?

muglug commented 5 years ago

It's not possible, unfortunately - it would introduce a level of indirection that would make the first scanning step much slower.

juanfernandoe commented 5 years ago

I understand

Have you any advise to workaround this situation?, because I'm planning to add psalm to an existent project.

I'm very influenced by the Hacklang shapes, so I have array structure across multiples classes.

muglug commented 5 years ago

No workaround that I can think of - Psalm itself copies types in a number of places: https://github.com/vimeo/psalm/search?q=IssueData&unscoped_q=IssueData

muglug commented 5 years ago

If they ever become a language feature (as they are with Hack) it may be possible to use other means to make this performative.

juanfernandoe commented 5 years ago

maybe you consider something like Phan Stubs in order to define global psalm-type in a special place, so you can parse global types at the beginning.

https://github.com/phan/phan/wiki/How-To-Use-Stubs

muglug commented 5 years ago

Ah yes, Psalm already supports stubs so that should be relatively easy. Created #1386 to track that.