| Total Complexity | 49 |
| Total Lines | 224 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 2 | Features | 0 |
Complex classes like Quoter 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 Quoter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class Quoter implements QuoterInterface |
||
| 37 | { |
||
| 38 | public function __construct( |
||
| 45 | } |
||
| 46 | |||
| 47 | public function cleanUpTableNames(array $tableNames): array |
||
| 84 | } |
||
| 85 | |||
| 86 | public function getRawTableName(string $name): string |
||
| 95 | } |
||
| 96 | |||
| 97 | public function getTableNameParts(string $name, bool $withColumn = false): array |
||
| 98 | { |
||
| 99 | $parts = array_slice(explode('.', $name), -2, 2); |
||
| 100 | |||
| 101 | return $this->unquoteParts($parts, $withColumn); |
||
| 102 | } |
||
| 103 | |||
| 104 | public function ensureNameQuoted(string $name): string |
||
| 105 | { |
||
| 106 | $name = str_replace(["'", '"', '`', '[', ']'], '', $name); |
||
| 107 | |||
| 108 | if ($name && !preg_match('/^{{.*}}$/', $name)) { |
||
| 109 | return '{{' . $name . '}}'; |
||
| 110 | } |
||
| 111 | |||
| 112 | return $name; |
||
| 113 | } |
||
| 114 | |||
| 115 | public function ensureColumnName(string $name): string |
||
| 123 | } |
||
| 124 | |||
| 125 | public function quoteColumnName(string $name): string |
||
| 126 | { |
||
| 127 | if (str_contains($name, '(') || str_contains($name, '[[')) { |
||
| 128 | return $name; |
||
| 129 | } |
||
| 130 | |||
| 131 | if (($pos = strrpos($name, '.')) !== false) { |
||
| 132 | $prefix = $this->quoteTableName(substr($name, 0, $pos)) . '.'; |
||
| 133 | $name = substr($name, $pos + 1); |
||
| 134 | } else { |
||
| 135 | $prefix = ''; |
||
| 136 | } |
||
| 137 | |||
| 138 | if (str_contains($name, '{{')) { |
||
| 139 | return $name; |
||
| 140 | } |
||
| 141 | |||
| 142 | return $prefix . $this->quoteSimpleColumnName($name); |
||
| 143 | } |
||
| 144 | |||
| 145 | public function quoteSimpleColumnName(string $name): string |
||
| 146 | { |
||
| 147 | if (is_string($this->columnQuoteCharacter)) { |
||
| 148 | $startingCharacter = $endingCharacter = $this->columnQuoteCharacter; |
||
| 149 | } else { |
||
| 150 | [$startingCharacter, $endingCharacter] = $this->columnQuoteCharacter; |
||
| 151 | } |
||
| 152 | |||
| 153 | return $name === '*' || str_starts_with($name, $startingCharacter) |
||
| 154 | ? $name |
||
| 155 | : $startingCharacter . $name . $endingCharacter; |
||
| 156 | } |
||
| 157 | |||
| 158 | public function quoteSimpleTableName(string $name): string |
||
| 159 | { |
||
| 160 | if (is_string($this->tableQuoteCharacter)) { |
||
| 161 | $startingCharacter = $endingCharacter = $this->tableQuoteCharacter; |
||
| 162 | } else { |
||
| 163 | [$startingCharacter, $endingCharacter] = $this->tableQuoteCharacter; |
||
| 164 | } |
||
| 165 | |||
| 166 | return str_starts_with($name, $startingCharacter) |
||
| 167 | ? $name |
||
| 168 | : $startingCharacter . $name . $endingCharacter; |
||
| 169 | } |
||
| 170 | |||
| 171 | public function quoteSql(string $sql): string |
||
| 172 | { |
||
| 173 | return preg_replace_callback( |
||
| 174 | '/({{(%?[\w\-. ]+)%?}}|\\[\\[([\w\-. ]+)]])/', |
||
| 175 | function ($matches) { |
||
| 176 | if (isset($matches[3])) { |
||
| 177 | return $this->quoteColumnName($matches[3]); |
||
| 178 | } |
||
| 179 | |||
| 180 | return str_replace('%', $this->tablePrefix, $this->quoteTableName($matches[2])); |
||
| 181 | }, |
||
| 182 | $sql |
||
| 183 | ); |
||
| 184 | } |
||
| 185 | |||
| 186 | public function quoteTableName(string $name): string |
||
| 207 | } |
||
| 208 | |||
| 209 | public function quoteValue(mixed $value): mixed |
||
| 210 | { |
||
| 211 | if (!is_string($value)) { |
||
| 212 | return $value; |
||
| 213 | } |
||
| 214 | |||
| 215 | return "'" . str_replace("'", "''", addcslashes($value, "\000\032")) . "'"; |
||
| 216 | } |
||
| 217 | |||
| 218 | public function unquoteSimpleColumnName(string $name): string |
||
| 219 | { |
||
| 220 | if (is_string($this->columnQuoteCharacter)) { |
||
| 221 | $startingCharacter = $this->columnQuoteCharacter; |
||
| 222 | } else { |
||
| 223 | $startingCharacter = $this->columnQuoteCharacter[0]; |
||
| 224 | } |
||
| 225 | |||
| 226 | return !str_starts_with($name, $startingCharacter) |
||
| 227 | ? $name |
||
| 228 | : substr($name, 1, -1); |
||
| 229 | } |
||
| 230 | |||
| 231 | public function unquoteSimpleTableName(string $name): string |
||
| 232 | { |
||
| 233 | if (is_string($this->tableQuoteCharacter)) { |
||
| 234 | $startingCharacter = $this->tableQuoteCharacter; |
||
| 235 | } else { |
||
| 236 | $startingCharacter = $this->tableQuoteCharacter[0]; |
||
| 237 | } |
||
| 238 | |||
| 239 | return !str_starts_with($name, $startingCharacter) |
||
| 240 | ? $name |
||
| 241 | : substr($name, 1, -1); |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @psalm-param string[] $parts Parts of table name |
||
| 246 | * |
||
| 247 | * @psalm-return string[] |
||
| 248 | */ |
||
| 249 | protected function unquoteParts(array $parts, bool $withColumn): array |
||
| 260 | } |
||
| 261 | } |
||
| 262 |