Lines 100.00% 5 / 5
Methods 100.00% 1 / 1
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 fromChecked 100.00% 5 / 5 100.00% 1 / 1 1
11enum ForeignKeyAction: string
12{
13    /** Delete/update the child rows too. */
14    case Cascade = 'CASCADE';
15
16    /** Set the child columns to `NULL` (they must be nullable). */
17    case SetNull = 'SET NULL';
18
19    /** Refuse the delete/update (error, transaction-compatible). */
20    case Restrict = 'RESTRICT';
21
22    /** Refuse the delete/update (SQL-standard variant). */
23    case NoAction = 'NO ACTION';
24
25    /** Set the child columns to their declared default. */
26    case SetDefault = 'SET DEFAULT';
27
28    /**
29     * Resolve a string to a case, failing fast on anything else.
30     *
31     * @param  string  $action
32     * @return self
33     * @throws \InvalidArgumentException
34     */
35    public static function fromChecked(string $action): self
36    {
37        $normalized = strtoupper(str_replace(['-', '_'], ' ', trim($action)));
38
39        return self::tryFrom($normalized)
40            ?? throw new \InvalidArgumentException(
41                "A foreign key action must be one of CASCADE, SET NULL, RESTRICT, NO ACTION, or SET DEFAULT; got [{$action}]."
42            );
43    }
44}