Passed
Push — master ( 6f39bc...2ba271 )
by Josh
02:32
created

MbStringUtil   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 52
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A strpos() 0 7 2
A stripos() 0 7 2
A __construct() 0 8 3
A substr() 0 7 2
A strlen() 0 7 2
1
<?php
2
3
namespace Caxy\HtmlDiff\Util;
4
5
/**
6
 * Using multi-byte functions have a huge performance impact on the diff algorithm.
7
 *
8
 * Therefor we added this wrapper around common string function that only uses mb_* functions if they
9
 * are necessary for the data-set we are processing.
10
 */
11
class MbStringUtil
12
{
13
    /**
14
     * @var bool
15
     */
16
    protected $mbRequired;
17
18 16
    public function __construct($oldText, $newText)
19
    {
20 16
        $this->mbRequired =
21 16
            strlen($oldText) !== mb_strlen($oldText) ||
22 15
            strlen($newText) !== mb_strlen($newText);
23
24 16
        if (true === $this->mbRequired) {
25 2
            mb_substitute_character(0x20);
26
        }
27 16
    }
28
29 16
    public function strlen($string)
30
    {
31 16
        if (true === $this->mbRequired) {
32 2
            return mb_strlen($string);
33
        }
34
35 14
        return strlen($string);
36
    }
37
38 9
    public function strpos($haystack, $needle, $offset = 0)
39
    {
40 9
        if (true === $this->mbRequired) {
41 1
            return mb_strpos($haystack, $needle, $offset);
42
        }
43
44 8
        return strpos($haystack, $needle, $offset);
45
    }
46
47 15
    public function stripos($haystack, $needle, $offset = 0)
48
    {
49 15
        if (true === $this->mbRequired) {
50 1
            return mb_stripos($haystack, $needle, $offset);
51
        }
52
53 14
        return stripos($haystack, $needle, $offset);
54
    }
55
56 7
    public function substr($string, $start, $length = null)
57
    {
58 7
        if (true === $this->mbRequired) {
59 1
            return mb_substr($string, $start, $length);
60
        }
61
62 6
        return substr($string, $start, $length);
63
    }
64
}
65