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

Table   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 66
rs 10
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A hasSchema() 0 3 2
A getAlias() 0 3 1
A hasAlias() 0 3 2
A __construct() 0 5 1
A getName() 0 3 1
A create() 0 6 4
A getSchema() 0 3 1
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