| Conditions | 18 |
| Paths | 66 |
| Total Lines | 91 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 = []) |
||
| 226 |