Completed
Push — master ( c5c126...29bff4 )
by Edson
01:37
created

Schema   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 78
ccs 0
cts 58
cp 0
rs 10
c 0
b 0
f 0
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A onDelete() 0 5 2
A getColumns() 0 19 3
A __construct() 0 3 1
A create() 0 9 1
A run() 0 4 1
A onUpdate() 0 5 2
A fk() 0 5 2
A pk() 0 5 2
1
<?php
2
3
namespace Bonfim\ActiveRecord;
4
5
abstract class Schema extends ActiveRecord
6
{
7
    abstract public function up();
8
    abstract public function down();
9
10
    public function __construct()
11
    {
12
        $this->table = new Table();
13
    }
14
15
    public $sql = '';
16
17
    private $table;
18
19
    public function create(string $name, $callback)
20
    {
21
        call_user_func_array($callback, [$this->table]);
22
23
        $q = "CREATE TABLE $name ({$this->getColumns()}";
24
        $q .= $this->pk().$this->fk().$this->onUpdate().$this->onDelete();
25
        $q .= "\n) ENGINE=INNODB;";
26
27
        $this->sql = $q;
28
    }
29
30
    private function pk()
31
    {
32
        return (!empty($pk = $this->table->getPk()))
0 ignored issues
show
Bug introduced by
The method getPk() does not exist on Bonfim\ActiveRecord\Table. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

32
        return (!empty($pk = $this->table->/** @scrutinizer ignore-call */ getPk()))
Loading history...
33
            ? $this->sql .= ",\n\tPRIMARY KEY ($pk)"
34
            : "";
35
    }
36
37
    private function fk()
38
    {
39
        return (!empty($fk = $this->table->getFk()))
0 ignored issues
show
Bug introduced by
The method getFk() does not exist on Bonfim\ActiveRecord\Table. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

39
        return (!empty($fk = $this->table->/** @scrutinizer ignore-call */ getFk()))
Loading history...
40
            ? ",\n\t$fk {$this->table->getReferences()}"
0 ignored issues
show
Bug introduced by
The method getReferences() does not exist on Bonfim\ActiveRecord\Table. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

40
            ? ",\n\t$fk {$this->table->/** @scrutinizer ignore-call */ getReferences()}"
Loading history...
41
            : "";
42
    }
43
44
    private function onUpdate()
45
    {
46
        return (!empty($on = $this->table->getOnupdate()))
0 ignored issues
show
Bug introduced by
The method getOnupdate() does not exist on Bonfim\ActiveRecord\Table. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

46
        return (!empty($on = $this->table->/** @scrutinizer ignore-call */ getOnupdate()))
Loading history...
47
            ? " $on"
48
            : "";
49
    }
50
51
    private function onDelete()
52
    {
53
        return (!empty($on = $this->table->getOndelete()))
0 ignored issues
show
Bug introduced by
The method getOndelete() does not exist on Bonfim\ActiveRecord\Table. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

53
        return (!empty($on = $this->table->/** @scrutinizer ignore-call */ getOndelete()))
Loading history...
54
            ? " $on"
55
            : "";
56
    }
57
58
    private function getColumns()
59
    {
60
        $q = "\n\t";
61
62
        $columns = $this->table->getColumns();
63
        $count = count($columns);
64
65
        reset($columns);
66
67
        for ($i = 0; $i < $count; $i++) {
68
            $key = key($columns);
69
            $q .= key($columns) . ' ' . $columns[$key];
70
            if ($i < $count - 1) {
71
                $q .= ",\n\t";
72
            }
73
            next($columns);
74
        }
75
76
        return $q;
77
    }
78
79
    public function run()
80
    {
81
        var_dump($this->sql);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($this->sql) looks like debug code. Are you sure you do not want to remove it?
Loading history...
82
        self::exec($this->sql);
83
    }
84
}
85