Conditions | 82 |
Paths | > 20000 |
Total Lines | 400 |
Code Lines | 253 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
118 | public static function spanLtrRtl(string $inputText): string |
||
119 | { |
||
120 | if ($inputText === '') { |
||
121 | // Nothing to do |
||
122 | return ''; |
||
123 | } |
||
124 | |||
125 | $workingText = str_replace("\n", '<br>', $inputText); |
||
126 | $workingText = str_replace([ |
||
127 | '<span class="starredname"><br>', |
||
128 | '<span<br>class="starredname">', |
||
129 | ], '<br><span class="starredname">', $workingText); // Reposition some incorrectly placed line breaks |
||
130 | $workingText = self::stripLrmRlm($workingText); // Get rid of any existing UTF8 control codes |
||
131 | |||
132 | self::$previousState = ''; |
||
133 | self::$currentState = strtoupper(I18N::direction()); |
||
134 | $numberState = false; // Set when we're inside a numeric string |
||
135 | $result = ''; |
||
136 | self::$waitingText = ''; |
||
137 | $openParDirection = []; |
||
138 | |||
139 | self::beginCurrentSpan($result); |
||
140 | |||
141 | while ($workingText !== '') { |
||
142 | $charArray = self::getChar($workingText, 0); // Get the next ASCII or UTF-8 character |
||
143 | $currentLetter = $charArray['letter']; |
||
144 | $currentLen = $charArray['length']; |
||
145 | |||
146 | $openParIndex = strpos(self::OPEN_PARENTHESES, $currentLetter); // Which opening parenthesis is this? |
||
147 | $closeParIndex = strpos(self::CLOSE_PARENTHESES, $currentLetter); // Which closing parenthesis is this? |
||
148 | |||
149 | switch ($currentLetter) { |
||
150 | case '<': |
||
151 | // Assume this '<' starts an HTML element |
||
152 | $endPos = strpos($workingText, '>'); // look for the terminating '>' |
||
153 | if ($endPos === false) { |
||
154 | $endPos = 0; |
||
155 | } |
||
156 | $currentLen += $endPos; |
||
157 | $element = substr($workingText, 0, $currentLen); |
||
158 | $temp = strtolower(substr($element, 0, 3)); |
||
159 | if (strlen($element) < 7 && $temp === '<br') { |
||
160 | if ($numberState) { |
||
161 | $numberState = false; |
||
162 | if (self::$currentState === 'RTL') { |
||
163 | self::$waitingText .= self::UTF8_PDF; |
||
164 | } |
||
165 | } |
||
166 | self::breakCurrentSpan($result); |
||
167 | } elseif (self::$waitingText === '') { |
||
168 | $result .= $element; |
||
169 | } else { |
||
170 | self::$waitingText .= $element; |
||
171 | } |
||
172 | $workingText = substr($workingText, $currentLen); |
||
173 | break; |
||
174 | case '&': |
||
175 | // Assume this '&' starts an HTML entity |
||
176 | $endPos = strpos($workingText, ';'); // look for the terminating ';' |
||
177 | if ($endPos === false) { |
||
178 | $endPos = 0; |
||
179 | } |
||
180 | $currentLen += $endPos; |
||
181 | $entity = substr($workingText, 0, $currentLen); |
||
182 | if (strtolower($entity) === ' ') { |
||
183 | $entity = ' '; // Ensure consistent case for this entity |
||
184 | } |
||
185 | if (self::$waitingText === '') { |
||
186 | $result .= $entity; |
||
187 | } else { |
||
188 | self::$waitingText .= $entity; |
||
189 | } |
||
190 | $workingText = substr($workingText, $currentLen); |
||
191 | break; |
||
192 | case '{': |
||
193 | if (substr($workingText, 1, 1) === '{') { |
||
194 | // Assume this '{{' starts a TCPDF directive |
||
195 | $endPos = strpos($workingText, '}}'); // look for the terminating '}}' |
||
196 | if ($endPos === false) { |
||
197 | $endPos = 0; |
||
198 | } |
||
199 | $currentLen = $endPos + 2; |
||
200 | $directive = substr($workingText, 0, $currentLen); |
||
201 | $workingText = substr($workingText, $currentLen); |
||
202 | $result .= self::$waitingText . $directive; |
||
203 | self::$waitingText = ''; |
||
204 | break; |
||
205 | } |
||
206 | // no break |
||
207 | default: |
||
208 | // Look for strings of numbers with optional leading or trailing + or - |
||
209 | // and with optional embedded numeric punctuation |
||
210 | if ($numberState) { |
||
211 | // If we're inside a numeric string, look for reasons to end it |
||
212 | $offset = 0; // Be sure to look at the current character first |
||
213 | $charArray = self::getChar($workingText . "\n", $offset); |
||
214 | if (!str_contains(self::NUMBERS, $charArray['letter'])) { |
||
215 | // This is not a digit. Is it numeric punctuation? |
||
216 | if (substr($workingText . "\n", $offset, 6) === ' ') { |
||
217 | $offset += 6; // This could be numeric punctuation |
||
218 | } elseif (str_contains(self::NUMBER_PUNCTUATION, $charArray['letter'])) { |
||
219 | $offset += $charArray['length']; // This could be numeric punctuation |
||
220 | } |
||
221 | // If the next character is a digit, the current character is numeric punctuation |
||
222 | $charArray = self::getChar($workingText . "\n", $offset); |
||
223 | if (!str_contains(self::NUMBERS, $charArray['letter'])) { |
||
224 | // This is not a digit. End the run of digits and punctuation. |
||
225 | $numberState = false; |
||
226 | if (self::$currentState === 'RTL') { |
||
227 | if (!str_contains(self::NUMBER_PREFIX, $currentLetter)) { |
||
228 | $currentLetter = self::UTF8_PDF . $currentLetter; |
||
229 | } else { |
||
230 | $currentLetter .= self::UTF8_PDF; // Include a trailing + or - in the run |
||
231 | } |
||
232 | } |
||
233 | } |
||
234 | } |
||
235 | } elseif (str_contains(self::NUMBER_PREFIX, $currentLetter)) { |
||
236 | // If we're outside a numeric string, look for reasons to start it |
||
237 | // This might be a number lead-in |
||
238 | $offset = $currentLen; |
||
239 | $nextChar = substr($workingText . "\n", $offset, 1); |
||
240 | if (str_contains(self::NUMBERS, $nextChar)) { |
||
241 | $numberState = true; // We found a digit: the lead-in is therefore numeric |
||
242 | if (self::$currentState === 'RTL') { |
||
243 | $currentLetter = self::UTF8_LRE . $currentLetter; |
||
244 | } |
||
245 | } |
||
246 | } elseif (str_contains(self::NUMBERS, $currentLetter)) { |
||
247 | $numberState = true; // The current letter is a digit |
||
248 | if (self::$currentState === 'RTL') { |
||
249 | $currentLetter = self::UTF8_LRE . $currentLetter; |
||
250 | } |
||
251 | } |
||
252 | |||
253 | // Determine the directionality of the current UTF-8 character |
||
254 | $newState = self::$currentState; |
||
255 | |||
256 | while (true) { |
||
257 | if (I18N::scriptDirection(I18N::textScript($currentLetter)) === 'rtl') { |
||
258 | if (self::$currentState === '') { |
||
259 | $newState = 'RTL'; |
||
260 | break; |
||
261 | } |
||
262 | |||
263 | if (self::$currentState === 'RTL') { |
||
264 | break; |
||
265 | } |
||
266 | // Switch to RTL only if this isn't a solitary RTL letter |
||
267 | $tempText = substr($workingText, $currentLen); |
||
268 | while ($tempText !== '') { |
||
269 | $nextCharArray = self::getChar($tempText, 0); |
||
270 | $nextLetter = $nextCharArray['letter']; |
||
271 | $nextLen = $nextCharArray['length']; |
||
272 | $tempText = substr($tempText, $nextLen); |
||
273 | |||
274 | if (I18N::scriptDirection(I18N::textScript($nextLetter)) === 'rtl') { |
||
275 | $newState = 'RTL'; |
||
276 | break 2; |
||
277 | } |
||
278 | |||
279 | if (str_contains(self::PUNCTUATION, $nextLetter) || str_contains(self::OPEN_PARENTHESES, $nextLetter)) { |
||
280 | $newState = 'RTL'; |
||
281 | break 2; |
||
282 | } |
||
283 | |||
284 | if ($nextLetter === ' ') { |
||
285 | break; |
||
286 | } |
||
287 | $nextLetter .= substr($tempText . "\n", 0, 5); |
||
288 | if ($nextLetter === ' ') { |
||
289 | break; |
||
290 | } |
||
291 | } |
||
292 | // This is a solitary RTL letter : wrap it in UTF8 control codes to force LTR directionality |
||
293 | $currentLetter = self::UTF8_LRO . $currentLetter . self::UTF8_PDF; |
||
294 | $newState = 'LTR'; |
||
295 | break; |
||
296 | } |
||
297 | if ($currentLen !== 1 || $currentLetter >= 'A' && $currentLetter <= 'Z' || $currentLetter >= 'a' && $currentLetter <= 'z') { |
||
298 | // Since it’s neither Hebrew nor Arabic, this UTF-8 character or ASCII letter must be LTR |
||
299 | $newState = 'LTR'; |
||
300 | break; |
||
301 | } |
||
302 | if ($closeParIndex !== false) { |
||
303 | // This closing parenthesis has to inherit the matching opening parenthesis' directionality |
||
304 | if (!empty($openParDirection[$closeParIndex]) && $openParDirection[$closeParIndex] !== '?') { |
||
305 | $newState = $openParDirection[$closeParIndex]; |
||
306 | } |
||
307 | $openParDirection[$closeParIndex] = ''; |
||
308 | break; |
||
309 | } |
||
310 | self::$waitingText .= $currentLetter; |
||
311 | $workingText = substr($workingText, $currentLen); |
||
312 | if ($openParIndex !== false) { |
||
313 | // Opening parentheses always inherit the following directionality |
||
314 | while (true) { |
||
315 | if ($workingText === '') { |
||
316 | break; |
||
317 | } |
||
318 | if (str_starts_with($workingText, ' ')) { |
||
319 | // Spaces following this left parenthesis inherit the following directionality too |
||
320 | self::$waitingText .= ' '; |
||
321 | $workingText = substr($workingText, 1); |
||
322 | continue; |
||
323 | } |
||
324 | if (str_starts_with($workingText, ' ')) { |
||
325 | // Spaces following this left parenthesis inherit the following directionality too |
||
326 | self::$waitingText .= ' '; |
||
327 | $workingText = substr($workingText, 6); |
||
328 | continue; |
||
329 | } |
||
330 | break; |
||
331 | } |
||
332 | $openParDirection[$openParIndex] = '?'; |
||
333 | break 2; // double break because we're waiting for more information |
||
334 | } |
||
335 | |||
336 | // We have a digit or a "normal" special character. |
||
337 | // |
||
338 | // When this character is not at the start of the input string, it inherits the preceding directionality; |
||
339 | // at the start of the input string, it assumes the following directionality. |
||
340 | // |
||
341 | // Exceptions to this rule will be handled later during final clean-up. |
||
342 | // |
||
343 | if (self::$currentState !== '') { |
||
344 | $result .= self::$waitingText; |
||
345 | self::$waitingText = ''; |
||
346 | } |
||
347 | break 2; // double break because we're waiting for more information |
||
348 | } |
||
349 | if ($newState !== self::$currentState) { |
||
350 | // A direction change has occurred |
||
351 | self::finishCurrentSpan($result); |
||
352 | self::$previousState = self::$currentState; |
||
353 | self::$currentState = $newState; |
||
354 | self::beginCurrentSpan($result); |
||
355 | } |
||
356 | self::$waitingText .= $currentLetter; |
||
357 | $workingText = substr($workingText, $currentLen); |
||
358 | $result .= self::$waitingText; |
||
359 | self::$waitingText = ''; |
||
360 | |||
361 | foreach ($openParDirection as $index => $value) { |
||
362 | // Since we now know the proper direction, remember it for all waiting opening parentheses |
||
363 | if ($value === '?') { |
||
364 | $openParDirection[$index] = self::$currentState; |
||
365 | } |
||
366 | } |
||
367 | |||
368 | break; |
||
369 | } |
||
370 | } |
||
371 | |||
372 | // We're done. Finish last <span> if necessary |
||
373 | if ($numberState) { |
||
374 | if (self::$waitingText === '') { |
||
375 | if (self::$currentState === 'RTL') { |
||
376 | $result .= self::UTF8_PDF; |
||
377 | } |
||
378 | } elseif (self::$currentState === 'RTL') { |
||
379 | self::$waitingText .= self::UTF8_PDF; |
||
380 | } |
||
381 | } |
||
382 | self::finishCurrentSpan($result, true); |
||
383 | |||
384 | // Get rid of any waiting text |
||
385 | if (self::$waitingText !== '') { |
||
386 | if (I18N::direction() === 'rtl' && self::$currentState === 'LTR') { |
||
387 | $result .= self::START_RTL; |
||
388 | $result .= self::$waitingText; |
||
389 | $result .= self::END_RTL; |
||
390 | } else { |
||
391 | $result .= self::START_LTR; |
||
392 | $result .= self::$waitingText; |
||
393 | $result .= self::END_LTR; |
||
394 | } |
||
395 | self::$waitingText = ''; |
||
396 | } |
||
397 | |||
398 | // Lastly, do some more cleanups |
||
399 | |||
400 | // Move leading RTL numeric strings to following LTR text |
||
401 | // (this happens when the page direction is RTL and the original text begins with a number and is followed by LTR text) |
||
402 | while (substr($result, 0, self::LENGTH_START + 3) === self::START_RTL . self::UTF8_LRE) { |
||
403 | $spanEnd = strpos($result, self::END_RTL . self::START_LTR); |
||
404 | if ($spanEnd === false) { |
||
405 | break; |
||
406 | } |
||
407 | $textSpan = self::stripLrmRlm(substr($result, self::LENGTH_START + 3, $spanEnd - self::LENGTH_START - 3)); |
||
408 | if (I18N::scriptDirection(I18N::textScript($textSpan)) === 'rtl') { |
||
409 | break; |
||
410 | } |
||
411 | $result = self::START_LTR . substr($result, self::LENGTH_START, $spanEnd - self::LENGTH_START) . substr($result, $spanEnd + self::LENGTH_START + self::LENGTH_END); |
||
412 | break; |
||
413 | } |
||
414 | |||
415 | // On RTL pages, put trailing "." in RTL numeric strings into its own RTL span |
||
416 | if (I18N::direction() === 'rtl') { |
||
417 | $result = str_replace(self::UTF8_PDF . '.' . self::END_RTL, self::UTF8_PDF . self::END_RTL . self::START_RTL . '.' . self::END_RTL, $result); |
||
418 | } |
||
419 | |||
420 | // Trim trailing blanks preceding <br> in LTR text |
||
421 | while (self::$previousState !== 'RTL') { |
||
422 | if (str_contains($result, ' <LTRbr>')) { |
||
423 | $result = str_replace(' <LTRbr>', '<LTRbr>', $result); |
||
424 | continue; |
||
425 | } |
||
426 | if (str_contains($result, ' <LTRbr>')) { |
||
427 | $result = str_replace(' <LTRbr>', '<LTRbr>', $result); |
||
428 | continue; |
||
429 | } |
||
430 | if (str_contains($result, ' <br>')) { |
||
431 | $result = str_replace(' <br>', '<br>', $result); |
||
432 | continue; |
||
433 | } |
||
434 | if (str_contains($result, ' <br>')) { |
||
435 | $result = str_replace(' <br>', '<br>', $result); |
||
436 | continue; |
||
437 | } |
||
438 | break; // Neither space nor : we're done |
||
439 | } |
||
440 | |||
441 | // Trim trailing blanks preceding <br> in RTL text |
||
442 | while (true) { |
||
443 | if (str_contains($result, ' <RTLbr>')) { |
||
444 | $result = str_replace(' <RTLbr>', '<RTLbr>', $result); |
||
445 | continue; |
||
446 | } |
||
447 | if (str_contains($result, ' <RTLbr>')) { |
||
448 | $result = str_replace(' <RTLbr>', '<RTLbr>', $result); |
||
449 | continue; |
||
450 | } |
||
451 | break; // Neither space nor : we're done |
||
452 | } |
||
453 | |||
454 | // Convert '<LTRbr>' and '<RTLbr' |
||
455 | $result = str_replace([ |
||
456 | '<LTRbr>', |
||
457 | '<RTLbr>', |
||
458 | ], [ |
||
459 | self::END_LTR . '<br>' . self::START_LTR, |
||
460 | self::END_RTL . '<br>' . self::START_RTL, |
||
461 | ], $result); |
||
462 | |||
463 | // Include leading indeterminate directional text in whatever follows |
||
464 | if (substr($result . "\n", 0, self::LENGTH_START) !== self::START_LTR && substr($result . "\n", 0, self::LENGTH_START) !== self::START_RTL && !str_starts_with($result . "\n", '<br>')) { |
||
465 | $leadingText = ''; |
||
466 | while (true) { |
||
467 | if ($result === '') { |
||
468 | $result = $leadingText; |
||
469 | break; |
||
470 | } |
||
471 | if (substr($result . "\n", 0, self::LENGTH_START) !== self::START_LTR && substr($result . "\n", 0, self::LENGTH_START) !== self::START_RTL) { |
||
472 | $leadingText .= substr($result, 0, 1); |
||
473 | $result = substr($result, 1); |
||
474 | continue; |
||
475 | } |
||
476 | $result = substr($result, 0, self::LENGTH_START) . $leadingText . substr($result, self::LENGTH_START); |
||
477 | break; |
||
478 | } |
||
479 | } |
||
480 | |||
481 | // Include solitary "-" and "+" in surrounding RTL text |
||
482 | $result = str_replace([ |
||
483 | self::END_RTL . self::START_LTR . '-' . self::END_LTR . self::START_RTL, |
||
484 | self::END_RTL . self::START_LTR . '+' . self::END_LTR . self::START_RTL, |
||
485 | ], [ |
||
486 | '-', |
||
487 | '+', |
||
488 | ], $result); |
||
489 | |||
490 | //$result = strtr($result, [ |
||
491 | // self::END_RTL . self::START_LTR . '-' . self::END_LTR . self::START_RTL => '-', |
||
492 | // self::END_RTL . self::START_LTR . '+' . self::END_LTR . self::START_RTL => '+', |
||
493 | //]); |
||
494 | |||
495 | // Remove empty spans |
||
496 | $result = str_replace([ |
||
497 | self::START_LTR . self::END_LTR, |
||
498 | self::START_RTL . self::END_RTL, |
||
499 | ], '', $result); |
||
500 | |||
501 | // Finally, correct '<LTR>', '</LTR>', '<RTL>', and '</RTL>' |
||
502 | // LTR text: <span dir="ltr"> text </span> |
||
503 | // RTL text: <span dir="rtl"> text </span> |
||
504 | |||
505 | $result = str_replace([ |
||
506 | self::START_LTR, |
||
507 | self::END_LTR, |
||
508 | self::START_RTL, |
||
509 | self::END_RTL, |
||
510 | ], [ |
||
511 | '<span dir="ltr">', |
||
512 | '</span>', |
||
513 | '<span dir="rtl">', |
||
514 | '</span>', |
||
515 | ], $result); |
||
516 | |||
517 | return $result; |
||
518 | } |
||
1153 |