sebdesign / laravel-state-machine

Winzou State Machine service provider for Laravel
MIT License
325 stars 57 forks source link

SM\\StateMachine\\StateMachine::getState(): Return value must be of type string, null returned #72

Open matthewstick opened 3 months ago

matthewstick commented 3 months ago

In the getState function, sometimes $accessor->getValue can return null.

however, getState is expecting a return type of string, so it breaks the state machine.

If you add a check, this fixes it:

if (is_null($state)) { return ''; }

but maybe there is a better way?

`

    public function getState(): string
    {

    $accessor = new PropertyAccessor();
    $state = $accessor->getValue($this->object, $this->config['property_path']);

    if ($state instanceof \BackedEnum) {
        return $state->value;
    }

    if ($state instanceof \UnitEnum) {
        return $state->name;
    }

   // possible fix?
    if (is_null($state)) {
        return '';
    }

    return $state;
}

`

matthewstick commented 3 months ago

hi @sebdesign . Tagging in case you didn't see it. This fix is working for me. Hoping not to fork your project, but let me know if this looks legit and you can implement? Thanks!