Total Complexity | 49 |
Total Lines | 258 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Grammar 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 Grammar, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | abstract class Grammar implements GrammarInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var DriverInterface |
||
18 | */ |
||
19 | protected $driver; |
||
20 | |||
21 | /** |
||
22 | * @var UtilInterface |
||
23 | */ |
||
24 | protected $util; |
||
25 | |||
26 | /** |
||
27 | * @var TranslatorInterface |
||
28 | */ |
||
29 | protected $trans; |
||
30 | |||
31 | /** |
||
32 | * @var ConnectionInterface |
||
33 | */ |
||
34 | protected $connection; |
||
35 | |||
36 | /** |
||
37 | * The constructor |
||
38 | * |
||
39 | * @param DriverInterface $driver |
||
40 | * @param UtilInterface $util |
||
41 | * @param TranslatorInterface $trans |
||
42 | * @param ConnectionInterface $connection |
||
43 | */ |
||
44 | public function __construct(DriverInterface $driver, UtilInterface $util, TranslatorInterface $trans, ConnectionInterface $connection) |
||
45 | { |
||
46 | $this->driver = $driver; |
||
47 | $this->util = $util; |
||
48 | $this->trans = $trans; |
||
49 | $this->connection = $connection; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @inheritDoc |
||
54 | */ |
||
55 | public function escapeId(string $idf) |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @inheritDoc |
||
62 | */ |
||
63 | public function unescapeId(string $idf) |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @inheritDoc |
||
71 | */ |
||
72 | public function table(string $idf) |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @inheritDoc |
||
79 | */ |
||
80 | public function formatForeignKey(ForeignKeyEntity $foreignKey) |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @inheritDoc |
||
112 | */ |
||
113 | public function quote($string) |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @inheritDoc |
||
120 | */ |
||
121 | public function buildSelectQuery(TableSelectEntity $select) |
||
122 | { |
||
123 | $isGroup = (count($select->group) < count($select->fields)); |
||
124 | $query = ''; |
||
125 | if ($this->driver->jush() === 'sql' && ($select->page) && |
||
126 | ($select->limit) && !empty($select->group) && $isGroup) { |
||
127 | $query = 'SQL_CALC_FOUND_ROWS '; |
||
128 | } |
||
129 | $query .= \implode(', ', $select->fields) . "\nFROM " . $this->table($select->table); |
||
130 | $limit = +$select->limit; |
||
131 | $offset = $select->page ? $limit * $select->page : 0; |
||
132 | |||
133 | return 'SELECT' . $this->limit($query, $select->clauses, $limit, $offset, "\n"); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @inheritDoc |
||
138 | */ |
||
139 | public function applySqlFunction(string $function, string $column) |
||
140 | { |
||
141 | return ($function ? ($function == "unixepoch" ? "DATETIME($column, '$function')" : |
||
142 | ($function == "count distinct" ? "COUNT(DISTINCT " : strtoupper("$function(")) . "$column)") : $column); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @inheritDoc |
||
147 | */ |
||
148 | public function defaultValue($field) |
||
149 | { |
||
150 | $default = $field->default; |
||
151 | return ($default === null ? "" : " DEFAULT " . |
||
152 | (preg_match('~char|binary|text|enum|set~', $field->type) || |
||
153 | preg_match('~^(?![a-z])~i', $default) ? $this->quote($default) : $default)); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @inheritDoc |
||
158 | */ |
||
159 | public function limit(string $query, string $where, int $limit, int $offset = 0, string $separator = " ") |
||
160 | { |
||
161 | return ""; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @inheritDoc |
||
166 | */ |
||
167 | public function limitToOne(string $table, string $query, string $where, string $separator = "\n") |
||
168 | { |
||
169 | return $this->limit($query, $where, 1, 0, $separator); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @inheritDoc |
||
174 | */ |
||
175 | public function convertField(TableFieldEntity $field) |
||
176 | { |
||
177 | return ''; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @inheritDoc |
||
182 | */ |
||
183 | public function unconvertField(TableFieldEntity $field, string $value) |
||
184 | { |
||
185 | return $value; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @inheritDoc |
||
190 | */ |
||
191 | public function convertFields(array $columns, array $fields, array $select = []) |
||
192 | { |
||
193 | $clause = ""; |
||
194 | foreach ($columns as $key => $val) { |
||
195 | if (!empty($select) && !in_array($this->escapeId($key), $select)) { |
||
196 | continue; |
||
197 | } |
||
198 | $as = $this->convertField($fields[$key]); |
||
199 | if ($as) { |
||
200 | $clause .= ", $as AS " . $this->escapeId($key); |
||
201 | } |
||
202 | } |
||
203 | return $clause; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @inheritDoc |
||
208 | */ |
||
209 | public function countRowsSql(string $table, array $where, bool $isGroup, array $groups) |
||
210 | { |
||
211 | $query = " FROM " . $this->table($table) . ($where ? " WHERE " . implode(" AND ", $where) : ""); |
||
212 | return ($isGroup && ($this->driver->jush() == "sql" || count($groups) == 1) |
||
213 | ? "SELECT COUNT(DISTINCT " . implode(", ", $groups) . ")$query" |
||
214 | : "SELECT COUNT(*)" . ($isGroup ? " FROM (SELECT 1$query GROUP BY " . implode(", ", $groups) . ") x" : $query) |
||
215 | ); |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @inheritDoc |
||
220 | */ |
||
221 | public function sqlForCreateTable(string $table, bool $autoIncrement, string $style) |
||
222 | { |
||
223 | return ''; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @inheritDoc |
||
228 | */ |
||
229 | public function sqlForCreateIndex(string $table, string $type, string $name, string $columns) |
||
230 | { |
||
231 | return ''; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @inheritDoc |
||
236 | */ |
||
237 | public function sqlForUseDatabase(string $database) |
||
238 | { |
||
239 | return ""; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * @inheritDoc |
||
244 | */ |
||
245 | public function sqlForForeignKeys(string $table) |
||
246 | { |
||
247 | return ''; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * @inheritDoc |
||
252 | */ |
||
253 | public function sqlForTruncateTable(string $table) |
||
254 | { |
||
255 | return ''; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * @inheritDoc |
||
260 | */ |
||
261 | public function sqlForCreateTrigger(string $table) |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * @inheritDoc |
||
268 | */ |
||
269 | public function autoIncrement() |
||
270 | { |
||
271 | return ""; |
||
272 | } |
||
273 | } |
||
274 |