| Total Complexity | 41 |
| Total Lines | 248 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Select 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 Select, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Select extends GenericSelect |
||
| 10 | { |
||
| 11 | protected const UNION = 'union'; |
||
| 12 | protected const INTERSECT = 'intersect'; |
||
| 13 | protected const EXCEPT = 'except'; |
||
| 14 | |||
| 15 | public const LOCK_FOR_UPDATE = 'FOR UPDATE'; |
||
| 16 | public const LOCK_FOR_NO_KEY_UPDATE = 'FOR NO KEY UPDATE'; |
||
| 17 | public const LOCK_FOR_SHARE = 'FOR SHARE'; |
||
| 18 | public const LOCK_FOR_KEY_SHARE = 'FOR KEY SHARE'; |
||
| 19 | public const LOCK_NOWAIT = 'NOWAIT'; |
||
| 20 | public const LOCK_SKIP_LOCKED = 'SKIP LOCKED'; |
||
| 21 | |||
| 22 | protected array $unionLikes = []; |
||
| 23 | |||
| 24 | /** @var string[] */ |
||
| 25 | protected array $locks = []; |
||
| 26 | |||
| 27 | protected ?string $lockTable = null; |
||
| 28 | |||
| 29 | protected ?int $outerOffset = null; |
||
| 30 | |||
| 31 | protected ?int $outerLimit = null; |
||
| 32 | |||
| 33 | /** @var array<string,string> */ |
||
| 34 | protected array $outerOrderByParts = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param int|null $offset |
||
| 38 | * |
||
| 39 | * @return $this |
||
| 40 | */ |
||
| 41 | public function setOuterOffset(?int $offset): static |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param int|null $limit |
||
| 50 | * |
||
| 51 | * @return $this |
||
| 52 | */ |
||
| 53 | public function setOuterLimit(?int $limit): static |
||
| 54 | { |
||
| 55 | $this->outerLimit = $limit; |
||
| 56 | |||
| 57 | return $this; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param string $column |
||
| 62 | * @param string $direction |
||
| 63 | * |
||
| 64 | * @return Select |
||
| 65 | */ |
||
| 66 | public function addOuterOrderBy(string $column, string $direction = 'ASC'): static |
||
| 67 | { |
||
| 68 | $this->outerOrderByParts[$column] = $direction; |
||
| 69 | |||
| 70 | return $this; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param string ...$locks |
||
| 75 | * |
||
| 76 | * @return $this |
||
| 77 | */ |
||
| 78 | public function addLock(string ...$locks): static |
||
| 79 | { |
||
| 80 | foreach ($locks as $lock) { |
||
| 81 | switch ($lock) { |
||
| 82 | case static::LOCK_FOR_SHARE: |
||
| 83 | case static::LOCK_FOR_UPDATE: |
||
| 84 | case static::LOCK_FOR_KEY_SHARE: |
||
| 85 | case static::LOCK_FOR_NO_KEY_UPDATE: |
||
| 86 | $this->locks[0] = $lock; |
||
| 87 | break; |
||
| 88 | case static::LOCK_NOWAIT: |
||
| 89 | case static::LOCK_SKIP_LOCKED: |
||
| 90 | $this->locks[1] = $lock; |
||
| 91 | break; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | ksort($this->locks); |
||
| 96 | |||
| 97 | return $this; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param string $lockTable |
||
| 102 | * |
||
| 103 | * @return $this |
||
| 104 | */ |
||
| 105 | public function addLockTable(string $lockTable): static |
||
| 106 | { |
||
| 107 | $this->lockTable = $lockTable; |
||
| 108 | |||
| 109 | return $this; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param Select $select |
||
| 114 | * @param string $modifier |
||
| 115 | * |
||
| 116 | * @return $this |
||
| 117 | */ |
||
| 118 | public function addUnion(Select $select, string $modifier = ''): static |
||
| 119 | { |
||
| 120 | return $this->addUnionLike(static::UNION, $select, $modifier); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param Select $select |
||
| 125 | * @param string $modifier |
||
| 126 | * |
||
| 127 | * @return $this |
||
| 128 | */ |
||
| 129 | public function addIntersect(Select $select, string $modifier = ''): static |
||
| 130 | { |
||
| 131 | return $this->addUnionLike(static::INTERSECT, $select, $modifier); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param Select $select |
||
| 136 | * @param string $modifier |
||
| 137 | * |
||
| 138 | * @return $this |
||
| 139 | */ |
||
| 140 | public function addExcept(Select $select, string $modifier = ''): static |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param string $type |
||
| 147 | * @param Select $select |
||
| 148 | * @param string $modifier |
||
| 149 | * |
||
| 150 | * @return $this |
||
| 151 | */ |
||
| 152 | protected function addUnionLike(string $type, Select $select, string $modifier = ''): static |
||
| 153 | { |
||
| 154 | if ($type !== static::INTERSECT && $type !== static::EXCEPT) { |
||
| 155 | $type = static::UNION; |
||
| 156 | } |
||
| 157 | |||
| 158 | if ($modifier !== static::ALL && $modifier !== static::DISTINCT) { |
||
| 159 | $modifier = ''; |
||
| 160 | } |
||
| 161 | |||
| 162 | $this->unionLikes[] = [$type, $select, $modifier]; |
||
| 163 | |||
| 164 | return $this; |
||
| 165 | } |
||
| 166 | |||
| 167 | public function __toString(): string |
||
| 168 | { |
||
| 169 | $parts = array_merge( |
||
| 170 | [parent::__toString()], |
||
| 171 | $this->getLocks(), |
||
| 172 | $this->getUnionLikes() |
||
| 173 | ); |
||
| 174 | |||
| 175 | $parts = array_filter($parts); |
||
| 176 | |||
| 177 | $sql = implode(PHP_EOL, $parts); |
||
| 178 | |||
| 179 | if ($this->outerLimit === null && $this->outerOffset === null && count($this->outerOrderByParts) === 0) { |
||
| 180 | return $sql; |
||
| 181 | } |
||
| 182 | |||
| 183 | $parts = array_merge( |
||
| 184 | ['(' . $sql . ')'], |
||
| 185 | $this->getOuterOrderBy(), |
||
| 186 | $this->getOuterLimit() |
||
| 187 | ); |
||
| 188 | |||
| 189 | $parts = array_filter($parts); |
||
| 190 | |||
| 191 | return implode(PHP_EOL, $parts); |
||
| 192 | } |
||
| 193 | |||
| 194 | protected function getLocks(): array |
||
| 204 | } |
||
| 205 | |||
| 206 | public function getUnionLikes(): array |
||
| 207 | { |
||
| 208 | $parts = []; |
||
| 209 | foreach ($this->unionLikes as $unionLike) { |
||
| 231 | } |
||
| 232 | |||
| 233 | protected function getOuterOrderBy(): array |
||
| 245 | } |
||
| 246 | |||
| 247 | protected function getOuterLimit(): array |
||
| 257 | } |
||
| 258 | } |
||
| 259 |