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

MbStringUtil::substr()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 3
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
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