composer-attribute-collector icon indicating copy to clipboard operation
composer-attribute-collector copied to clipboard

Generating attributes fails with psr/log ^2.0 || ^3.0 and a class implementing LoggerInterface with attributes

Open slepic opened this issue 1 year ago • 5 comments

Composer is using psr/log ^1.0 internally

This package is a plugin to composer.

While scanning for attributes it runs with psr/log 1.0 loaded by composer.

If this plugin scans a file that contains a class which implements Psr\LoggerInterface with the signatures from psr/log 2.0 or 3.0

the plugin will fail with the following error

Generating autoload files
Generating attributes file

Fatal error: Declaration of MyLogger::emergency(Stringable|string $message, array $context = []): void must be compatible with Psr\Log\LoggerInterface::emergency($message, array $context = []) in vendor/psr/log/src/LoggerTrait.php on line 18

This doesnt work

use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;

final readonly class MyLogger implements LoggerInterface
{
    use LoggerTrait;

    public function __construct()
    {
    }

    #[\Override]
    public function log($level, string|\Stringable $message, array $context = []): void
    {
    }
}

When there is a commented out attribute it also doesnt work

use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;

final readonly class MyLogger implements LoggerInterface
{
    use LoggerTrait;

    public function __construct()
    {
    }

//    #[\Override]
    public function log($level, string|\Stringable $message, array $context = []): void
    {
    }
}

It works as long as there are no attributes used in the file

use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;

final readonly class MyLogger implements LoggerInterface
{
    use LoggerTrait;

    public function __construct()
    {
    }

    public function log($level, string|\Stringable $message, array $context = []): void
    {
    }
}

A related issue in composer: https://github.com/composer/composer/issues/11437

I actually just avoided using attributes in the affected class, and honestly it's quite an edge case. I just thought i'd stick it here for anyone encountering the same problem.

slepic avatar Nov 21 '24 15:11 slepic