Total Complexity | 50 |
Total Lines | 229 |
Duplicated Lines | 0 % |
Changes | 4 | ||
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 |
||
216 | } |
||
217 | |||
218 | public function setTablePrefix(string $value): void |
||
219 | { |
||
220 | $this->tablePrefix = $value; |
||
221 | } |
||
222 | |||
223 | public function unquoteSimpleColumnName(string $name): string |
||
224 | { |
||
225 | if (is_string($this->columnQuoteCharacter)) { |
||
226 | $startingCharacter = $this->columnQuoteCharacter; |
||
227 | } else { |
||
228 | $startingCharacter = $this->columnQuoteCharacter[0]; |
||
229 | } |
||
230 | |||
231 | return !str_starts_with($name, $startingCharacter) |
||
232 | ? $name |
||
233 | : substr($name, 1, -1); |
||
234 | } |
||
235 | |||
236 | public function unquoteSimpleTableName(string $name): string |
||
237 | { |
||
238 | if (is_string($this->tableQuoteCharacter)) { |
||
239 | $startingCharacter = $this->tableQuoteCharacter; |
||
240 | } else { |
||
241 | $startingCharacter = $this->tableQuoteCharacter[0]; |
||
242 | } |
||
243 | |||
244 | return !str_starts_with($name, $startingCharacter) |
||
245 | ? $name |
||
246 | : substr($name, 1, -1); |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * @psalm-param string[] $parts Parts of table name |
||
251 | * |
||
252 | * @psalm-return string[] |
||
253 | */ |
||
254 | protected function unquoteParts(array $parts, bool $withColumn): array |
||
265 | } |
||
266 | } |
||
267 |