Passed
Push — master ( 1b6475...610123 )
by Petr
10:18
created

Strings   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 27
rs 10
c 1
b 0
f 0
wmc 7
1
<?php
2
3
namespace Support;
4
5
6
use kalanis\UploadPerPartes\Exceptions\UploadException;
7
8
9
/**
10
 * Class Strings
11
 * @package Support
12
 * Processing Strings - reimplementation of necessary methods (contains fuckups)
13
 */
14
class Strings
15
{
16
    /**
17
     * Original one returns shitty results, so need re-implement parts
18
     * It's necessary to have nullable limit, not only set as undefined
19
     * @param string $what
20
     * @param int $offset
21
     * @param int|null $limit
22
     * @param string $errorMessage
23
     * @throws UploadException
24
     * @return string
25
     */
26
    public static function substr(string $what, int $offset, ?int $limit, string $errorMessage = ''): string
27
    {
28
        $length = strlen($what);
29
        if (!is_null($limit) && ($limit > $length)) { // not over
30
            $limit = null;
31
        }
32
        if (empty($limit)) {
33
            $result = (!empty($offset)) ? substr($what, $offset) : $what ;
34
        } else {
35
            $result = (!empty($offset)) ? substr($what, $offset, $limit) : substr($what, 0, $limit);
36
        }
37
        if (false === $result) {
38
            throw new UploadException($errorMessage); // failed substr
39
        }
40
        return $result;
41
    }
42
}
43