Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
andrew-demb opened this issue Sep 18, 2023 · 1 comment · Fixed by #10261
Closed

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

andrew-demb opened this issue Sep 18, 2023 · 1 comment · Fixed by #10261

Comments

@andrew-demb
Copy link
Contributor

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
Copy link

I found these snippets:

https://psalm.dev/r/22d155399e
<?php

/**
 * Returned after [query execution](executing-queries.md).
 * Represents both - result of successful execution and of a failed one
 * (with errors collected in `errors` prop).
 *
 * Could be converted to [spec-compliant](https://facebook.github.io/graphql/#sec-Response-Format)
 * serializable array using `toArray()`.
 *
 * @phpstan-type SerializableError array{
 *   message: string,
 *   locations?: array<int, array{line: int, column: int}>,
 *   path?: array<int, int|string>,
 *   extensions?: array<string, mixed>
 * }
 * @phpstan-type SerializableErrors array<int, SerializableError>
 * @phpstan-type SerializableResult array{
 *     data?: array<string, mixed>,
 *     errors?: SerializableErrors,
 *     extensions?: array<string, mixed>
 * }
 * @phpstan-type ErrorFormatter callable(\Throwable): SerializableError
 * @phpstan-type ErrorsHandler callable(array<Error> $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<array-key, mixed>'

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant