Completed
Push — master ( 502244...65ec5f )
by Viacheslav
05:58
created

CreateTable::isEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Yaoi\Sql;
4
5
use Yaoi\Database\Definition\Index;
6
use Yaoi\Database\Definition\Table;
7
use Yaoi\String\Quoter;
8
9
abstract class CreateTable extends Batch
10
{
11
    /** @var  Table */
12
    protected $table;
13
14 View Code Duplication
    protected function appendColumns() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
        $utility = $this->database()->getUtility();
16
17
        foreach ($this->table->getColumns(true) as $column) {
0 ignored issues
show
Bug introduced by
The expression $this->table->getColumns(true) of type array|object<Yaoi\Database\Definition\Columns> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
18
            $this->createLines->commaExpr(' ? ' . $utility->getColumnTypeString($column), new Symbol($column->schemaName));
19
        }
20
    }
21
22
23
24
    protected function appendIndexes() {
25
        foreach ($this->table->indexes as $index) {
26
            $columns = Symbol::prepareColumns($index->columns);
27
28
            if ($index->type === Index::TYPE_KEY) {
29
                $this->createLines->commaExpr(' KEY ? (?)', new Symbol($index->getName()), $columns);
30
            }
31
            elseif ($index->type === Index::TYPE_UNIQUE) {
32
                $this->createLines->commaExpr(' UNIQUE KEY ? (?)', new Symbol($index->getName()), $columns);
33
            }
34
        }
35
    }
36
37
    protected function appendForeignKeys() {
38
        if ($this->table->disableForeignKeys) {
39
            return;
40
        }
41
        foreach ($this->table->getForeignKeys() as $foreignKey) {
42
            $this->fkLines->commaExpr($this->database()->getUtility()->generateForeignKeyExpression($foreignKey));
43
        }
44
    }
45
46
    public function appendPrimaryKey() {
47
        $columns = array();
48
        foreach ($this->table->primaryKey as $column) {
49
            $columns []= new Symbol($column->schemaName);
50
        }
51
        $this->createLines->commaExpr(' PRIMARY KEY (:columns)', array('columns' => $columns));
52
    }
53
54
55
    /** @var  Expression */
56
    protected $createLines;
57
58
    /** @var  Expression */
59
    protected $fkLines;
60
61
    public function __construct(Table $table) {
62
        $this->table = $table;
63
        $this->bindDatabase($table->database());
64
        $this->createLines = new SimpleExpression();
65
        $this->createLines->setOpComma(',' . PHP_EOL);
66
        $this->fkLines = new SimpleExpression();
67
        $this->fkLines->setOpComma(',' . PHP_EOL);
68
69
        $createExpression = new SimpleExpression('CREATE TABLE ? (' . PHP_EOL, $this->table);
70
        $this->add($createExpression);
71
72
        $createExpression->appendExpr($this->createLines);
73
        $createExpression->appendExpr(PHP_EOL . ')');
74
75
76
        $this->appendColumns();
77
        $this->appendIndexes();
78
        $this->createLines->commaExpr($this->fkLines);
79
        $this->appendForeignKeys();
80
        $this->appendPrimaryKey();
81
82
        if ($this->createLines->isEmpty()) {
83
            $createExpression->disable();
84
        }
85
    }
86
87
    public function extractForeignKeysStatement()
88
    {
89
        $this->fkLines->disable();
90
        return $this->database()->getUtility()
91
            ->generateAlterTable(new Table(null, $this->database(), '_any_name'), $this->table)
92
            ->extractForeignKeysStatement();
93
    }
94
95
    public function isEmpty()
96
    {
97
        return false;
98
    }
99
}