laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.16k stars 10.88k forks source link

Contextual binding resolved when it shouldn't be for LogManager #52480

Open wslawski-printify opened 4 weeks ago

wslawski-printify commented 4 weeks ago

Laravel Version

11.20.0

PHP Version

8.3.9

Database Driver & Version

No response

Description

So currently there is an issue with contextual binding(LogManager is just example) and Container::call() method, but any alias that is pointing to two different not compatible with each other interfaces can cause this problem.

Basically, we have an issue after upgrading to laravel 9(but also happens on 11) that when we have contextual binding for LoggerInterface::class, but deprecation log is happening there will be an exception triggered for that:

During inheritance of JsonSerializable: Uncaught Error: Call to undefined method Monolog\Logger::channel() in /Users/wslawski/PhpstormProjects/untitled1/example-app/vendor/laravel/framework/src/Illuminate/Log/Logger.php:309 Stack trace:

0 /Users/wslawski/PhpstormProjects/untitled1/example-app/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(103): Illuminate\Log\Logger->__call('channel', Array)

The reason is that both classes are aliased as log and laravel container when getting LogManager resolves it to alias log and since there is contextual binding for it, it returns wrong instance, without channel method existing.

I see this being solved by either stopping aliasing incompatible types with each other in the container or somehow resolving the correct type in the first place — don't use contextual binding when it shouldn't be used.

On laravel 8 everything worked as expected - because we laravel didn't update buildStack when resolving dependencies for method calls, and starting from 9 it is happening here - https://github.com/laravel/framework/blob/11.x/src/Illuminate/Container/Container.php#L685

Steps To Reproduce

Service provider:

namespace App\Providers;

use App\Commands\TestCommand;
use App\TestClass;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
use Psr\Log\LoggerInterface;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        $this->app->when(TestClass::class)
            ->needs(LoggerInterface::class)
            ->give(fn() => Log::channel('test'));

        $this->commands([TestCommand::class]);
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        //
    }
}

TestClass:

namespace App;

use App\Test;
use Psr\Log\LoggerInterface;

final class TestClass
{
    public function __construct(
    ) {
    }

    public function handle(LoggerInterface $logger): void
    {
        $xyz = new Test();
        $xyz->jsonSerialize();
    }
}

Test:

namespace App;

final class Test implements \JsonSerializable
{
    public function jsonSerialize()
    {
        return [];
    }
}

TestCommand:

namespace App\Commands;

use App\TestClass;
use Illuminate\Bus\Dispatcher;
use Illuminate\Console\Command;

final class TestCommand extends Command
{
    protected $signature = 'test:deprecation';

    public function handle(Dispatcher $dispatcher): void
    {
        $dispatcher->dispatch(new TestClass());
    }
}

Set LOG_DEPRECATIONS_CHANNEL=single

Run php artisan test:deprecation - you will get an exception that channel method does not exist

github-actions[bot] commented 4 weeks ago

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

rust17 commented 2 weeks ago

I think the issue is due to the jsonSerialize() method not being fully compatible with JsonSerializable::jsonSerialize(). You can refer to the documentation here: https://www.php.net/manual/en/class.jsonserializable.php. Feel free to give it another try:

Test:

namespace App;

final class Test implements \JsonSerializable
{
-     public function jsonSerialize()
+    public function jsonSerialize() : mixed
    {
        return [];
    }
}
wslawski-printify commented 1 week ago

@rust17 I know about this, because it triggers deprecation. But this is not root cause of issue - the issue here is bugged contextual binding and that's used when being in HandleExceptions context and/or using same alias for classes that are not compatible with each other.

wslawski-printify commented 1 week ago

@crynobone I have question here, why there is help wanted added? For me this is clearly a bug that should be fixed, it happens from laravel 9 to laravel 11.

driesvints commented 1 week ago

The label is added because we'd appreciate any community effort here in getting this resolved.