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

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

Open
slepic opened this issue Nov 21, 2024 · 2 comments

Comments

@slepic
Copy link

slepic commented Nov 21, 2024

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:
composer/composer#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.

@olvlvl
Copy link
Owner

olvlvl commented Nov 21, 2024

Hi @slepic, thanks for reporting. I was able to reproduce the issue. Do you want the file to be collected? If not, you can exclude it:

{
  "require": {
    "php": ">=8.4",
    "psr/log": "^3.0",
    "olvlvl/composer-attribute-collector": "dev-main as 2.0"
  },
  "config": {
    "allow-plugins": {
      "olvlvl/composer-attribute-collector": true
    }
  },
  "extra": {
    "composer-attribute-collector": {
      "include": [
        "src"
      ],
      "exclude": [
        "src/MyLogger.php"
      ]
    }
  },
  "autoload": {
    "psr-4": {
      "App\\": "src"
    }
  }
}

@slepic
Copy link
Author

slepic commented Dec 2, 2024

hi, @olvlvl

sry for the late response, yeah its really just that

"require": {
    "psr/log": "^2.0 || ^3.0",
    "olvlvl/composer-attribute-collector": "^2.0"
  },

you can exclude it

yeah good tip

i actually do not need to collect, i discovered it with the Override attributes and I just removed them as I dont really need them for anything other than clarity. and I dont want to add it to exclude either. It's not for a logging lib, its for a thin wrapper inside an application code and I'll just carry on without attributes on that class that's ok for me...


Anyway, I dont really think there is a true solution to this problem as long as this runs as a composer plugin.

As I understood from the discussions in composer repo, the psr/log package bundled with composer itself will not receive an update any sooner than in composer 3.0


So to close up, I dont think any further steps need to be taken here, except waiting for new major version of composer.
Unless ofc you can think of some clever way to circumvent the limitation in the current version.
I'd say keep it open till then, for anyone encountering the same problem...

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

No branches or pull requests

2 participants