Passed
Pull Request — master (#347)
by Def
04:44 queued 02:40
created

TableName::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Schema;
6
7
use Yiisoft\Db\Expression\ExpressionInterface;
8
9
/**
10
 * TableName - abstraction for name of table in DataBase
11
 */
12
class TableName implements TableNameInterface
13
{
14
    private const DELIMITER = '.';
15
16
    private string|ExpressionInterface $tableName;
17
    private ?string $prefix;
18
    private ?string $schemaName;
19
    private ?string $catalogName;
20
    private ?string $serverName;
21
22
    public function __construct(
23
        string|ExpressionInterface $tableName,
24
        ?string $schemaName = null,
25
        ?string $catalogName = null,
26
        ?string $serverName = null
27
    ) {
28
        $this->tableName = $tableName;
29
        $this->schemaName = $schemaName;
30
        $this->catalogName = $catalogName;
31
        $this->serverName = $serverName;
32
    }
33
34
    public function getTableName(): string
35
    {
36
        return $this->addPrefix($this->tableName);
0 ignored issues
show
Bug introduced by
It seems like $this->tableName can also be of type Yiisoft\Db\Expression\ExpressionInterface; however, parameter $name of Yiisoft\Db\Schema\TableName::addPrefix() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
        return $this->addPrefix(/** @scrutinizer ignore-type */ $this->tableName);
Loading history...
37
    }
38
39
    public function getRawTableName(): string|ExpressionInterface
40
    {
41
        return $this->tableName;
42
    }
43
44
    public function getPrefix(): ?string
45
    {
46
        return $this->prefix;
47
    }
48
49
    public function setPrefix(?string $prefix = null): static
50
    {
51
        $this->prefix = $prefix;
52
        return $this;
53
    }
54
55
    public function withPrefix(?string $prefix = null): static
56
    {
57
        $new = clone $this;
58
        $new->prefix = $prefix;
59
        return $new;
60
    }
61
62
    public function getSchemaName(): ?string
63
    {
64
        return $this->schemaName;
65
    }
66
67
    public function getCatalogName(): ?string
68
    {
69
        return $this->catalogName;
70
    }
71
72
    public function getServerName(): ?string
73
    {
74
        return $this->serverName;
75
    }
76
77
    public function __toString()
78
    {
79
        return implode(static::DELIMITER, array_filter([
80
            $this->serverName,
81
            $this->catalogName,
82
            $this->schemaName,
83
            $this->getTableName(),
84
        ]));
85
    }
86
87
    private function addPrefix(string $name): string
88
    {
89
        if (!str_contains($name, '{{')) {
90
            return $name;
91
        }
92
93
        $name = preg_replace('/{{(.*?)}}/', '\1', $name);
94
95
        return str_replace('%', $this->prefix ?? '', $name);
96
    }
97
}
98