| Total Complexity | 40 |
| Total Lines | 208 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like HandlesAliases 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 HandlesAliases, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | trait HandlesAliases |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var array<string, Expression|string> |
||
| 19 | */ |
||
| 20 | public array $tableAliases = []; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var array<string, Expression|string> |
||
| 24 | */ |
||
| 25 | public array $columnAliases = []; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param array<mixed>|string $column |
||
| 29 | * @return array<mixed>|string |
||
| 30 | */ |
||
| 31 | public function convertColumnId(array|string|Expression $column): array|string|Expression |
||
| 32 | { |
||
| 33 | |||
| 34 | if ($column instanceof Expression) { |
||
|
|
|||
| 35 | return $column; |
||
| 36 | } |
||
| 37 | |||
| 38 | return $this->convertIdToKey($column); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Extract table and alias from sql alias notation (entity AS `alias`) |
||
| 43 | * |
||
| 44 | * @return array<int|string, Expression|string> |
||
| 45 | * |
||
| 46 | * @throws Exception |
||
| 47 | */ |
||
| 48 | public function extractAlias(string $entity, int|null|string $key = null): array |
||
| 67 | } |
||
| 68 | |||
| 69 | public function generateTableAlias(string|Expression $table, string $postfix = 'Doc'): string |
||
| 75 | } |
||
| 76 | |||
| 77 | public function getTableAlias(string|Expression $table): float|int|null|string |
||
| 92 | } |
||
| 93 | |||
| 94 | public function getColumnAlias(string $column): Expression|null|string |
||
| 95 | { |
||
| 96 | if (isset($this->columnAliases[$column])) { |
||
| 97 | return $this->columnAliases[$column]; |
||
| 98 | } |
||
| 99 | |||
| 100 | return null; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @return array<string, Expression|string> |
||
| 105 | */ |
||
| 106 | public function getTableAliases(): array |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param array<string, Expression|string>|IlluminateQueryBuilder $aliases |
||
| 113 | * @return void |
||
| 114 | */ |
||
| 115 | public function importTableAliases(array|IlluminateQueryBuilder $aliases): void |
||
| 116 | { |
||
| 117 | if ($aliases instanceof IlluminateQueryBuilder) { |
||
| 118 | assert($aliases instanceof Builder); |
||
| 119 | $aliases = $aliases->getTableAliases(); |
||
| 120 | } |
||
| 121 | |||
| 122 | $this->tableAliases = array_merge($this->tableAliases, $aliases); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param IlluminateEloquentBuilder|IlluminateQueryBuilder|Relation $query |
||
| 127 | * @return void |
||
| 128 | */ |
||
| 129 | public function exchangeTableAliases($query): void |
||
| 135 | } |
||
| 136 | |||
| 137 | public function isTableAlias(string $value): bool |
||
| 138 | { |
||
| 139 | return in_array($value, $this->tableAliases); |
||
| 140 | } |
||
| 141 | |||
| 142 | public function isTable(string $value): bool |
||
| 143 | { |
||
| 144 | return array_key_exists($value, $this->tableAliases); |
||
| 145 | } |
||
| 146 | |||
| 147 | public function prefixAlias(string $target, string $value): string |
||
| 148 | { |
||
| 149 | /** @phpstan-ignore-next-line */ |
||
| 150 | $alias = $this->grammar->getValue($this->getTableAlias($target)); |
||
| 151 | |||
| 152 | if (Str::startsWith($value, $alias . '.')) { |
||
| 153 | return $value; |
||
| 154 | } |
||
| 155 | |||
| 156 | return $alias . '.' . $value; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @throws Exception |
||
| 161 | */ |
||
| 162 | public function registerColumnAlias(string $column, ?string $alias = null): bool |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @SuppressWarnings("PHPMD.CyclomaticComplexity") |
||
| 179 | */ |
||
| 180 | public function registerTableAlias(string|Expression $table, ?string $alias = null): string |
||
| 209 | } |
||
| 210 | |||
| 211 | public function replaceTableForAlias(string $reference): string |
||
| 223 | } |
||
| 224 | } |
||
| 225 |