| Total Complexity | 7 |
| Total Lines | 27 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 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 | } |
||
| 43 |