Skip to content

[12.x] Refactor: simplify code#58820

Merged
taylorotwell merged 1 commit intolaravel:12.xfrom
alipowerful7:refactor/simplify-code
Feb 14, 2026
Merged

[12.x] Refactor: simplify code#58820
taylorotwell merged 1 commit intolaravel:12.xfrom
alipowerful7:refactor/simplify-code

Conversation

@alipowerful7
Copy link
Contributor

@alipowerful7 alipowerful7 commented Feb 14, 2026

Simplify castBinding() by removing redundant enum check

Changes

This PR simplifies the castBinding method by removing the redundant instanceof UnitEnum check and directly calling enum_value().

Rationale

The current implementation checks if the value is a UnitEnum before calling enum_value():

public function castBinding($value)
{
    if ($value instanceof UnitEnum) {
        return enum_value($value);
    }

    return $value;
}

However, the enum_value() helper already handles all these cases internally using a match expression:

function enum_value($value, $default = null)
{
    return match (true) {
        $value instanceof \BackedEnum => $value->value,
        $value instanceof \UnitEnum => $value->name,
        default => $value ?? value($default),
    };
}

Since enum_value() already returns the original value when it's not an enum (via the default case), we can simplify castBinding to:

public function castBinding($value)
{
    return enum_value($value);
}

Benefits

  • Removes duplication: Eliminates redundant enum type checking
  • Cleaner code: Reduces one conditional statement and one return statement
  • Single source of truth: All enum handling logic stays in enum_value()
  • Same behavior: No functional changes, just simplified code

@alipowerful7
Copy link
Contributor Author

Un related test failed

@taylorotwell taylorotwell merged commit 1cf38ed into laravel:12.x Feb 14, 2026
47 of 72 checks passed
@alipowerful7 alipowerful7 deleted the refactor/simplify-code branch February 15, 2026 14:57
DarkGhostHunter pushed a commit to DarkGhostHunter/laravel-framework that referenced this pull request Feb 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants