Total Complexity | 50 |
Total Lines | 239 |
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 | /** |
||
87 | * Returns the actual name of a given table name. |
||
88 | * |
||
89 | * This method will strip off curly brackets from the given table name and replace the percentage character '%' with |
||
90 | * {@see ConnectionInterface::tablePrefix}. |
||
91 | * |
||
92 | * @param string $name The table name to convert. |
||
93 | * |
||
94 | * @return string The real name of the given table name. |
||
95 | */ |
||
96 | public function getRawTableName(string $name): string |
||
105 | } |
||
106 | |||
107 | public function getTableNameParts(string $name, bool $withColumn = false): array |
||
108 | { |
||
109 | $parts = array_slice(explode('.', $name), -2, 2); |
||
110 | |||
111 | return $this->unquoteParts($parts, $withColumn); |
||
112 | } |
||
113 | |||
114 | public function ensureNameQuoted(string $name): string |
||
115 | { |
||
116 | $name = str_replace(["'", '"', '`', '[', ']'], '', $name); |
||
117 | |||
118 | if ($name && !preg_match('/^{{.*}}$/', $name)) { |
||
119 | return '{{' . $name . '}}'; |
||
120 | } |
||
121 | |||
122 | return $name; |
||
123 | } |
||
124 | |||
125 | public function ensureColumnName(string $name): string |
||
133 | } |
||
134 | |||
135 | public function quoteColumnName(string $name): string |
||
136 | { |
||
137 | if (str_contains($name, '(') || str_contains($name, '[[')) { |
||
138 | return $name; |
||
139 | } |
||
140 | |||
141 | if (($pos = strrpos($name, '.')) !== false) { |
||
142 | $prefix = $this->quoteTableName(substr($name, 0, $pos)) . '.'; |
||
143 | $name = substr($name, $pos + 1); |
||
144 | } else { |
||
145 | $prefix = ''; |
||
146 | } |
||
147 | |||
148 | if (str_contains($name, '{{')) { |
||
149 | return $name; |
||
150 | } |
||
151 | |||
152 | return $prefix . $this->quoteSimpleColumnName($name); |
||
153 | } |
||
154 | |||
155 | public function quoteSimpleColumnName(string $name): string |
||
156 | { |
||
157 | if (is_string($this->columnQuoteCharacter)) { |
||
158 | $startingCharacter = $endingCharacter = $this->columnQuoteCharacter; |
||
159 | } else { |
||
160 | [$startingCharacter, $endingCharacter] = $this->columnQuoteCharacter; |
||
161 | } |
||
162 | |||
163 | return $name === '*' || str_starts_with($name, $startingCharacter) |
||
164 | ? $name |
||
165 | : $startingCharacter . $name . $endingCharacter; |
||
166 | } |
||
167 | |||
168 | public function quoteSimpleTableName(string $name): string |
||
169 | { |
||
170 | if (is_string($this->tableQuoteCharacter)) { |
||
171 | $startingCharacter = $endingCharacter = $this->tableQuoteCharacter; |
||
172 | } else { |
||
173 | [$startingCharacter, $endingCharacter] = $this->tableQuoteCharacter; |
||
174 | } |
||
175 | |||
176 | return str_starts_with($name, $startingCharacter) |
||
177 | ? $name |
||
178 | : $startingCharacter . $name . $endingCharacter; |
||
179 | } |
||
180 | |||
181 | public function quoteSql(string $sql): string |
||
182 | { |
||
183 | return preg_replace_callback( |
||
184 | '/({{(%?[\w\-. ]+)%?}}|\\[\\[([\w\-. ]+)]])/', |
||
185 | function ($matches) { |
||
186 | if (isset($matches[3])) { |
||
187 | return $this->quoteColumnName($matches[3]); |
||
188 | } |
||
189 | |||
190 | return str_replace('%', $this->tablePrefix, $this->quoteTableName($matches[2])); |
||
191 | }, |
||
192 | $sql |
||
193 | ); |
||
194 | } |
||
195 | |||
196 | public function quoteTableName(string $name): string |
||
217 | } |
||
218 | |||
219 | public function quoteValue(mixed $value): mixed |
||
220 | { |
||
221 | if (!is_string($value)) { |
||
222 | return $value; |
||
223 | } |
||
224 | |||
225 | return "'" . str_replace("'", "''", addcslashes($value, "\000\032")) . "'"; |
||
226 | } |
||
227 | |||
228 | public function setTablePrefix(string $value): void |
||
229 | { |
||
230 | $this->tablePrefix = $value; |
||
231 | } |
||
232 | |||
233 | public function unquoteSimpleColumnName(string $name): string |
||
234 | { |
||
235 | if (is_string($this->columnQuoteCharacter)) { |
||
236 | $startingCharacter = $this->columnQuoteCharacter; |
||
237 | } else { |
||
238 | $startingCharacter = $this->columnQuoteCharacter[0]; |
||
239 | } |
||
240 | |||
241 | return !str_starts_with($name, $startingCharacter) |
||
242 | ? $name |
||
243 | : substr($name, 1, -1); |
||
244 | } |
||
245 | |||
246 | public function unquoteSimpleTableName(string $name): string |
||
247 | { |
||
248 | if (is_string($this->tableQuoteCharacter)) { |
||
249 | $startingCharacter = $this->tableQuoteCharacter; |
||
250 | } else { |
||
251 | $startingCharacter = $this->tableQuoteCharacter[0]; |
||
252 | } |
||
253 | |||
254 | return !str_starts_with($name, $startingCharacter) |
||
255 | ? $name |
||
256 | : substr($name, 1, -1); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * @psalm-param string[] $parts Parts of table name |
||
261 | * |
||
262 | * @psalm-return string[] |
||
263 | */ |
||
264 | protected function unquoteParts(array $parts, bool $withColumn): array |
||
275 | } |
||
276 | } |
||
277 |