Passed
Pull Request — master (#366)
by Sergei
03:39
created

CustomTableNameTrait::withTableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\ActiveRecord\Trait;
6
7
/**
8
 * Trait to implement custom table name for ActiveRecord.
9
 *
10
 * @see ActiveRecordInterface::getTableName()
11
 */
12
trait CustomTableNameTrait
13
{
14
    private string $tableName;
15
16
    /**
17
     * Sets the table name for the ActiveRecord.
18
     */
19
    public function withTableName($tableName): static
20
    {
21
        $new = clone $this;
22
        $new->tableName = $tableName;
23
        return $new;
24
    }
25
26
    public function getTableName(): string
27
    {
28
        return $this->tableName ??= parent::getTableName();
29
    }
30
}
31