SplitterText   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 36
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C split() 0 33 8
1
<?php
2
3
namespace TTS\Helper;
4
5
class SplitterText extends AbstractHelper
6
{
7
    public function split($text, $numSymbols)
8
    {
9
        $lines = [];
10
        $char = 0;
11
        $lastPos = 0;
12
        $goodChar = 0;
13
14
        for ($i = 0; $i < mb_strlen($text); $i++) {
15
            $char++;
16
17
            if (mb_substr($text, $i, 1) == ' ') {
18
                $goodChar = $i;
19
            }
20
21
            if ($char > $numSymbols) {
22
                $lines[] = trim(mb_substr($text, $lastPos, $goodChar - $lastPos));
23
                $char = 0;
24
                $lastPos = $goodChar;
25
            } elseif (mb_substr($text, $i, 2) == '. ' && ($goodChar - $lastPos > 5)) {
26
                $goodChar = $i + 1;
27
                $lines[] = trim(mb_substr($text, $lastPos, $goodChar - $lastPos));
28
                $char = 0;
29
                $lastPos = $goodChar;
30
            } elseif (mb_substr($text, $i, 2) == '! ' && ($goodChar - $lastPos > 5)) {
31
                $goodChar = $i + 1;
32
                $lines[] = trim(mb_substr($text, $lastPos, $goodChar - $lastPos));
33
                $char = 0;
34
                $lastPos = $goodChar;
35
            }
36
        }
37
        $lines[] = trim(mb_substr($text, $lastPos, $lastPos + $char));
38
        return $lines;
39
    }
40
}
41