Passed
Push — master ( ca0405...0b9135 )
by Henri
01:27
created

SynchronizeTrait::mountTable_Null()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
namespace HnrAzevedo\Datamanager;
4
5
trait SynchronizeTrait{
6
    use CrudTrait;
7
8
    protected ?string $table = null;
9
    protected ?string $primary = null;
10
    protected bool $full = false;
11
    protected static ?array $describe = null;
12
13
    protected function synchronize(string $table, string $primary)
14
    {
15
        $this->table = $table;
16
        $this->primary = $primary;
17
        self::$describe ??= $this->describe();
18
        
19
        $this->check_fail();
20
21
        $this->mountData(self::$describe);
0 ignored issues
show
Bug introduced by
It seems like self::describe can also be of type null; however, parameter $table of HnrAzevedo\Datamanager\S...onizeTrait::mountData() does only seem to accept array, 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

21
        $this->mountData(/** @scrutinizer ignore-type */ self::$describe);
Loading history...
22
        $this->full = true;
23
        return $this;
24
    }
25
26
    protected function mountData(array $table): Datamanager
27
    {
28
        foreach ($table as $column) {
29
            foreach ($column as $propriety => $value) {
30
                $method = "mountTable_{$propriety}";
31
                $this->$method($column['Field'], $value);
32
            }
33
        }
34
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type HnrAzevedo\Datamanager\SynchronizeTrait which includes types incompatible with the type-hinted return HnrAzevedo\Datamanager\Datamanager.
Loading history...
35
    }
36
37
    protected function mountTable_Field(string $field, $value = null)
38
    {
39
        $this->$field = null;
40
    }
41
42
    protected function mountTable_Type(string $field, $value = null)
43
    {
44
        $type = $value;
45
46
        if(strpos($value,'(')){
47
            $type = (in_array( substr($value, 0, strpos($value,'(')) , ['varchar','char','text'])) ? 'string' : $type;
48
            $type = (in_array( substr($value, 0, strpos($value,'(')) , ['tinyint','mediumint','smallint','bigtint','int'])) ? 'int' : $type;
49
            $type = (in_array( substr($value, 0, strpos($value,'(')) , ['decimal','float','double','real'])) ? 'float' : $type;
50
        }
51
52
        $this->mountTable_Maxlength($field, $type, $value);
53
        $this->$field = ['type' => $type];
54
    }
55
56
    protected function mountTable_Maxlength(string $field, string $type, $default = null)
57
    {
58
        $maxlength = (in_array( $type , ['string','float','int'])) ? substr($default,(strpos($default,'(')+1),-1) : 0;
59
        $maxlength = (in_array( $type , ['date'])) ? 10 : $maxlength;
60
        $maxlength = (in_array( $type , ['datetime'])) ? 19 : $maxlength;
61
        $maxlength = (in_array( $type , ['boolean'])) ? 1 : $maxlength;
62
        $this->$field = ['maxlength' => $maxlength];
63
    }
64
65
    protected function mountTable_Null(string $field, $value = null)
66
    {
67
        $this->$field = ['null' => ($value === 'YES') ? 1 : 0];
68
    }
69
70
    protected function mountTable_Key(string $field, $value = null)
71
    {
72
        $this->$field = ['key' => $value];
73
        $this->$field = ['upgradeable' => ($value == 'PRI') ? 0 : 1];
74
    }
75
76
    protected function mountTable_Extra(string $field, $value = null)
77
    {
78
        $this->$field = ['extra' => $value];
79
    }
80
81
    protected function mountTable_Default(string $field, $value = null)
82
    {
83
        $this->$field = ['default' => $value];
84
        $this->$field = ['value' => null];
85
        $this->$field = ['changed' => false];
86
        $this->select[$field] = true;
0 ignored issues
show
Bug Best Practice introduced by
The property select does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
87
    }
88
89
}