| Total Complexity | 43 |
| Total Lines | 243 |
| Duplicated Lines | 0 % |
| Changes | 16 | ||
| Bugs | 0 | Features | 2 |
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 DataTrait, SynchronizeTrait, EntityTrait; |
||
| 10 | |||
| 11 | protected ?string $table = null; |
||
| 12 | protected ?string $primary = null; |
||
| 13 | protected array $data = []; |
||
| 14 | protected array $where = [''=> ["1",'=',"1"] ]; |
||
| 15 | protected array $between = []; |
||
| 16 | |||
| 17 | public function __set(string $prop,$value) |
||
| 18 | { |
||
| 19 | if(is_array($value)){ |
||
| 20 | $attr = array_keys($value)[0]; |
||
| 21 | $this->data[$prop][$attr] = $value[$attr]; |
||
| 22 | return $this; |
||
| 23 | } |
||
| 24 | |||
| 25 | if($this->full){ |
||
| 26 | switch($this->data[$prop]['type']){ |
||
| 27 | case 'date': |
||
| 28 | $value = (date_format( date_create_from_format(DATAMANAGER_CONFIG['dateformat'],$value) , 'Y-m-d')); |
||
| 29 | break; |
||
| 30 | } |
||
| 31 | } |
||
| 32 | |||
| 33 | $this->isSettable($prop); |
||
| 34 | |||
| 35 | $this->data[$prop]['changed'] = true; |
||
| 36 | $this->data[$prop]['value'] = $value; |
||
| 37 | |||
| 38 | return $this; |
||
| 39 | } |
||
| 40 | |||
| 41 | public function getData(): ?array |
||
| 42 | { |
||
| 43 | return $this->data; |
||
| 44 | } |
||
| 45 | |||
| 46 | public function __get(string $field) |
||
| 47 | { |
||
| 48 | $this->isSettable($field); |
||
| 49 | |||
| 50 | if($this->full){ |
||
| 51 | switch($this->data[$field]['type']){ |
||
| 52 | case 'date': $this->data[$field]['value'] = (date_format( date_create_from_format('Y-m-d' , $this->data[$field]['value'] ) , DATAMANAGER_CONFIG['dateformat'])); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | return $this->data[$field]['value']; |
||
| 57 | } |
||
| 58 | |||
| 59 | public function getCount(): int |
||
| 60 | { |
||
| 61 | return $this->count; |
||
| 62 | } |
||
| 63 | |||
| 64 | public function except($deniable) |
||
| 65 | { |
||
| 66 | $deniable = (is_array($deniable)) ? $deniable : [$deniable]; |
||
| 67 | |||
| 68 | foreach ($deniable as $field) { |
||
| 69 | if(!array_key_exists($field,$this->data)){ |
||
| 70 | throw new DatamanagerException("{$field} field does not exist in the table {$this->table}."); |
||
| 71 | } |
||
| 72 | |||
| 73 | $this->excepts[$field] = true; |
||
| 74 | } |
||
| 75 | |||
| 76 | return $this; |
||
| 77 | } |
||
| 78 | |||
| 79 | public function deny() |
||
| 80 | { |
||
| 81 | foreach ($this->excepts as $field => $value) { |
||
| 82 | unset($this->select[$field]); |
||
| 83 | } |
||
| 84 | return $this; |
||
| 85 | } |
||
| 86 | |||
| 87 | public function orderBy(string $field, string $ord = 'ASC') |
||
| 88 | { |
||
| 89 | $this->isSettable( str_replace(['asc','ASC','desc','DESC',' '],'',$field) ); |
||
| 90 | |||
| 91 | $ord = (strpos(strtolower($field),'asc') || strpos(strtolower($field),'desc')) ? '' : $ord; |
||
| 92 | |||
| 93 | $this->order = " ORDER BY {$field} {$ord} "; |
||
| 94 | return $this; |
||
| 95 | } |
||
| 96 | |||
| 97 | public function only($params) |
||
| 98 | { |
||
| 99 | $params = (is_array($params)) ? $params : [$params]; |
||
| 100 | $this->select = []; |
||
| 101 | |||
| 102 | foreach ($params as $field) { |
||
| 103 | |||
| 104 | $this->isSettable($field); |
||
| 105 | |||
| 106 | $this->select[$field] = true; |
||
| 107 | } |
||
| 108 | $this->select[$this->primary] = true; |
||
| 109 | |||
| 110 | return $this; |
||
| 111 | } |
||
| 112 | |||
| 113 | public function where(array $where) |
||
| 114 | { |
||
| 115 | $this->where['AND'] = (array_key_exists('AND',$this->where)) ?? ''; |
||
| 116 | $w = []; |
||
| 117 | foreach ($where as $condition => $values) { |
||
| 118 | |||
| 119 | if(!is_array($values)){ |
||
| 120 | $w['AND'][] = $values; |
||
| 121 | continue; |
||
| 122 | } |
||
| 123 | |||
| 124 | $this->check_where_array($values); |
||
| 125 | |||
| 126 | $w[(is_int($condition) ? 'AND' : $condition)][] = $values; |
||
| 127 | |||
| 128 | } |
||
| 129 | |||
| 130 | $this->where = array_merge($this->where,$w); |
||
| 131 | |||
| 132 | return $this; |
||
| 133 | } |
||
| 134 | |||
| 135 | public function between(array $bet) |
||
| 136 | { |
||
| 137 | $this->between = array_merge($this->between, $bet); |
||
| 138 | return $this; |
||
| 139 | } |
||
| 140 | |||
| 141 | public function limit(string $limit) |
||
| 142 | { |
||
| 143 | $this->limit = $limit; |
||
| 144 | return $this; |
||
| 145 | } |
||
| 146 | |||
| 147 | public function offset(int $offset) |
||
| 148 | { |
||
| 149 | $this->checkLimit(); |
||
| 150 | |||
| 151 | $this->offset = $offset; |
||
| 152 | return $this; |
||
| 153 | } |
||
| 154 | |||
| 155 | public function result(): array |
||
| 156 | { |
||
| 157 | return $this->result; |
||
| 158 | } |
||
| 159 | |||
| 160 | public function first() |
||
| 161 | { |
||
| 162 | return (count($this->result) > 0) ? $this->setByDatabase($this->result[0]) : $this; |
||
| 163 | } |
||
| 164 | |||
| 165 | public function setByDatabase(array $arrayValues) |
||
| 166 | { |
||
| 167 | $clone = clone $this; |
||
| 168 | |||
| 169 | $clone->result = [ |
||
| 170 | 0 => $this->result[0] |
||
| 171 | ]; |
||
| 172 | |||
| 173 | $clone->count = 1; |
||
| 174 | |||
| 175 | foreach ($arrayValues as $key => $value) { |
||
| 176 | |||
| 177 | $this->isSettable($key); |
||
| 178 | |||
| 179 | $clone->data[$key]['value'] = $value; |
||
| 180 | |||
| 181 | } |
||
| 182 | return $clone; |
||
| 183 | } |
||
| 184 | |||
| 185 | public function toJson(): string |
||
| 186 | { |
||
| 187 | $string = ''; |
||
| 188 | foreach ($this->data as $key => $value) { |
||
| 189 | |||
| 190 | if(gettype($value)==='object'){ |
||
| 191 | $value = $value->getData()[$this->primary]['value']; |
||
| 192 | } |
||
| 193 | |||
| 194 | $string .= '"'.$key.'"'.':"'.$value.'",'; |
||
| 195 | } |
||
| 196 | return str_replace(',}', '}', '{'.$string.'}'); |
||
| 197 | } |
||
| 198 | |||
| 199 | private function removeById(): bool |
||
| 206 | } |
||
| 207 | |||
| 208 | public function findById($id) |
||
| 209 | { |
||
| 210 | return $this->where([$this->primary,'=',$id]); |
||
| 211 | } |
||
| 212 | |||
| 213 | public function execute() |
||
| 214 | { |
||
| 215 | if(!is_null($this->clause) && $this->clause == 'remove'){ |
||
| 216 | return $this->remove(true); |
||
| 217 | } |
||
| 218 | |||
| 219 | $this->deny(); |
||
| 220 | |||
| 221 | $this->mountSelect(); |
||
| 222 | |||
| 223 | $where = substr($this->mountWhereExec()['where'],0,-1); |
||
| 224 | $where .= substr($this->mountBetweenExec()['where'],0,-1); |
||
| 225 | |||
| 226 | $this->query .= " WHERE {$where} "; |
||
| 227 | |||
| 228 | $this->query .= $this->order; |
||
| 229 | |||
| 230 | $this->mountLimit(); |
||
| 231 | $this->mountOffset(); |
||
| 232 | |||
| 233 | $this->result = $this->select( |
||
| 234 | $this->query, |
||
| 235 | array_merge($this->mountWhereExec()['data'], $this->mountBetweenExec()['data']) |
||
| 236 | ); |
||
| 237 | |||
| 238 | $this->check_fail(); |
||
| 239 | |||
| 240 | $this->count = count($this->result); |
||
| 241 | $this->query = null; |
||
| 242 | |||
| 243 | return $this; |
||
| 244 | } |
||
| 245 | |||
| 246 | public function find(?int $key = null) |
||
| 250 | } |
||
| 251 | |||
| 252 | |||
| 253 | |||
| 254 | } |
||
| 255 |
This check looks for private methods that have been defined, but are not used inside the class.