Passed
Pull Request — master (#368)
by Sergei
02:42
created

CustomTableNameTrait   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 2
eloc 6
c 2
b 0
f 0
dl 0
loc 17
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A withTableName() 0 5 1
A getTableName() 0 3 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(string $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