vyuldashev / laravel-queue-rabbitmq

RabbitMQ driver for Laravel Queue. Supports Laravel Horizon.
MIT License
1.93k stars 382 forks source link

RabbitMQQueue::$currentJob must not be accessed before initialization #572

Closed Doclassif closed 10 months ago

Doclassif commented 1 year ago

Typed property VladimirYuldashev\LaravelQueueRabbitMQ\Queue\RabbitMQQueue::$currentJob must not be accessed before initialization /opt/apps/laravel/vendor/vladimir-yuldashev/laravel-queue-rabbitmq/src/Queue/RabbitMQQueue.php:580

This error can be resolved by either providing a default value for the property or by initializing the property within the class constructor.

Doclassif commented 1 year ago

It's kind of fixed, but it's not =) https://github.com/vyuldashev/laravel-queue-rabbitmq/issues/555

iamfarhad commented 1 year ago

you can use this package https://github.com/iamfarhad/LaravelRabbitMQ

adm-bome commented 10 months ago

https://github.com/vyuldashev/laravel-queue-rabbitmq/pull/543#pullrequestreview-1456074682 The comment is not entirely true.

Yes: undefined and null both result in a false value.

Since PHP 7.4 introduces type-hinting for properties, it is particularly important to provide valid values for all properties, so that all properties have values that match their declared types.

A property that has never been assigned doesn't have a null value, but it is in an undefined state, which will never match any declared type. undefined !== null.

It's now best practice to: define properties with a default value or set the property within the construction. So the $currentJob should be defined as null value.

The is_set() check is still a good approach. But with de definition in place all the checks for existance, can be shortend by obmitting the is_set() because the undefined state, is not possible anymore. It's either null or an instance of RabbitMQJob::class. The result: Better code and state management, no unexpected results.

Also see: stackoverflow

Since PHP 8.0 there is also the: null save opperator [?. or ?->], like [??] and [?:] comparison this makes the syntax of comparison for existance shorter.

Without specificaly assigning properties a value, and depending on the undefined state the new features of php like these won't work.

see: null-safe-operator


// This code
        if (isset($this->currentJob) && ! $this->currentJob->isDeletedOrReleased()) {
            $this->reject($this->currentJob, true);
        }

// Can use the shorthanded notation 
        if ($this->currentJob && ! $this->currentJob->isDeletedOrReleased()) {
            $this->reject($this->currentJob, true);
        }
        // or explicit
        if ($this->currentJob?->isDeletedOrReleased() === false) {
            $this->reject($this->currentJob, true);
        }