| Total Complexity | 48 |
| Total Lines | 207 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 28 | class Quoter implements QuoterInterface |
||
| 29 | { |
||
| 30 | public function __construct( |
||
| 37 | } |
||
| 38 | |||
| 39 | public function cleanUpTableNames(array $tableNames): array |
||
| 40 | { |
||
| 41 | $cleanedUpTableNames = []; |
||
| 42 | $pattern = <<<PATTERN |
||
| 43 | ~^\s*((?:['"`\[]|{{).*?(?:['"`\]]|}})|\(.*?\)|.*?)(?:(?:\s+(?:as\s+)?)((?:['"`\[]|{{).*?(?:['"`\]]|}})|.*?))?\s*$~iux |
||
| 44 | PATTERN; |
||
| 45 | |||
| 46 | /** @psalm-var array<array-key, ExpressionInterface|string> $tableNames */ |
||
| 47 | foreach ($tableNames as $alias => $tableName) { |
||
| 48 | if (is_string($tableName) && !is_string($alias)) { |
||
| 49 | if (preg_match($pattern, $tableName, $matches)) { |
||
| 50 | if (isset($matches[2])) { |
||
| 51 | [, $tableName, $alias] = $matches; |
||
| 52 | } else { |
||
| 53 | $tableName = $alias = $matches[1]; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | if (!is_string($alias)) { |
||
| 59 | throw new InvalidArgumentException( |
||
| 60 | 'To use Expression in from() method, pass it in array format with alias.' |
||
| 61 | ); |
||
| 62 | } |
||
| 63 | |||
| 64 | if (is_string($tableName)) { |
||
| 65 | $cleanedUpTableNames[$this->ensureNameQuoted($alias)] = $this->ensureNameQuoted($tableName); |
||
| 66 | } elseif ($tableName instanceof ExpressionInterface) { |
||
| 67 | $cleanedUpTableNames[$this->ensureNameQuoted($alias)] = $tableName; |
||
| 68 | } else { |
||
| 69 | throw new InvalidArgumentException( |
||
| 70 | 'Use ExpressionInterface without cast to string as object of tableName' |
||
| 71 | ); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | return $cleanedUpTableNames; |
||
| 76 | } |
||
| 77 | |||
| 78 | public function getTableNameParts(string $name, bool $withColumn = false): array |
||
| 79 | { |
||
| 80 | $parts = array_slice(explode('.', $name), -2, 2); |
||
| 81 | |||
| 82 | return $this->unquoteParts($parts, $withColumn); |
||
| 83 | } |
||
| 84 | |||
| 85 | public function ensureNameQuoted(string $name): string |
||
| 86 | { |
||
| 87 | $name = str_replace(["'", '"', '`', '[', ']'], '', $name); |
||
| 88 | |||
| 89 | if ($name && !preg_match('/^{{.*}}$/', $name)) { |
||
| 90 | return '{{' . $name . '}}'; |
||
| 91 | } |
||
| 92 | |||
| 93 | return $name; |
||
| 94 | } |
||
| 95 | |||
| 96 | public function ensureColumnName(string $name): string |
||
| 104 | } |
||
| 105 | |||
| 106 | public function quoteColumnName(string $name): string |
||
| 107 | { |
||
| 108 | if (str_contains($name, '(') || str_contains($name, '[[')) { |
||
| 109 | return $name; |
||
| 110 | } |
||
| 111 | |||
| 112 | if (($pos = strrpos($name, '.')) !== false) { |
||
| 113 | $prefix = $this->quoteTableName(substr($name, 0, $pos)) . '.'; |
||
| 114 | $name = substr($name, $pos + 1); |
||
| 115 | } else { |
||
| 116 | $prefix = ''; |
||
| 117 | } |
||
| 118 | |||
| 119 | if (str_contains($name, '{{')) { |
||
| 120 | return $name; |
||
| 121 | } |
||
| 122 | |||
| 123 | return $prefix . $this->quoteSimpleColumnName($name); |
||
| 124 | } |
||
| 125 | |||
| 126 | public function quoteSimpleColumnName(string $name): string |
||
| 127 | { |
||
| 128 | if (is_string($this->columnQuoteCharacter)) { |
||
| 129 | $startingCharacter = $endingCharacter = $this->columnQuoteCharacter; |
||
| 130 | } else { |
||
| 131 | [$startingCharacter, $endingCharacter] = $this->columnQuoteCharacter; |
||
| 132 | } |
||
| 133 | |||
| 134 | return $name === '*' || str_contains($name, $startingCharacter) ? $name : $startingCharacter . $name |
||
| 135 | . $endingCharacter; |
||
| 136 | } |
||
| 137 | |||
| 138 | public function quoteSimpleTableName(string $name): string |
||
| 139 | { |
||
| 140 | if (is_string($this->tableQuoteCharacter)) { |
||
| 141 | $startingCharacter = $endingCharacter = $this->tableQuoteCharacter; |
||
| 142 | } else { |
||
| 143 | [$startingCharacter, $endingCharacter] = $this->tableQuoteCharacter; |
||
| 144 | } |
||
| 145 | |||
| 146 | return str_contains($name, $startingCharacter) ? $name : $startingCharacter . $name . $endingCharacter; |
||
| 147 | } |
||
| 148 | |||
| 149 | public function quoteSql(string $sql): string |
||
| 150 | { |
||
| 151 | return preg_replace_callback( |
||
| 152 | '/({{(%?[\w\-. ]+%?)}}|\\[\\[([\w\-. ]+)]])/', |
||
| 153 | function ($matches) { |
||
| 154 | if (isset($matches[3])) { |
||
| 155 | return $this->quoteColumnName($matches[3]); |
||
| 156 | } |
||
| 157 | |||
| 158 | return str_replace('%', $this->tablePrefix, $this->quoteTableName($matches[2])); |
||
| 159 | }, |
||
| 160 | $sql |
||
| 161 | ); |
||
| 162 | } |
||
| 163 | |||
| 164 | public function quoteTableName(string $name): string |
||
| 185 | } |
||
| 186 | |||
| 187 | public function quoteValue(mixed $value): mixed |
||
| 188 | { |
||
| 189 | if (!is_string($value)) { |
||
| 190 | return $value; |
||
| 191 | } |
||
| 192 | |||
| 193 | return '\'' . str_replace('\'', '\'\'', addcslashes($value, "\000\032")) . '\''; |
||
| 194 | } |
||
| 195 | |||
| 196 | public function unquoteSimpleColumnName(string $name): string |
||
| 197 | { |
||
| 198 | if (is_string($this->columnQuoteCharacter)) { |
||
| 199 | $startingCharacter = $this->columnQuoteCharacter; |
||
| 200 | } else { |
||
| 201 | $startingCharacter = $this->columnQuoteCharacter[0]; |
||
| 202 | } |
||
| 203 | |||
| 204 | return !str_contains($name, $startingCharacter) ? $name : substr($name, 1, -1); |
||
| 205 | } |
||
| 206 | |||
| 207 | public function unquoteSimpleTableName(string $name): string |
||
| 208 | { |
||
| 209 | if (is_string($this->tableQuoteCharacter)) { |
||
| 210 | $startingCharacter = $this->tableQuoteCharacter; |
||
| 211 | } else { |
||
| 212 | $startingCharacter = $this->tableQuoteCharacter[0]; |
||
| 213 | } |
||
| 214 | |||
| 215 | return !str_contains($name, $startingCharacter) ? $name : substr($name, 1, -1); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param string[] $parts |
||
| 220 | * @param bool $withColumn |
||
| 221 | * |
||
| 222 | * @return string[] |
||
| 223 | */ |
||
| 224 | protected function unquoteParts(array $parts, bool $withColumn): array |
||
| 235 | } |
||
| 236 | } |
||
| 237 |