Lines 100.00% 8 / 8
Methods 100.00% 4 / 4
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 typeColumn 100.00% 1 / 1 100.00% 1 / 1 1
 keyColumn 100.00% 1 / 1 100.00% 1 / 1 1
 assertKeyTypeCapable 100.00% 5 / 5 100.00% 1 / 1 2
22final class Morphs
23{
24    /**
25     * Create a morph-pair declaration.
26     *
27     * @param  string  $name
28     * @param  bool  $nullable
29     * @param  ColumnType  $keyType  The `{name}_id` column's type; every morph target's primary key must match it.
30     */
31    public function __construct(
32        public string $name,
33        public bool $nullable = false,
34        public ColumnType $keyType = ColumnType::BigInt,
35    ) {
36    }
37
38    /**
39     * The type-discriminator column name this declaration emits.
40     *
41     * @return string
42     */
43    public function typeColumn(): string
44    {
45        return $this->name . '_type';
46    }
47
48    /**
49     * The key column name this declaration emits.
50     *
51     * @return string
52     */
53    public function keyColumn(): string
54    {
55        return $this->name . '_id';
56    }
57
58    /**
59     * Assert the declared key type can hold a primary-key value.
60     *
61     * @return void
62     * @throws \InvalidArgumentException
63     */
64    public function assertKeyTypeCapable(): void
65    {
66        if (!$this->keyType->primaryKeyCapable()) {
67            throw new \InvalidArgumentException(
68                "#[Morphs(name: '{$this->name}')] declares keyType [{$this->keyType->value}], "
69                . 'which cannot hold a primary-key value; use an integer, string, char, or uuid type.'
70            );
71        }
72    }
73}