Total Complexity | 41 |
Total Lines | 145 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like QueryTrait 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 QueryTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | trait QueryTrait |
||
13 | { |
||
14 | /** |
||
15 | * Get the table fields |
||
16 | * |
||
17 | * @param string $table The table name |
||
18 | * @param array $queryOptions The query options |
||
19 | * |
||
20 | * @return array |
||
21 | */ |
||
22 | private function getFields(string $table, array $queryOptions): array |
||
23 | { |
||
24 | // From edit.inc.php |
||
25 | $fields = $this->driver->fields($table); |
||
26 | |||
27 | //!!!! $queryOptions["select"] is never set here !!!!// |
||
28 | |||
29 | $where = $this->driver->where($queryOptions, $fields); |
||
30 | $update = $where; |
||
31 | foreach ($fields as $name => $field) { |
||
32 | $generated = $field->generated ?? false; |
||
33 | if (!isset($field->privileges[$update ? "update" : "insert"]) || |
||
34 | $this->admin->fieldName($field) == "" || $generated) { |
||
35 | unset($fields[$name]); |
||
36 | } |
||
37 | } |
||
38 | |||
39 | return [$fields, $where, $update]; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @param array $fields |
||
44 | * @param array $queryOptions |
||
45 | * |
||
46 | * @return array |
||
47 | */ |
||
48 | private function getQuerySelect(array $fields, array $queryOptions): array |
||
49 | { |
||
50 | $select = []; |
||
51 | foreach ($fields as $name => $field) { |
||
52 | if (isset($field->privileges["select"])) { |
||
53 | $as = $this->driver->convertField($field); |
||
54 | if ($queryOptions["clone"] && $field->autoIncrement) { |
||
55 | $as = "''"; |
||
56 | } |
||
57 | if ($this->driver->jush() == "sql" && preg_match("~enum|set~", $field->type)) { |
||
58 | $as = "1*" . $this->driver->escapeId($name); |
||
59 | } |
||
60 | $select[] = ($as ? "$as AS " : "") . $this->driver->escapeId($name); |
||
61 | } |
||
62 | } |
||
63 | if (!$this->driver->support("table")) { |
||
64 | $select = ["*"]; |
||
65 | } |
||
66 | return $select; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param string $table |
||
71 | * @param string $where |
||
72 | * @param array $fields |
||
73 | * @param array $queryOptions |
||
74 | * |
||
75 | * @return array|null |
||
76 | */ |
||
77 | private function getQueryFirstRow(string $table, string $where, array $fields, array $queryOptions): ?array |
||
78 | { |
||
79 | // From edit.inc.php |
||
80 | $row = null; |
||
81 | if (($where)) { |
||
82 | $select = $this->getQuerySelect($fields, $queryOptions); |
||
83 | $row = []; |
||
84 | if ($select) { |
||
85 | $statement = $this->driver->select($table, $select, [$where], $select, [], |
||
86 | (isset($queryOptions["select"]) ? 2 : 1)); |
||
87 | if (($statement)) { |
||
88 | $row = $statement->fetchAssoc(); |
||
89 | }/* else { |
||
90 | $error = $this->driver->error(); |
||
91 | }*/ |
||
92 | // if(isset($queryOptions["select"]) && (!$row || $statement->fetchAssoc())) |
||
93 | // { |
||
94 | // // $statement->rowCount() != 1 isn't available in all drivers |
||
95 | // $row = null; |
||
96 | // } |
||
97 | } |
||
98 | } |
||
99 | return $row; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param TableFieldEntity $field |
||
104 | * @param string $name |
||
105 | * @param array|null $row |
||
106 | * @param string $update |
||
107 | * @param array $queryOptions |
||
108 | * |
||
109 | * @return mixed |
||
110 | */ |
||
111 | private function getRowFieldValue(TableFieldEntity $field, string $name, ?array $row, string $update, array $queryOptions) |
||
112 | { |
||
113 | // $default = $queryOptions["set"][$this->driver->bracketEscape($name)] ?? null; |
||
114 | // if($default === null) |
||
115 | // { |
||
116 | $default = $field->default; |
||
117 | if ($field->type == "bit" && preg_match("~^b'([01]*)'\$~", $default, $regs)) { |
||
118 | $default = $regs[1]; |
||
119 | } |
||
120 | // } |
||
121 | if ($row === null) { |
||
|
|||
122 | return !$update && $field->autoIncrement ? "" : (isset($queryOptions["select"]) ? false : $default); |
||
123 | } |
||
124 | if ($row[$name] != "" && $this->driver->jush() == "sql" && preg_match("~enum|set~", $field->type) ) { |
||
125 | return is_array($row[$name]) ? array_sum($row[$name]) : +$row[$name]; |
||
126 | } |
||
127 | return is_bool($row[$name]) ? +$row[$name] : $row[$name]; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @param TableFieldEntity $field |
||
132 | * @param string $name |
||
133 | * @param mixed $value |
||
134 | * @param string $update |
||
135 | * @param array $queryOptions |
||
136 | * |
||
137 | * @return string|null |
||
138 | */ |
||
139 | private function getRowFieldFunction(TableFieldEntity $field, string $name, $value, string $update, array $queryOptions): ?string |
||
157 | } |
||
158 | } |
||
159 |