Passed
Branch master (64c2be)
by Henri
07:23 queued 34s
created
src/Datamanager.php 1 patch
Spacing   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -8,7 +8,7 @@  discard block
 block discarded – undo
8 8
 {
9 9
     use DataTrait;
10 10
     
11
-    private array $where = [''=> ["1",'=',"1"] ];
11
+    private array $where = [''=> ["1", '=', "1"]];
12 12
 
13 13
     private function mountTable_Field(string $field, $value = null)
14 14
     {
@@ -20,16 +20,16 @@  discard block
 block discarded – undo
20 20
         $type = $value;
21 21
         $maxlength = null;
22 22
 
23
-        if(strpos($value,'(')){
24
-            $type = (in_array( substr($value, 0, strpos($value,'(')) , ['varchar','char','text'])) ? 'string' : $type;
25
-            $type = (in_array( substr($value, 0, strpos($value,'(')) , ['tinyint','mediumint','smallint','bigtint','int'])) ? 'int' : $type;
26
-            $type = (in_array( substr($value, 0, strpos($value,'(')) , ['decimal','float','double','real'])) ? 'float' : $type;
23
+        if (strpos($value, '(')) {
24
+            $type = (in_array(substr($value, 0, strpos($value, '(')), ['varchar', 'char', 'text'])) ? 'string' : $type;
25
+            $type = (in_array(substr($value, 0, strpos($value, '(')), ['tinyint', 'mediumint', 'smallint', 'bigtint', 'int'])) ? 'int' : $type;
26
+            $type = (in_array(substr($value, 0, strpos($value, '(')), ['decimal', 'float', 'double', 'real'])) ? 'float' : $type;
27 27
         }
28 28
 
29
-        $maxlength = (in_array( $type , ['string','float','int'])) ? substr($value,(strpos($value,'(')+1),-1) : $maxlength;
30
-        $maxlength = (in_array( $type , ['date'])) ? 10 : $maxlength;
31
-        $maxlength = (in_array( $type , ['datetime'])) ? 19 : $maxlength;
32
-        $maxlength = (in_array( $type , ['boolean'])) ? 1 : $maxlength;
29
+        $maxlength = (in_array($type, ['string', 'float', 'int'])) ? substr($value, (strpos($value, '(') + 1), -1) : $maxlength;
30
+        $maxlength = (in_array($type, ['date'])) ? 10 : $maxlength;
31
+        $maxlength = (in_array($type, ['datetime'])) ? 19 : $maxlength;
32
+        $maxlength = (in_array($type, ['boolean'])) ? 1 : $maxlength;
33 33
 
34 34
         $this->$field = ['maxlength' => $maxlength];
35 35
         $this->$field = ['type' => $type];
@@ -72,11 +72,11 @@  discard block
 block discarded – undo
72 72
 
73 73
     public function check_where_array(array $where)
74 74
     {
75
-        if(count($where) != 3){
76
-            throw new Exception("Condition where set incorrectly: ".implode(' ',$where));
75
+        if (count($where) != 3) {
76
+            throw new Exception("Condition where set incorrectly: ".implode(' ', $where));
77 77
         }
78 78
 
79
-        if(!array_key_exists($where[0],$this->data) && $this->full){
79
+        if (!array_key_exists($where[0], $this->data) && $this->full) {
80 80
             throw new Exception("{$where[0]} field does not exist in the table {$this->table}.");
81 81
         }
82 82
     }
@@ -84,14 +84,14 @@  discard block
 block discarded – undo
84 84
     private function mountRemove(): array
85 85
     {
86 86
         $return = ['data' => [], 'where' => ''];
87
-        foreach($this->where as $clause => $condition){
88
-            if(strlen($clause) === 0){
87
+        foreach ($this->where as $clause => $condition) {
88
+            if (strlen($clause) === 0) {
89 89
                 $return['where'] .= " {$clause} {$condition[0]} {$condition[1]} :q_{$condition[0]} ";
90 90
                 $return['data'] .= "q_{$condition[0]}={$condition[2]}&";
91 91
                 continue;
92 92
             }
93 93
                 
94
-            foreach($condition as $value){
94
+            foreach ($condition as $value) {
95 95
                 $return['where'] .= " {$clause} {$value[0]} {$value[1]} :q_{$value[0]} ";
96 96
                 $return['data'] .= "q_{$value[0]}={$value[2]}&";
97 97
             }
@@ -104,11 +104,11 @@  discard block
 block discarded – undo
104 104
         $return = ['data' => []];
105 105
 
106 106
         foreach ($this->data as $key => $value) {
107
-            if(strstr($this->data[$key]['extra'],'auto_increment') && $key !== $this->primary){
107
+            if (strstr($this->data[$key]['extra'], 'auto_increment') && $key !== $this->primary) {
108 108
                 continue;
109 109
             }
110 110
 
111
-            if(($this->data[$key]['changed'] && $this->data[$key]['upgradeable']) || $this->primary === $key){
111
+            if (($this->data[$key]['changed'] && $this->data[$key]['upgradeable']) || $this->primary === $key) {
112 112
                 $return['data'][$key] = $this->data[$key]['value'];
113 113
             }
114 114
         }
@@ -120,7 +120,7 @@  discard block
 block discarded – undo
120 120
     {
121 121
         $this->transaction('begin');
122 122
 
123
-        try{
123
+        try {
124 124
             $this->update(
125 125
                 $this->mountSave()['data'],
126 126
                 "{$this->primary}=:{$this->primary}", 
@@ -130,7 +130,7 @@  discard block
 block discarded – undo
130 130
             $this->check_fail();
131 131
 
132 132
             $this->transaction('commit');
133
-        }catch(Exception $er){
133
+        } catch (Exception $er) {
134 134
             $this->transaction('rollback');
135 135
             throw $er;
136 136
         }
@@ -146,7 +146,7 @@  discard block
 block discarded – undo
146 146
 
147 147
             $key = (!$key) ? '' : " {$key} ";
148 148
 
149
-            if(is_array($value[0])){
149
+            if (is_array($value[0])) {
150 150
 
151 151
                 foreach ($value as $k => $v) {
152 152
                     $return['where'] .= " {$key} {$v[0]} {$v[1]} :q_{$v[0]} ";
@@ -165,21 +165,21 @@  discard block
 block discarded – undo
165 165
 
166 166
     private function mountSelect()
167 167
     {
168
-        $select = implode(',',array_keys($this->select));
168
+        $select = implode(',', array_keys($this->select));
169 169
 
170
-        $this->query = str_replace('*', $select,$this->query);
170
+        $this->query = str_replace('*', $select, $this->query);
171 171
     }
172 172
 
173 173
     private function mountLimit()
174 174
     {
175
-        if(!is_null($this->limit)){
175
+        if (!is_null($this->limit)) {
176 176
             $this->query .= " LIMIT {$this->limit}";
177 177
         }
178 178
     }
179 179
 
180 180
     private function mountOffset()
181 181
     {
182
-        if(!is_null($this->offset)){
182
+        if (!is_null($this->offset)) {
183 183
             $this->query .= " OFFSET {$this->offset}";
184 184
         }
185 185
     }
Please login to merge, or discard this patch.
src/DataTrait.php 1 patch
Spacing   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -4,7 +4,7 @@  discard block
 block discarded – undo
4 4
 
5 5
 use Exception;
6 6
 
7
-trait DataTrait{
7
+trait DataTrait {
8 8
     use CrudTrait;
9 9
     
10 10
     protected ?string $table = null;
@@ -36,16 +36,16 @@  discard block
 block discarded – undo
36 36
         return $this;
37 37
     }
38 38
 
39
-    public function __set(string $prop,$value)
39
+    public function __set(string $prop, $value)
40 40
     {
41 41
 
42
-        if(is_array($value)){
42
+        if (is_array($value)) {
43 43
             $attr = array_keys($value)[0];
44 44
             $this->data[$prop][$attr] = $value[$attr];
45 45
             return $this;
46 46
         }
47 47
 
48
-        if($this->full && !array_key_exists($prop,$this->data)){
48
+        if ($this->full && !array_key_exists($prop, $this->data)) {
49 49
             throw new Exception("{$prop} field does not exist in the table {$this->table}.");
50 50
         }
51 51
 
@@ -62,7 +62,7 @@  discard block
 block discarded – undo
62 62
 
63 63
     public function __get(string $field)
64 64
     {
65
-        if($this->full && !array_key_exists($field,$this->data)){
65
+        if ($this->full && !array_key_exists($field, $this->data)) {
66 66
             throw new Exception("{$field} field does not exist in the table {$this->table}.");
67 67
         }
68 68
 
@@ -79,7 +79,7 @@  discard block
 block discarded – undo
79 79
         $deniable = (is_array($deniable)) ? $deniable : [$deniable];
80 80
 
81 81
         foreach ($deniable as $field) {
82
-            if(!array_key_exists($field,$this->data)){
82
+            if (!array_key_exists($field, $this->data)) {
83 83
                 throw new Exception("{$field} field does not exist in the table {$this->table}.");
84 84
             }
85 85
 
@@ -99,11 +99,11 @@  discard block
 block discarded – undo
99 99
 
100 100
     public function orderBy(string $field, string $ord = 'ASC')
101 101
     {
102
-        if(!array_key_exists(str_replace(['asc','ASC','desc','DESC',' '],'',$field),$this->data) && $this->full){
102
+        if (!array_key_exists(str_replace(['asc', 'ASC', 'desc', 'DESC', ' '], '', $field), $this->data) && $this->full) {
103 103
             throw new Exception("{$field} field does not exist in the table {$this->table}.");
104 104
         }
105 105
 
106
-        if(strpos(strtolower($field),'asc') || strpos(strtolower($field),'desc')){
106
+        if (strpos(strtolower($field), 'asc') || strpos(strtolower($field), 'desc')) {
107 107
             $ord = '';
108 108
         }
109 109
 
@@ -118,7 +118,7 @@  discard block
 block discarded – undo
118 118
 
119 119
         foreach ($params as $field) {
120 120
 
121
-            if(!array_key_exists($field,$this->data) && $this->full){
121
+            if (!array_key_exists($field, $this->data) && $this->full) {
122 122
                 throw new Exception("{$field} field does not exist in the table {$this->table}.");
123 123
             }
124 124
 
@@ -131,11 +131,11 @@  discard block
 block discarded – undo
131 131
 
132 132
     public function where(array $where)
133 133
     {
134
-        $this->where['AND'] = (array_key_exists('AND',$this->where)) ?? '';
134
+        $this->where['AND'] = (array_key_exists('AND', $this->where)) ?? '';
135 135
         $w = [];
136 136
         foreach ($where as $condition => $values) {
137 137
 
138
-            if(!is_array($values)){
138
+            if (!is_array($values)) {
139 139
                 $w['AND'][] = $values;
140 140
                 continue;
141 141
             }
@@ -146,7 +146,7 @@  discard block
 block discarded – undo
146 146
                        
147 147
         }
148 148
 
149
-        $this->where = array_merge($this->where,$w);
149
+        $this->where = array_merge($this->where, $w);
150 150
 
151 151
         return $this;
152 152
     }
@@ -159,7 +159,7 @@  discard block
 block discarded – undo
159 159
 
160 160
     public function offset(int $offset)
161 161
     {
162
-        if(is_null($this->limit)){
162
+        if (is_null($this->limit)) {
163 163
             throw new Exception("The limit must be set before the offset.");
164 164
         }
165 165
 
@@ -189,7 +189,7 @@  discard block
 block discarded – undo
189 189
 
190 190
         foreach ($arrayValues as $key => $value) {
191 191
 
192
-            if(!array_key_exists($key,$this->data)){
192
+            if (!array_key_exists($key, $this->data)) {
193 193
                 throw new Exception("{$key} field does not exist in the table {$this->table}.");
194 194
             }
195 195
 
@@ -203,7 +203,7 @@  discard block
 block discarded – undo
203 203
         $string = '';
204 204
         foreach ($this->data as $key => $value) {
205 205
 
206
-            if(gettype($value)==='object'){
206
+            if (gettype($value) === 'object') {
207 207
                 $value = $value->getData()[$this->primary]['value'];
208 208
             }
209 209
 
@@ -214,19 +214,19 @@  discard block
 block discarded – undo
214 214
 
215 215
     public function remove(?bool $exec = false)
216 216
     {
217
-        if(!$exec){
217
+        if (!$exec) {
218 218
             $this->clause = 'remove';    
219 219
             return $this;
220 220
         }
221 221
 
222 222
         $this->clause = null;
223 223
 
224
-        if(count($this->where) == 1){
224
+        if (count($this->where) == 1) {
225 225
             $this->removeById();
226 226
             return $this;
227 227
         }
228 228
 
229
-        $this->delete($this->mountRemove()['where'], substr( $this->mountRemove()['data'] ,0,-1) );
229
+        $this->delete($this->mountRemove()['where'], substr($this->mountRemove()['data'], 0, -1));
230 230
 
231 231
         $this->check_fail();
232 232
             
@@ -235,7 +235,7 @@  discard block
 block discarded – undo
235 235
 
236 236
     private function removeById(): bool
237 237
     {
238
-        $delete = $this->delete("{$this->primary}=:{$this->primary}","{$this->primary}={$this->getData()[$this->primary]['value']}");
238
+        $delete = $this->delete("{$this->primary}=:{$this->primary}", "{$this->primary}={$this->getData()[$this->primary]['value']}");
239 239
 
240 240
         $this->check_fail();
241 241
 
@@ -249,11 +249,11 @@  discard block
 block discarded – undo
249 249
         $data = [];
250 250
 
251 251
         foreach ($this->data as $key => $value) {
252
-            if(strstr($this->data[$key]['extra'],'auto_increment')){
252
+            if (strstr($this->data[$key]['extra'], 'auto_increment')) {
253 253
                 continue;
254 254
             }
255 255
 
256
-            if(strlen($value['value']) > $value['maxlength']){
256
+            if (strlen($value['value']) > $value['maxlength']) {
257 257
                 throw new Exception("The information provided for column {$key} of table {$this->table} exceeded that allowed.");
258 258
             }
259 259
 
@@ -263,7 +263,7 @@  discard block
 block discarded – undo
263 263
         }
264 264
 
265 265
         $this->transaction('begin');
266
-        try{
266
+        try {
267 267
            
268 268
             $id = $this->insert($data);
269 269
 
@@ -273,7 +273,7 @@  discard block
 block discarded – undo
273 273
             
274 274
             $this->transaction('commit');
275 275
 
276
-        }catch(Exception $er){
276
+        } catch (Exception $er) {
277 277
             $this->transaction('rollback');
278 278
             throw $er;
279 279
         }
@@ -283,13 +283,13 @@  discard block
 block discarded – undo
283 283
 
284 284
     public function toEntity()
285 285
     {
286
-        if($this->getCount() === 0){
286
+        if ($this->getCount() === 0) {
287 287
             return null;
288 288
         }
289 289
 
290 290
         $entity = $this->setByDatabase($this->result[0]);
291 291
 
292
-        if(count($this->result) > 1){
292
+        if (count($this->result) > 1) {
293 293
             $entity = [];
294 294
             foreach ($this->result as $key => $value) {
295 295
                 $entity[] = $this->setByDatabase($value);
@@ -301,13 +301,13 @@  discard block
 block discarded – undo
301 301
 
302 302
     public function findById($id)
303 303
     {
304
-        $this->where([$this->primary,'=',$id]);
304
+        $this->where([$this->primary, '=', $id]);
305 305
         return $this;
306 306
     }
307 307
 
308 308
     public function execute()
309 309
     {
310
-        if(!is_null($this->clause) && $this->clause == 'remove'){
310
+        if (!is_null($this->clause) && $this->clause == 'remove') {
311 311
             return $this->remove(true);
312 312
         }
313 313
 
@@ -315,7 +315,7 @@  discard block
 block discarded – undo
315 315
         
316 316
         $this->mountSelect();
317 317
         
318
-        $where = substr($this->mountWhereExec()['where'],0,-1);
318
+        $where = substr($this->mountWhereExec()['where'], 0, -1);
319 319
         $this->query .= " WHERE {$where} ";
320 320
 
321 321
         $this->query .= $this->order;
@@ -337,7 +337,7 @@  discard block
 block discarded – undo
337 337
     {
338 338
         $this->query = " SELECT * FROM {$this->table} ";
339 339
 
340
-        if(is_int($key)){
340
+        if (is_int($key)) {
341 341
             return $this->findById($key);
342 342
         }
343 343
 
Please login to merge, or discard this patch.