Conditions | 4 |
Paths | 4 |
Total Lines | 21 |
Code Lines | 11 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
38 | function breakLine(string $line, int $maxLength): array |
||
39 | { |
||
40 | if (\strlen($line) < ($maxLength + 5)) { |
||
41 | return [$line]; |
||
42 | } |
||
43 | |||
44 | $pos = $maxLength - 40; |
||
45 | $parts = \preg_split("/((?: |^).{15,{$pos}}(?= |$))/", $line); |
||
46 | $partsCount = \count($parts); |
||
47 | |||
48 | if ($partsCount < 4) { |
||
49 | return [$line]; |
||
50 | } |
||
51 | |||
52 | $subLines = [$parts[0] . $parts[1] . $parts[2]]; |
||
53 | |||
54 | for ($i = 3; $i < $partsCount; $i++) { |
||
55 | $subLines[] = \array_slice($parts[$i], 1) . $parts[$i + 1]; |
||
56 | } |
||
57 | |||
58 | return $subLines; |
||
59 | } |
||
69 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: