Total Complexity | 46 |
Total Lines | 184 |
Duplicated Lines | 0 % |
Changes | 10 | ||
Bugs | 0 | Features | 0 |
Complex classes like Datamanager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Datamanager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | abstract class Datamanager |
||
8 | { |
||
9 | use CrudTrait, DataTrait; |
||
10 | |||
11 | private array $result = []; |
||
12 | protected array $data = []; |
||
13 | |||
14 | private bool $full = false; |
||
15 | private ?string $clause = null; |
||
16 | |||
17 | private array $where = [''=> ["1",'=',"1"] ]; |
||
18 | |||
19 | |||
20 | private function mountTable_Field(string $field, $value = null) |
||
21 | { |
||
22 | $this->$field = null; |
||
23 | } |
||
24 | |||
25 | private function mountTable_Type(string $field, $value = null) |
||
26 | { |
||
27 | $type = $value; |
||
28 | $maxlength = null; |
||
29 | |||
30 | if(strpos($value,'(')){ |
||
31 | $type = (array_key_exists( substr($value,0,strpos($value,'(')) , ['varchar','char','text'])) ? 'string' : $type; |
||
32 | $type = (array_key_exists( substr($value,0,strpos($value,'(')) , ['tinyint','mediumint','smallint','bigtint','int'])) ? 'int' : $type; |
||
33 | $type = (array_key_exists( substr($value,0,strpos($value,'(')) , ['decimal','float','double','real'])) ? 'float' : $type; |
||
34 | } |
||
35 | |||
36 | $maxlength = (array_key_exists( $type , ['string','float','int'])) ? substr($value,(strpos($value,'(')+1),-1) : $maxlength; |
||
37 | $maxlength = (array_key_exists( $type , ['date'])) ? 10 : $maxlength; |
||
38 | $maxlength = (array_key_exists( $type , ['datetime'])) ? 19 : $maxlength; |
||
39 | $maxlength = (array_key_exists( $type , ['boolean'])) ? 1 : $maxlength; |
||
40 | |||
41 | $this->$field = ['maxlength' => $maxlength]; |
||
42 | $this->$field = ['type' => $type]; |
||
43 | } |
||
44 | |||
45 | private function mountTable_Null(string $field, $value = null) |
||
46 | { |
||
47 | $this->$field = ['null' => ($value === 'YES') ? 1 : 0]; |
||
48 | } |
||
49 | |||
50 | private function mountTable_Key(string $field, $value = null) |
||
54 | } |
||
55 | |||
56 | private function mountTable_Extra(string $field, $value = null) |
||
57 | { |
||
58 | $this->$field = ['extra' => $value]; |
||
59 | } |
||
60 | |||
61 | private function mountTable_Default(string $field, $value = null) |
||
62 | { |
||
63 | $this->$field = ['default' => $value]; |
||
64 | $this->$field = ['value' => null]; |
||
65 | $this->$field = ['changed' => false]; |
||
66 | $this->select[$field] = true; |
||
67 | } |
||
68 | |||
69 | private function mountData(array $table): Datamanager |
||
78 | } |
||
79 | |||
80 | public function check_where_array(array $where) |
||
81 | { |
||
82 | if(count($where) != 3){ |
||
83 | throw new Exception("Condition where set incorrectly: ".implode(' ',$where)); |
||
84 | } |
||
85 | |||
86 | if(!array_key_exists($where[0],$this->data) && $this->full){ |
||
87 | throw new Exception("{$where[0]} field does not exist in the table {$this->table}."); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | private function mountRemove(): array |
||
92 | { |
||
93 | $return = ['data' => [], 'where' => []]; |
||
94 | foreach($this->where as $clause => $condition){ |
||
95 | if(strlen($clause) === 0){ |
||
96 | $return['where'] .= " {$clause} {$condition[0]} {$condition[1]} :q_{$condition[0]} "; |
||
97 | $return['data'] .= "q_{$condition[0]}={$condition[2]}&"; |
||
98 | continue; |
||
99 | } |
||
100 | |||
101 | foreach($condition as $value){ |
||
102 | $return['where'] .= " {$clause} {$value[0]} {$value[1]} :q_{$value[0]} "; |
||
103 | $return['data'] .= "q_{$value[0]}={$value[2]}&"; |
||
104 | } |
||
105 | } |
||
106 | return $return; |
||
107 | } |
||
108 | |||
109 | private function mountSave(): array |
||
110 | { |
||
111 | $return = ['data' => []]; |
||
112 | |||
113 | foreach ($this->data as $key => $value) { |
||
114 | if(strstr($this->data[$key]['extra'],'auto_increment') && $key !== $this->primary){ |
||
115 | continue; |
||
116 | } |
||
117 | |||
118 | if(($this->data[$key]['changed'] && $this->data[$key]['upgradeable']) || $this->primary === $key){ |
||
119 | $return['data'][$key] = $this->data[$key]['value']; |
||
120 | } |
||
121 | } |
||
122 | |||
123 | return $return; |
||
124 | } |
||
125 | |||
126 | public function save(): Datamanager |
||
146 | } |
||
147 | |||
148 | private function mountWhereExec(): array |
||
149 | { |
||
150 | $return = ['where' => [], 'data' => []]; |
||
151 | |||
152 | foreach ($this->where as $key => $value) { |
||
153 | |||
154 | $key = (!$key) ? '' : " {$key} "; |
||
155 | |||
156 | if(is_array($value[0])){ |
||
157 | |||
158 | foreach ($value as $k => $v) { |
||
159 | $return['where'] .= " {$key} {$v[0]} {$v[1]} :q_{$v[0]} "; |
||
160 | $return['data']["q_{$v[0]}"] = $v[2]; |
||
161 | } |
||
162 | |||
163 | continue; |
||
164 | } |
||
165 | |||
166 | $return['where'] .= " {$key} {$value[0]} {$value[1]} :q_{$value[0]} "; |
||
167 | $return['data']["q_{$value[0]}"] = $value[2]; |
||
168 | |||
169 | } |
||
170 | return $return; |
||
171 | } |
||
172 | |||
173 | private function mountSelect() |
||
178 | } |
||
179 | |||
180 | private function mountLimit() |
||
184 | } |
||
185 | } |
||
186 | |||
187 | private function mountOffset() |
||
191 | } |
||
192 | } |
||
193 | |||
195 |