| Total Complexity | 45 |
| Total Lines | 217 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ClauseJoin 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 ClauseJoin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | trait ClauseJoin |
||
| 10 | { |
||
| 11 | protected $joined_tables = []; |
||
| 12 | |||
| 13 | abstract public function table(TableManipulationInterface $setter = null): TableManipulationInterface; |
||
| 14 | abstract public function tableName(); |
||
| 15 | abstract public function tableAlias($setter = null); |
||
| 16 | abstract public function tableLabel($table_name = null); |
||
| 17 | abstract public function selectAlso($setter); |
||
| 18 | abstract public function backTick($field, $table_name = null); |
||
| 19 | abstract public function addBinding($field, $value, $table_name = null, $bind_label = null): string; |
||
| 20 | abstract public function joinRaw($sql); |
||
| 21 | |||
| 22 | public function addTables($setter) |
||
| 23 | { |
||
| 24 | $this->joined_tables = array_merge($this->joined_tables, is_array($setter) ? $setter : [$setter]); |
||
| 25 | return $this; |
||
| 26 | } |
||
| 27 | |||
| 28 | public function eager($table_aliases = []) |
||
| 29 | { |
||
| 30 | if (isset($table_aliases[$this->tableName()])) { |
||
| 31 | $this->tableAlias($table_aliases[$this->tableName()]); |
||
| 32 | } |
||
| 33 | |||
| 34 | foreach ($this->table()->foreignKeysByTable() as $foreign_table_name => $fk_columns) { |
||
| 35 | $foreign_table = Crudites::inspect($foreign_table_name); |
||
| 36 | |||
| 37 | $single_fk = count($fk_columns) === 1; //assumption |
||
| 38 | foreach ($fk_columns as $fk_column) { |
||
| 39 | $select_also = []; |
||
| 40 | |||
| 41 | // TODO this sucks hard.. 'created_by' & 'kadro_operator' have *NOTHING* to do in SelectJoin, must create mecanism for such exception |
||
| 42 | if ($fk_column->foreignTableName() == 'kadro_operator' && $fk_column->name() == 'created_by') { |
||
| 43 | continue; // dont load the log information |
||
| 44 | } else { |
||
| 45 | $m = []; |
||
| 46 | if (preg_match('/(.+)_(' . $fk_column->foreignColumnName() . ')$/', $fk_column->name(), $m)) { |
||
| 47 | $foreign_table_alias = $m[1]; |
||
| 48 | } else { |
||
| 49 | $foreign_table_alias = $foreign_table_name; |
||
| 50 | } |
||
| 51 | $foreign_table_alias = $single_fk === true ? $foreign_table_alias : $foreign_table_alias . '_' . $fk_column->name(); |
||
| 52 | |||
| 53 | // auto select non nullable columns |
||
| 54 | } |
||
| 55 | |||
| 56 | if (empty($select_also)) { |
||
| 57 | foreach ($foreign_table->columns() as $col) { |
||
| 58 | // if (!$col->isHidden()) { |
||
| 59 | $select_also [] = "$col"; |
||
| 60 | // } |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | $this->autoJoin([$foreign_table, $foreign_table_alias], $select_also); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | |||
| 70 | public function join($table_names, $joins, $join_type = '') |
||
| 71 | { |
||
| 72 | list($join_table_name,$join_table_alias) = self::processParamTableNames($table_names); |
||
| 73 | |||
| 74 | if (preg_match('/^(INNER|LEFT|RIGHT|FULL)(\sOUTER)?/i', $join_type) !== 1) { |
||
| 75 | $join_type = ''; |
||
| 76 | } |
||
| 77 | |||
| 78 | $this->joinRaw($this->generateJoin($join_type, $join_table_name, $join_table_alias, $joins)); |
||
| 79 | |||
| 80 | return $this; |
||
| 81 | } |
||
| 82 | |||
| 83 | public function autoJoin($other_table, $select_also = [], $relation_type = null) |
||
| 84 | { |
||
| 85 | $other_table_alias = null; |
||
| 86 | |||
| 87 | if (is_array($other_table)) { |
||
| 88 | list($other_table, $other_table_alias) = $other_table; |
||
| 89 | } else { |
||
| 90 | $other_table_alias = $other_table->name(); |
||
| 91 | } |
||
| 92 | |||
| 93 | $other_table_name = $other_table->name(); |
||
| 94 | |||
| 95 | $joins = []; |
||
| 96 | |||
| 97 | // 1. ? this->table.other_table_id -> $other_table.id |
||
| 98 | // 2. ? this_table.id -> $other_table.this_table_id) |
||
| 99 | // if(count($bonding_column = $this->table()->foreignKeysByTable()[$other_table_name] ?? []) === 1) |
||
| 100 | if (!is_null($bonding_column = $this->table()->singleForeignKeyTo($other_table))) { |
||
| 101 | $relation_type = $relation_type ?? $bonding_column->isNullable() ? 'LEFT OUTER' : 'INNER'; |
||
| 102 | // $joins []= [$bonding_column->tableName(), $bonding_column->name(), $other_table_alias ?? $bonding_column->foreignTableAlias(), $bonding_column->foreignColumnName()]; |
||
| 103 | $joins [] = [$this->tableAlias(), $bonding_column->name(), $other_table_alias ?? $bonding_column->foreignTableAlias(), $bonding_column->foreignColumnName()]; |
||
| 104 | } elseif (!is_null($bonding_column = $other_table->singleForeignKeyTo($this->table()))) { |
||
| 105 | // vd(__FUNCTION__.' : '.$other_table.' has fk to '.$this->table()); |
||
| 106 | $relation_type = $relation_type ?? 'LEFT OUTER'; |
||
| 107 | $joins [] = [$this->tableLabel(), $bonding_column->foreignColumnName(), $other_table_alias ?? $other_table->name(), $bonding_column->name()]; |
||
| 108 | } else { |
||
| 109 | $bondable_tables = $this->joinableTables(); |
||
| 110 | if (isset($bondable_tables[$other_table_name])) { |
||
| 111 | $bonding_columns = $bondable_tables[$other_table_name]; |
||
| 112 | if (count($bonding_columns) === 1) { |
||
| 113 | $bonding_column = current($bonding_columns); |
||
| 114 | $other_table_alias = $other_table_alias ?? $bonding_column->foreignTableAlias(); |
||
| 115 | |||
| 116 | $bonding_table_label = array_search($bonding_column->tableName(), $this->joinedTables()); |
||
| 117 | if ($bonding_table_label === false) { |
||
| 118 | $bonding_table_label = $bonding_column->tableName(); |
||
| 119 | } |
||
| 120 | |||
| 121 | $joins = [[$bonding_table_label, $bonding_column->name(), $other_table_alias, $bonding_column->foreignColumnName()]]; |
||
| 122 | $relation_type = $relation_type ?? (($bonding_column->isNullable()) ? 'LEFT OUTER' : 'INNER'); |
||
| 123 | } |
||
| 124 | } elseif (count($intersections = array_intersect_key($other_table->foreignKeysByTable(), $bondable_tables)) > 0) { |
||
| 125 | $other_table_alias = $other_table_alias ?? $other_table->name(); |
||
| 126 | foreach ($intersections as $table_name => $bonding_column) { |
||
| 127 | if (count($bonding_column) !== 1 || count($other_table->foreignKeysByTable()[$table_name]) !== 1) { |
||
| 128 | break; |
||
| 129 | } |
||
| 130 | // vd($this->tableName() . " $other_table_name"); |
||
| 131 | // vd($bonding_column); |
||
| 132 | |||
| 133 | $joins = []; |
||
| 134 | |||
| 135 | $bonding_column = current($bonding_column); |
||
| 136 | $joins [] = [$other_table_alias, $bonding_column->name(), $bonding_column->foreignTableAlias(), $bonding_column->foreignColumnName()]; |
||
| 137 | |||
| 138 | // vd($other_table_alias); |
||
| 139 | $bonding_column = current($bondable_tables[$table_name]); |
||
| 140 | $joins [] = [$bonding_column->tableName(), $bonding_column->name(), $bonding_column->foreignTableAlias(), $bonding_column->foreignColumnName()]; |
||
| 141 | |||
| 142 | // $relation_type = $relation_type ?? (($parent_column->isNullable() || $bonding_column->isNullable()) ? 'LEFT OUTER' : 'INNER'); |
||
| 143 | $relation_type = $relation_type ?? (($bonding_column->isNullable()) ? 'LEFT OUTER' : 'INNER'); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | // vd($relation_type.' '.json_encode($joins)); |
||
| 149 | if (!empty($joins)) { |
||
| 150 | // vd('ottojoin: '.$this->table()->name().' with '.$other_table_name.' as '.$other_table_alias); |
||
| 151 | $this->join([$other_table_name, $other_table_alias], $joins, $relation_type); |
||
| 152 | $this->addTables([$other_table_alias => $other_table_name]); |
||
| 153 | |||
| 154 | |||
| 155 | // if(is_null($select_also) empty($select_also)) |
||
| 156 | // $select_also=[$other_table_alias.'.*']; |
||
| 157 | if (!empty($select_also)) { |
||
| 158 | foreach ($select_also as $select_field) { |
||
| 159 | if (is_null($other_table->column("$select_field"))) { |
||
| 160 | $computed_selection = "$select_field"; // table column does not exist, no nood to prefix |
||
| 161 | } else { |
||
| 162 | $computed_selection = "$other_table_alias.$select_field as " . $other_table_alias . "_$select_field"; |
||
| 163 | } |
||
| 164 | |||
| 165 | // vd($computed_selection); |
||
| 166 | $this->selectAlso($computed_selection); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | return $other_table_alias; |
||
| 172 | } |
||
| 173 | |||
| 174 | protected function generateJoin($join_type, $join_table_name, $join_table_alias = null, $join_fields = []) |
||
| 175 | { |
||
| 176 | $join_table_alias = $join_table_alias ?? $join_table_name; |
||
| 177 | |||
| 178 | $join_parts = []; |
||
| 179 | foreach ($join_fields as $join_cond) { |
||
| 180 | if (isset($join_cond[3])) { // 4 joins param -> t.f = t.f |
||
| 181 | list($table, $field, $join_table, $join_table_field) = $join_cond; |
||
| 182 | $join_parts [] = $this->backTick($field, $table) . ' = ' . $this->backTick($join_table_field, $join_table); |
||
| 183 | } elseif (isset($join_cond[2])) { // 3 joins param -> t.f = v |
||
| 184 | list($table, $field, $value) = $join_cond; |
||
| 185 | $bind_label = ':loj_' . $join_table_alias . '_' . $table . '_' . $field; |
||
| 186 | $this->addBinding($field, $value, null, $bind_label); |
||
| 187 | |||
| 188 | $join_parts [] = $this->backTick($field, $table) . ' = ' . $bind_label; |
||
| 189 | } |
||
| 190 | } |
||
| 191 | return sprintf('%s JOIN `%s` %s ON %s', $join_type, $join_table_name, $join_table_alias, implode(' AND ', $join_parts)); |
||
| 192 | } |
||
| 193 | |||
| 194 | private function joinedTables() |
||
| 195 | { |
||
| 196 | return $this->joined_tables; |
||
| 197 | } |
||
| 198 | |||
| 199 | private function joinableTables(): array |
||
| 200 | { |
||
| 201 | $joinable_tables = $this->table()->foreignKeysByTable(); |
||
| 202 | foreach ($this->joinedTables() as $join_table) { |
||
| 203 | $joinable_tables += Crudites::inspect($join_table)->foreignKeysByTable(); |
||
| 204 | } |
||
| 205 | |||
| 206 | return $joinable_tables; |
||
| 207 | } |
||
| 208 | |||
| 209 | private static function processParamTableNames($table_names): array |
||
| 210 | { |
||
| 211 | if (is_array($table_names) && isset($table_names[1])) { |
||
| 212 | return $table_names; |
||
| 213 | } |
||
| 214 | |||
| 215 | if (is_array($table_names) && !isset($table_names[1])) { |
||
| 216 | $table_names = current($table_names); |
||
| 217 | } |
||
| 218 | |||
| 219 | if (is_string($table_names)) { |
||
| 220 | return [$table_names, $table_names]; |
||
| 221 | } |
||
| 222 | |||
| 223 | throw new CruditesException('INVALID_PARAM_TABLE_NAMES'); |
||
| 224 | } |
||
| 225 | } |
||
|
|
|||
| 226 |