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

Psalm could not infer this type being passed inside the php8 attribute #7685

Closed
taras-pinchuk-uinno opened this issue Feb 17, 2022 · 6 comments · Fixed by #7692
Closed

Psalm could not infer this type being passed inside the php8 attribute #7685

taras-pinchuk-uinno opened this issue Feb 17, 2022 · 6 comments · Fixed by #7692
Assignees
Labels

Comments

@taras-pinchuk-uinno
Copy link

Psalm version: 4.20.0
Php version: 8.1

Error ouptput:

ERROR: InvalidArgument - app/Requests/UpdateUserRequest.php:43:17 - Argument 7 of OpenApi\Attributes\Patch::__construct expects array<array-key, OpenApi\Attributes\Parameter>|null, array{"Psalm could not infer this type"} provided (see https://psalm.dev/004)
    parameters: [
        new Parameter(name: 'id', description: 'User ID', in: 'path', required: true),
 ],
@psalm-github-bot
Copy link

Hey @taras-pinchuk-uinno, can you reproduce the issue on https://psalm.dev ?

@taras-pinchuk-uinno
Copy link
Author

Hey @taras-pinchuk-uinno, can you reproduce the issue on https://psalm.dev ?

Yes, https://psalm.dev/r/344b174fa6

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/344b174fa6
<?php

#[Attribute]
class B{
     public function __construct(
    ?array $listOfB = null
){
 }
}

#[Attribute(Attribute::TARGET_CLASS)]
class A {
    /**
    * @param B[] $listOfB
  	 **/
 public function __construct(
    ?array $listOfB = null
){
 }
}

//That doesnt works
#[A([new B])]
class C{
}

//This Work
new A([new B]);
Psalm output (using commit 1ee7648):

ERROR: InvalidArgument - 23:5 - Argument 1 of A::__construct expects array<array-key, B>|null, array{'Psalm could not infer this type'} provided

@AndrolGenhald
Copy link
Collaborator

Two issues here, first, we probably shouldn't be using StubsGenerator::getExpressionFromType outside of stub generation if it returns a literal string 'Psalm could not infer this type' instead of mixed when it fails, second, this shouldn't be failing anyway.

@morrgot
Copy link

morrgot commented Feb 17, 2022

facing same problem, while trying to build api documentation with "zircote/swagger-php":

use OpenApi\Attributes as OA;

class GetProductController
{
    #[OA\PathParameter(
        parameter: "productId",
        name: "productId",
        description: "Product Id, to get information about",
        schema: new OA\Schema(
            format: "uuid"
        )
    )]
    public function __invoke(Request $request): Response
    {
    }
}

I get
Argument 4 of OpenApi\Attributes\PathParameter::__construct expects OpenApi\Attributes\Schema|null, "Psalm could not infer this type" provided

@sokil
Copy link

sokil commented Feb 17, 2022

This bug introduced in 4.20.0

    use OpenApi\Attributes as OA;

    #[OA\Parameter(
        schema: new OA\Schema(),
    )]
    public function __invoke(Request $request): Response {}

Problem occured at vendor/vimeo/psalm/src/Psalm/Internal/Analyzer/AttributeAnalyzer.php

            if ($type->isMixed()) {
                return;
            }

            $type_expr = StubsGenerator::getExpressionFromType(
                $type
            );

On version 4.19.0:

object(Psalm\Type\Union)#132719 (27) {
  ["types":"Psalm\Type\Union":private]=>
  array(1) {
    ["mixed"]=>
    object(Psalm\Type\Atomic\TMixed)#132715 (6) {
      ["checked"]=>
      bool(false)
      ["from_docblock"]=>
      bool(false)
      ["offset_start"]=>

On version 4.20.0

object(Psalm\Type\Union)#132693 (27) {
  ["types":"Psalm\Type\Union":private]=>
  array(1) {
    ["OpenApi\Attributes\Schema"]=>
    object(Psalm\Type\Atomic\TNamedObject)#132694 (9) {
      ["checked"]=>
      bool(false)
      ["from_docblock"]=>
      bool(false)
      ["offset_start"]=>
      NULL
      ["offset_end"]=>

But \Psalm\Internal\Stubs\Generator\StubsGenerator::getExpressionFromType does not know about TNamedObject.

So, downgrate to 4.19.0 may fix the problem temporary

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

Successfully merging a pull request may close this issue.

4 participants