DesignPatternsPHP/DesignPatternsPHP · error · InvalidArgumentException

Invalid status name given

Error message

Invalid status name given

What it means

PostStatus::ensureIsValidName() requires the status name string to appear exactly in $validStates (strict in_array). fromString() throws InvalidArgumentException('Invalid status name given') for any unrecognized or wrongly-cased name before any Status object is created.

Source

Thrown at More/Repository/Domain/PostStatus.php:74

     * and is therefore not able to operate well with exceptions
     */
    public function toString(): string
    {
        return $this->name;
    }

    private static function ensureIsValidId(int $status)
    {
        if (!in_array($status, array_keys(self::$validStates), true)) {
            throw new InvalidArgumentException('Invalid status id given');
        }
    }


    private static function ensureIsValidName(string $status)
    {
        if (!in_array($status, self::$validStates, true)) {
            throw new InvalidArgumentException('Invalid status name given');
        }
    }
}

View on GitHub (pinned to 54254e0f2a)

Solutions

  1. Pass a name exactly matching a value in PostStatus::$validStates (case-sensitive)
  2. Normalize input with strtolower()/trim() before calling fromString() if the source is user input
  3. Check available names via PostStatus::$validStates before calling

Example fix

// before
PostStatus::fromString('Draft'); // case mismatch -> throws
// after
PostStatus::fromString(strtolower(trim('Draft')));
Defensive patterns

Strategy: validation

Validate before calling

if (!in_array($statusName, PostStatus::$validStates, true)) {
    throw new InvalidArgumentException("Unknown post status name: $statusName");
}
$postStatus = PostStatus::fromString($statusName);

Type guard

function isValidStatusName(string $name): bool {
    return in_array($name, PostStatus::$validStates, true);
}

Try / catch

try {
    $status = PostStatus::fromString($inputName);
} catch (InvalidArgumentException $e) {
    $status = PostStatus::draft();
}

Prevention

When it happens

Trigger: PostStatus::fromString('Draft') (wrong case), fromString('published') if that name is not in $validStates, or fromString('') from empty request/config values.

Common situations: User-supplied or API-supplied status strings; typos and casing differences; status names renamed in code but still present in stored data.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of DesignPatternsPHP/DesignPatternsPHP@54254e0f2a (2026-09-01). Data as JSON: /api/errors/264dc27b8a2b09c0. Report an issue: GitHub.