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