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

InvalidAttribute error for ReturnTypeWillChange on PHP 8.1 #6731

Closed
Tracked by #6395
ramsey opened this issue Oct 24, 2021 · 8 comments · Fixed by #6732
Closed
Tracked by #6395

InvalidAttribute error for ReturnTypeWillChange on PHP 8.1 #6731

ramsey opened this issue Oct 24, 2021 · 8 comments · Fixed by #6732

Comments

@ramsey
Copy link

ramsey commented Oct 24, 2021

On PHP 8.1, Psalm responds with an InvalidAttribute error when using the ReturnTypeWillChange attribute. However, it should recognize ReturnTypeWillChange as a valid PHP 8 attribute.

  1. To reproduce, save the following to Foo.php in an empty directory.

    <?php
    
    declare(strict_types=1);
    
    namespace Acme;
    
    class Foo implements \JsonSerializable
    {
        /**
         * @return mixed
         */
        #[\ReturnTypeWillChange]
        public function jsonSerialize()
        {
            return [];
        }
    }
  2. Execute the following shell commands to download Composer and use Docker to run Psalm on PHP 8.1.

    curl -L -O https://getcomposer.org/download/latest-stable/composer.phar
    docker run --rm -it -v "$PWD:/workspace" -w "/workspace" php:8.1.0RC4-cli-alpine php composer.phar require vimeo/psalm
    docker run --rm -it -v "$PWD:/workspace" -w "/workspace" php:8.1.0RC4-cli-alpine php vendor/bin/psalm --init
    docker run --rm -it -v "$PWD:/workspace" -w "/workspace" php:8.1.0RC4-cli-alpine php vendor/bin/psalm
  3. You will see the following output:

    Scanning files...
    Analyzing files...
    
    E
    
    ERROR: InvalidAttribute - Foo.php:12:7 - The class ReturnTypeWillChange doesn’t have the Attribute attribute (see https://psalm.dev/242)
        #[\ReturnTypeWillChange]
    
    
    ------------------------------
    1 errors found
    ------------------------------
    
    Checks took 1.60 seconds and used 40.415MB of memory
    Psalm was unable to infer types in the codebase
    
@psalm-github-bot
Copy link

Hey @ramsey, can you reproduce the issue on https://psalm.dev ?

@ramsey
Copy link
Author

ramsey commented Oct 24, 2021

On psalm.dev, the error is different, I suspect because it's not updated to run 8.1 code yet. https://psalm.dev/r/52f0cbb033

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/52f0cbb033
<?php

declare(strict_types=1);

namespace Acme;

class Foo implements \JsonSerializable
{
    /**
     * @return mixed
     */
    #[\ReturnTypeWillChange]
    public function jsonSerialize()
    {
        return [];
    }
}
Psalm output (using commit 9fb9591):

INFO: UndefinedAttributeClass - 12:7 - Attribute class ReturnTypeWillChange does not exist

@ramsey
Copy link
Author

ramsey commented Oct 24, 2021

For reference, here is the RFC that introduced ReturnTypeWillChange: https://wiki.php.net/rfc/internal_method_return_types

@orklah
Copy link
Collaborator

orklah commented Oct 24, 2021

Thanks for the report. I don't use attributes much myself, is it the first core attribute in PHP?

EDIT: and does this one require specific treatment in Psalm?

@ramsey
Copy link
Author

ramsey commented Oct 24, 2021

This is not the first core attribute in PHP. Others were added in PHP 8.0.

This attribute is required to suppress a deprecation notice for classes that implement JsonSerialize in projects that want to support PHP earlier than 8.1. The return type signature for JsonSerialize has changed in PHP 8.1. If you change it in your code, the code will result in a fatal error in PHP 8.0 and earlier, so the solution (if you want to support PHP 7.x and 8.0, in addition to 8.1) is to use this new attribute.

I don't know if it requires specific treatment in Psalm.

@orklah
Copy link
Collaborator

orklah commented Oct 24, 2021

Do you know the names of the other attributes? I'm trying to locate them in Psalm's code

@ramsey
Copy link
Author

ramsey commented Oct 24, 2021

I thought there were others, but I can't find them, so it's possible this is the first one that affects core behavior. I know others are in discussion on the PHP wiki.

So far, it looks like there's the Attribute class that may be used to declare a class as an attribute, and there's the ReturnTypeWillChange attribute. See here and here.

The PHP manual has a section about how attributes work: https://www.php.net/attributes

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

Successfully merging a pull request may close this issue.

2 participants