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

Constructor of class xxx has an unused parameter $xxx also efects at new PHP 8 Attribute #7165

Open
ghost opened this issue May 2, 2022 · 3 comments · May be fixed by phpstan/phpstan-src#3776

Comments

@ghost
Copy link

ghost commented May 2, 2022

Bug report

Hi if you declare a new Attribute in PHP 8 this error is thrown:

Constructor of class XX\XXX\Structs\Attribute\ArrayList has an unused parameter $structName.

In this case it is part of the standart and the paramter will be used in reflection.

Code snippet that reproduces the problem

<?php
declare(strict_types=1);

namespace XX\XXX\Structs\Attribute;

use Attribute;

#[Attribute(Attribute::TARGET_PROPERTY)]
final class ArrayList
{
    public function __construct(string $structName)
    {}
}

https://phpstan.org/r/4f6303f0-6e98-46e8-9efc-9baf39b03a8b

Expected output

The issue is reported incorrectly.

Did PHPStan help you today? Did it make you happy in any way?

Yes, every day!

@mergeable
Copy link

mergeable bot commented May 2, 2022

This bug report is missing a link to reproduction at phpstan.org/try.

It will most likely be closed after manual review.

@ondrejmirtes
Copy link
Member

Can you post a piece of code how do you read such parameter?

@ghost
Copy link
Author

ghost commented May 2, 2022

<?php
declare(strict_types=1);

namespace XX\XXX;

use XX\XXX\Structs\Attribute\ArrayList;

class Ball
{
    #[ArrayList('balablub')]
    public array $colors = [];
}

<?php
declare(strict_types=1);

namespace XX\XXX;

class Example
{

    public function readArgumentValue(): void
    {
        $reflection = new \ReflectionClass(Ball::class);
        $reflectionProperties = $reflection->getProperties();
        $reflectionProperty = $reflectionProperties[0];
        $reflectionAttributes = $reflectionProperty->getAttributes();
        $reflectionAttribute = $reflectionAttributes[0];
        $reflectionArguments = $reflectionAttribute->getArguments();
        $reflectionArgument = $reflectionArguments[0]; // balablub
    }
}

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