Passed
Push — 6.0 ( ee4eb8...e26713 )
by Olivier
01:40
created

TableDefinition::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
namespace ICanBoogie\ActiveRecord\Config;
4
5
use ICanBoogie\ActiveRecord\Schema;
6
7
use function ICanBoogie\singularize;
8
use function strrpos;
9
use function substr;
10
11
/**
12
 * @internal
13
 *
14
 * A table definition, built during configuration.
15
 */
16
class TableDefinition
17
{
18
    /**
19
     * @param array{
20
     *     name: string,
21
     *     schema: Schema,
22
     *     alias: ?string,
23
     * } $an_array
24
     */
25
    public static function __set_state(array $an_array): self
26
    {
27
        return new self(... $an_array);
28
    }
29
30
    public readonly string $alias;
31
32
    /**
33
     * @param string $name
34
     *     Unprefixed name of the table.
35
     * @param Schema $schema
36
     *     Schema of the table.
37
     */
38
    public function __construct(
39
        public readonly string $name,
40
        public readonly Schema $schema,
41
        ?string $alias = null,
42
    ) {
43
        $this->alias = $alias ?? $this->make_alias($this->name);
0 ignored issues
show
Bug introduced by
The property alias is declared read-only in ICanBoogie\ActiveRecord\Config\TableDefinition.
Loading history...
44
    }
45
46
    private function make_alias(string $name): string
47
    {
48
        $pos = strrpos($name, '_');
49
        $alias = $pos !== false
50
            ? substr($name, $pos + 1)
51
            : $name;
52
53
        return singularize($alias);
54
    }
55
}
56