Passed
Pull Request — master (#247)
by Def
12:25
created

Table::create()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Schema;
6
7
use Yiisoft\Db\Expression\ExpressionInterface;
8
9
/**
10
 * @example
11
 * (new Query)->select('col1')->from(Table::create('table1', 't', 'dbo'))
12
 *
13
 * Note: We must use prefix (from connection) with tables with scema equal defaultSchema or without schema and don't use with other schemas
14
 * Note: With ExpressionInterface as tablename - we cannot add prefixes and quoting of table names.
15
 * For example with Oracle: (new Query)->select('*')->from(new Expression('dblink1.dbo.table')) for build `select * from dblink1.dbo.table1`
16
 *
17
 */
18
final class Table
19
{
20
    /**
21
     * @var string|ExpressionInterface
22
     */
23
    private $name;
24
25
    /**
26
     * @var string|null
27
     */
28
    private ?string $alias;
29
30
    /**
31
     * @var string|ExpressionInterface|null
32
     */
33
    private $schema;
34
35
    /**
36
     * @param string|ExpressionInterface $name
37
     * @param string|ExpressionInterface|null $alias
38
     * @param string|ExpressionInterface|null $schema
39
     */
40
    private function __construct($name, $alias, $schema = null)
41
    {
42
        $this->name = $name;
43
        $this->alias = $alias;
0 ignored issues
show
Documentation Bug introduced by
It seems like $alias can also be of type Yiisoft\Db\Expression\ExpressionInterface. However, the property $alias is declared as type null|string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
44
        $this->schema = $schema;
45
    }
46
47
    public static function create($name, ?string $alias = null, $schema = null)
48
    {
49
        assert(is_string($name) || $name instanceof ExpressionInterface);
50
        assert(is_string($schema) || $schema instanceof ExpressionInterface || $schema === null);
51
52
        return new self($name, $alias, $schema);
53
    }
54
55
    /**
56
     * @return string|ExpressionInterface
57
     */
58
    public function getName()
59
    {
60
        return $this->name;
61
    }
62
63
    public function getAlias(): ?string
64
    {
65
        return $this->alias;
66
    }
67
68
    /**
69
     * @return string|ExpressionInterface|null
70
     */
71
    public function getSchema()
72
    {
73
        return $this->schema;
74
    }
75
76
    public function hasAlias(): bool
77
    {
78
        return !($this->alias === null || $this->alias === '');
79
    }
80
81
    public function hasSchema(): bool
82
    {
83
        return !($this->schema === null || $this->schema === '');
84
    }
85
}
86