laravel-state-machine icon indicating copy to clipboard operation
laravel-state-machine copied to clipboard

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

Open matthewstick opened this issue 1 year ago • 1 comments
trafficstars

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 avatar Jun 05 '24 13:06 matthewstick

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!

matthewstick avatar Jun 11 '24 12:06 matthewstick

Sorry for the late answer, I fixed it in v3.4.5.

sebdesign avatar Feb 20 '25 13:02 sebdesign