vimeo / psalm

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

Named type with phpstan-specific formatting cannot be parsed by psalm #10217

Closed andrew-demb closed 9 months ago

andrew-demb commented 10 months ago

Phpstan playground https://phpstan.org/r/5da87ef6-9869-40ca-b851-d4edcac82fba Psalm playground https://psalm.dev/r/22d155399e

Source (exported types): https://github.com/webonyx/graphql-php/blob/29b5b613bd3ee34f227650f856ee8dd4ce151b88/src/Executor/ExecutionResult.php#L9-L34


Expected: there's no error for code that imports type SerializableError

Actual:

ERROR: [InvalidTypeImport](https://psalm.dev/233) - 34:25 - Type alias SerializableError imported from ExecutionResult is not defined on the source class
/**
 * @internal
 *
 * @phpstan-import-type SerializableError from ExecutionResult
 */
class Foo
{
    /**
      * @return SerializableError
     */
    public function format(\Throwable $error): array
    {
        return [
        'message' => 'message',
    ];
    }
}
psalm-github-bot[bot] commented 10 months ago

I found these snippets:

https://psalm.dev/r/22d155399e ```php , * path?: array, * extensions?: array * } * @phpstan-type SerializableErrors array * @phpstan-type SerializableResult array{ * data?: array, * errors?: SerializableErrors, * extensions?: array * } * @phpstan-type ErrorFormatter callable(\Throwable): SerializableError * @phpstan-type ErrorsHandler callable(array $errors, ErrorFormatter $formatter): SerializableErrors * * @see \GraphQL\Tests\Executor\ExecutionResultTest */ class ExecutionResult {} /** * @internal * * @phpstan-import-type SerializableError from ExecutionResult */ class Foo { /** * @return SerializableError */ public function format(\Throwable $error): array { return [ 'message' => 'message', ]; } } ``` ``` Psalm output (using commit 7428e49): ERROR: InvalidDocblock - 28:1 - Unexpected space ERROR: MismatchingDocblockReturnType - 39:17 - Docblock has incorrect return type 'type-alias(ExecutionResult::SerializableError)', should be 'array' INFO: MixedInferredReturnType - 39:17 - Could not verify return type 'type-alias(ExecutionResult::SerializableError)' for Foo::format ERROR: InvalidTypeImport - 34:25 - Type alias SerializableError imported from ExecutionResult is not defined on the source class ```