briannesbitt / Carbon

A simple PHP API extension for DateTime.
https://carbon.nesbot.com/
MIT License
16.53k stars 1.28k forks source link

Cannot bind an instance to a static closure in /var/www/app/vendor/nesbot/carbon/src/Carbon/Traits/Date.php on line 2488 #2538

Closed jonny-dengro closed 2 years ago

jonny-dengro commented 2 years ago

Hello,

I encountered an issue with the following code:

use Illuminate\Support\Carbon;
use Cmixin\BusinessTime;

    /**
     * {@inheritdoc}
     */
    public function calculateTimeToFirstContact(): float
    {
        // As a second argument you can set default opening hours:
        BusinessTime::enable(Carbon::class, [
            'monday' => ['09:00-17:00'],
            'tuesday' => ['09:00-17:00'],
            'wednesday' => ['09:00-17:00'],
            'thursday' => ['09:00-17:00']
            'friday' => ['09:00-17:00'],
            'saturday' => [],
            'sunday' => []
            // You can use the holidays provided by BusinessDay and mark them as fully closed days using 'holidaysAreClosed'
            'holidaysAreClosed' => true,
            // Note that exceptions will still have the precedence over the holidaysAreClosed option.
            'holidays' => [
                'region' => ''gb-national',
            ],
        ]);

        // ensure we have some opening hours before attempting to calculate diff
        /** @phpstan-ignore-next-line */
        error_reporting(0);
        $timeRange = Carbon::getCurrentOpenTimeRange();

        if ($timeRange) {
            /** @phpstan-ignore-next-line */
            return Carbon::parse('2022-01-07 09:00:00')
                ->diffInBusinessSeconds('2022-01-12 17:00:00');
        }

        return 0;
    }

Carbon version: 2.55.2

PHP version: 8.0

I expected to get:

No warning, float returned

But I actually get:

PHP Warning:  Cannot bind an instance to a static closure in /var/www/app/vendor/nesbot/carbon/src/Carbon/Traits/Date.php on line 2488

Float returned

Thanks!

ricardo-lobo commented 2 years ago

@jonny-dengro how did you manage to solve this? Having the exact same issue

jonny-dengro commented 2 years ago

@jonny-dengro how did you manage to solve this? Having the exact same issue

Couldn't unfortunately. The warning is thrown within the package so wasn't able to omit it within our app. Ended up using different packages.

kylekatarnls commented 2 years ago

With correct settings this won't happen, you likely have a custom error handler that by-pass the @ error-muter operator.