Passed
Pull Request — master (#347)
by Def
17:16 queued 15:04
created

TableName::getSchemaName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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 getSchemaName(): ?string
56
    {
57
        return $this->schemaName;
58
    }
59
60
    public function getCatalogName(): ?string
61
    {
62
        return $this->catalogName;
63
    }
64
65
    public function getServerName(): ?string
66
    {
67
        return $this->serverName;
68
    }
69
70
    public function __toString()
71
    {
72
        return implode(static::DELIMITER, array_filter([
73
            $this->serverName,
74
            $this->catalogName,
75
            $this->schemaName,
76
            $this->getTableName(),
77
        ]));
78
    }
79
80
    private function addPrefix(string $name): string
81
    {
82
        if (!str_contains($name, '{{')) {
83
            return $name;
84
        }
85
86
        $name = preg_replace('/{{(.*?)}}/', '\1', $name);
87
88
        return str_replace('%', $this->prefix ?? '', $name);
89
    }
90
}
91