| Total Complexity | 42 | 
| Total Lines | 234 | 
| Duplicated Lines | 0 % | 
| Changes | 3 | ||
| 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  | 
            ||
| 9 | class Grammar extends AbstractGrammar  | 
            ||
| 10 | { | 
            ||
| 11 | /**  | 
            ||
| 12 | * @inheritDoc  | 
            ||
| 13 | */  | 
            ||
| 14 | public function escapeId($idf)  | 
            ||
| 17 | }  | 
            ||
| 18 | |||
| 19 | private function constraints(string $table)  | 
            ||
| 34 | }  | 
            ||
| 35 | |||
| 36 | /**  | 
            ||
| 37 | * @inheritDoc  | 
            ||
| 38 | */  | 
            ||
| 39 | public function sqlForForeignKeys(string $table)  | 
            ||
| 40 |     { | 
            ||
| 41 | $query = "";  | 
            ||
| 42 | |||
| 43 | $status = $this->driver->tableStatus($table);  | 
            ||
| 44 | $fkeys = $this->driver->foreignKeys($table);  | 
            ||
| 45 | ksort($fkeys);  | 
            ||
| 46 | |||
| 47 |         foreach ($fkeys as $fkey_name => $fkey) { | 
            ||
| 48 | $query .= "ALTER TABLE ONLY " . $this->escapeId($status->schema) . "." .  | 
            ||
| 49 | $this->escapeId($status->name) . " ADD CONSTRAINT " . $this->escapeId($fkey_name) .  | 
            ||
| 50 |                 " {$fkey->definition} " . ($fkey->deferrable ? 'DEFERRABLE' : 'NOT DEFERRABLE') . ";\n"; | 
            ||
| 51 | }  | 
            ||
| 52 | |||
| 53 | return ($query ? "$query\n" : $query);  | 
            ||
| 54 | }  | 
            ||
| 55 | |||
| 56 | /**  | 
            ||
| 57 | * @param array $fields  | 
            ||
| 58 | * @param boolean $autoIncrement  | 
            ||
| 59 | * @param string $style  | 
            ||
| 60 | *  | 
            ||
| 61 | * @return array  | 
            ||
| 62 | */  | 
            ||
| 63 | private function _sequences(array $fields, bool $autoIncrement, string $style)  | 
            ||
| 81 | }  | 
            ||
| 82 | |||
| 83 | /**  | 
            ||
| 84 | * @param string $table  | 
            ||
| 85 | * @param array $fields  | 
            ||
| 86 | * @param array $indexes  | 
            ||
| 87 | *  | 
            ||
| 88 | * @return array  | 
            ||
| 89 | */  | 
            ||
| 90 | private function _clauses(string $table, array $fields, array $indexes)  | 
            ||
| 91 |     { | 
            ||
| 92 | $clauses = [];  | 
            ||
| 93 | // Fields definitions  | 
            ||
| 94 |         foreach ($fields as $field_name => $field) { | 
            ||
| 95 | $clauses[] = $this->escapeId($field->name) . ' ' . $field->fullType .  | 
            ||
| 96 | $this->driver->defaultValue($field) . ($field->attnotnull ? " NOT NULL" : "");  | 
            ||
| 97 | }  | 
            ||
| 98 | // Primary + unique keys  | 
            ||
| 99 |         foreach ($indexes as $index_name => $index) { | 
            ||
| 100 |             switch ($index->type) { | 
            ||
| 101 | case 'UNIQUE':  | 
            ||
| 102 | $clauses[] = "CONSTRAINT " . $this->escapeId($index_name) .  | 
            ||
| 103 |                         " UNIQUE (" . implode(', ', array_map(function ($column) { | 
            ||
| 104 | return $this->escapeId($column);  | 
            ||
| 105 | }, $index->columns)) . ")";  | 
            ||
| 106 | break;  | 
            ||
| 107 | case 'PRIMARY':  | 
            ||
| 108 | $clauses[] = "CONSTRAINT " . $this->escapeId($index_name) .  | 
            ||
| 109 |                         " PRIMARY KEY (" . implode(', ', array_map(function ($column) { | 
            ||
| 110 | return $this->escapeId($column);  | 
            ||
| 111 | }, $index->columns)) . ")";  | 
            ||
| 112 | break;  | 
            ||
| 113 | }  | 
            ||
| 114 | }  | 
            ||
| 115 | // Constraints  | 
            ||
| 116 | $constraints = $this->constraints($table);  | 
            ||
| 117 |         foreach ($constraints as $conname => $consrc) { | 
            ||
| 118 | $clauses[] = "CONSTRAINT " . $this->escapeId($conname) . " CHECK $consrc";  | 
            ||
| 119 | }  | 
            ||
| 120 | |||
| 121 | return $clauses;  | 
            ||
| 122 | }  | 
            ||
| 123 | |||
| 124 | /**  | 
            ||
| 125 | * @param array $indexes  | 
            ||
| 126 | * @param TableEntity $status  | 
            ||
| 127 | *  | 
            ||
| 128 | * @return string  | 
            ||
| 129 | */  | 
            ||
| 130 | private function _indexQueries(array $indexes, TableEntity $status)  | 
            ||
| 131 |     { | 
            ||
| 132 | $query = '';  | 
            ||
| 133 | // Indexes after table definition  | 
            ||
| 134 |         foreach ($indexes as $index_name => $index) { | 
            ||
| 135 |             if ($index->type == 'INDEX') { | 
            ||
| 136 | $columns = [];  | 
            ||
| 137 |                 foreach ($index->columns as $key => $val) { | 
            ||
| 138 | $columns[] = $this->escapeId($val) . ($index->descs[$key] ? " DESC" : "");  | 
            ||
| 139 | }  | 
            ||
| 140 | $query .= "\n\nCREATE INDEX " . $this->escapeId($index_name) . " ON " .  | 
            ||
| 141 | $this->escapeId($status->schema) . "." . $this->escapeId($status->name) .  | 
            ||
| 142 |                     " USING btree (" . implode(', ', $columns) . ");"; | 
            ||
| 143 | }  | 
            ||
| 144 | }  | 
            ||
| 145 | return $query;  | 
            ||
| 146 | }  | 
            ||
| 147 | |||
| 148 | /**  | 
            ||
| 149 | * @param array $fields  | 
            ||
| 150 | * @param TableEntity $status  | 
            ||
| 151 | *  | 
            ||
| 152 | * @return string  | 
            ||
| 153 | */  | 
            ||
| 154 | private function _commentQueries(array $fields, TableEntity $status)  | 
            ||
| 169 | }  | 
            ||
| 170 | |||
| 171 | /**  | 
            ||
| 172 | * @inheritDoc  | 
            ||
| 173 | */  | 
            ||
| 174 | public function sqlForCreateTable(string $table, bool $autoIncrement, string $style)  | 
            ||
| 175 |     { | 
            ||
| 176 | $status = $this->driver->tableStatus($table);  | 
            ||
| 177 |         if ($status !== null && $this->driver->isView($status)) { | 
            ||
| 178 | $view = $this->driver->view($table);  | 
            ||
| 179 |             return rtrim("CREATE VIEW " . $this->escapeId($table) . " AS $view[select]", ";"); | 
            ||
| 180 | }  | 
            ||
| 181 | |||
| 182 | $fields = $this->driver->fields($table);  | 
            ||
| 183 |         if (empty($status) || empty($fields)) { | 
            ||
| 184 | return '';  | 
            ||
| 185 | }  | 
            ||
| 186 | |||
| 187 | $sequences = $this->_sequences($fields, $autoIncrement, $style);  | 
            ||
| 188 | $indexes = $this->driver->indexes($table);  | 
            ||
| 189 | ksort($indexes);  | 
            ||
| 190 | $clauses = $this->_clauses($table, $fields, $indexes);  | 
            ||
| 191 | // Adding sequences before table definition  | 
            ||
| 192 | $query = '';  | 
            ||
| 193 |         if (!empty($sequences)) { | 
            ||
| 194 |             $query = implode("\n\n", $sequences) . "\n\n"; | 
            ||
| 195 | }  | 
            ||
| 196 | $query .= 'CREATE TABLE ' . $this->escapeId($status->schema) . '.' . $this->escapeId($status->name) . " (\n ";  | 
            ||
| 197 |         $query .= implode(",\n    ", $clauses) . "\n) WITH (oids = " . ($status->oid ? 'true' : 'false') . ");"; | 
            ||
| 198 | $query .= $this->_indexQueries($indexes, $status);  | 
            ||
| 199 | $query .= $this->_commentQueries($fields, $status);  | 
            ||
| 200 | |||
| 201 | return rtrim($query, ';');  | 
            ||
| 202 | }  | 
            ||
| 203 | |||
| 204 | /**  | 
            ||
| 205 | * @inheritDoc  | 
            ||
| 206 | */  | 
            ||
| 207 | public function sqlForTruncateTable(string $table)  | 
            ||
| 208 |     { | 
            ||
| 209 | return "TRUNCATE " . $this->table($table);  | 
            ||
| 210 | }  | 
            ||
| 211 | |||
| 212 | /**  | 
            ||
| 213 | * @inheritDoc  | 
            ||
| 214 | */  | 
            ||
| 215 | public function sqlForCreateTrigger(string $table)  | 
            ||
| 216 |     { | 
            ||
| 217 | $status = $this->driver->tableStatus($table);  | 
            ||
| 218 | $query = "";  | 
            ||
| 219 |         foreach ($this->driver->triggers($table) as $trg_id => $trg) { | 
            ||
| 220 | $trigger = $this->driver->trigger($trg_id, $status->name);  | 
            ||
| 221 | $query .= "\nCREATE TRIGGER " . $this->escapeId($trigger['Trigger']) .  | 
            ||
| 222 | " $trigger[Timing] $trigger[Events] ON " . $this->escapeId($status->schema) . "." .  | 
            ||
| 223 | $this->escapeId($status->name) . " $trigger[Type] $trigger[Statement];;\n";  | 
            ||
| 224 | }  | 
            ||
| 225 | return $query;  | 
            ||
| 226 | }  | 
            ||
| 227 | |||
| 228 | |||
| 229 | /**  | 
            ||
| 230 | * @inheritDoc  | 
            ||
| 231 | */  | 
            ||
| 232 | public function sqlForUseDatabase(string $database)  | 
            ||
| 235 | }  | 
            ||
| 236 | |||
| 237 | /**  | 
            ||
| 238 | * @inheritDoc  | 
            ||
| 239 | */  | 
            ||
| 240 | protected function queryRegex()  | 
            ||
| 245 |