• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

azjezz / psl / 17432557268

03 Sep 2025 11:52AM UTC coverage: 98.446% (-0.07%) from 98.513%
17432557268

push

github

web-flow
chore: migrate from `psalm` to `mago` (#527)

232 of 241 new or added lines in 81 files covered. (96.27%)

14 existing lines in 12 files now uncovered.

5510 of 5597 relevant lines covered (98.45%)

52.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.91
/src/Psl/Str/range.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Psl\Str;
6

7
use Psl\Range\LowerBoundRangeInterface;
8
use Psl\Range\RangeInterface;
9
use Psl\Range\UpperBoundRangeInterface;
10

11
/**
12
 * Slice a string using a range.
13
 *
14
 * If the range doesn't have an upper range, the slice will contain the
15
 * rest of the string. If the upper-bound is equal to the lower-bound,
16
 * then an empty string will be returned.
17
 *
18
 * Example:
19
 *
20
 * ```php
21
 * use Psl\Range;
22
 * use Psl\Str;
23
 *
24
 * $string = 'Hello, World!';
25
 *
26
 * Str\range($string, Range\between(0, 3, upper_inclusive: true)); // 'Hell'
27
 * Str\range($string, Range\between(0, 3, upper_inclusive: false)); // 'Hel'
28
 * Str\range($string, Range\from(3)); // 'lo, World!'
29
 * Str\range($string, Range\to(3, true)); // 'Hell'
30
 * Str\range($string, Range\to(3, false)); // 'Hel'
31
 * Str\range($string, Range\full()); // 'Hello, World!'
32
 * Str\range($string, Range\between(7, 5, true)); // 'World'
33
 * ```
34
 *
35
 * @param RangeInterface $range
36
 *
37
 * @throws Exception\OutOfBoundsException If the $offset is out-of-bounds.
38
 *
39
 * @pure
40
 */
41
function range(string $string, RangeInterface $range, Encoding $encoding = Encoding::Utf8): string
42
{
43
    $offset = 0;
23✔
44
    $length = null;
23✔
45
    if ($range instanceof LowerBoundRangeInterface) {
23✔
46
        $offset = $range->getLowerBound();
20✔
47
    }
48

49
    if ($range instanceof UpperBoundRangeInterface) {
23✔
50
        $length = $range->getUpperBound() - $offset;
18✔
51
        if ($range->isUpperInclusive()) {
18✔
52
            $length += 1;
9✔
53
        }
54

55
        if ($length < 0) {
18✔
NEW
56
            $length = 0;
×
57
        }
58
    }
59

60
    return slice($string, $offset, $length, $encoding);
23✔
61
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc