Completed
Pull Request — master (#247)
by Jaap
11:03 queued 01:08
created

Utils::pregSplit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of phpDocumentor.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @link      http://phpdoc.org
12
 */
13
14
namespace phpDocumentor\Reflection;
15
16
use phpDocumentor\Reflection\Exception\PcreException;
17
use function preg_split as php_preg_split;
18
use function preg_last_error;
19
20
abstract class Utils
21
{
22
23
    /**
24
     * Wrapper function for phps preg_split
25
     *
26
     * This function is inspired by {@link https://github.com/thecodingmachine/safe/blob/master/generated/pcre.php}. But
27
     * since this library is all about performance we decided to strip everything we don't need. Reducing the amount
28
     * of files that have to be loaded, ect.
29
     *
30
     * @param string $pattern The pattern to search for, as a string.
31
     * @param string $subject The input string.
32
     * @param int|null $limit If specified, then only substrings up to limit are returned with the rest of the string being placed in the last substring. A limit of -1 or 0 means "no limit".
33
     * @param int $flags flags can be any combination of the following flags (combined with the | bitwise operator):
34
     * *PREG_SPLIT_NO_EMPTY*
35
     *      If this flag is set, only non-empty pieces will be returned by preg_split().
36
     * *PREG_SPLIT_DELIM_CAPTURE*
37
     *      If this flag is set, parenthesized expression in the delimiter pattern will be captured and returned as well.
38
     * *PREG_SPLIT_OFFSET_CAPTURE*
39
     *      If this flag is set, for every occurring match the appendant string offset will also be returned. Note that this changes the return value in an array where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1.
40
     *
41
     * @return string[] Returns an array containing substrings of subject split along boundaries matched by pattern
42
     *
43
     * @throws PcreException
44
     */
45
    public static function pregSplit(string $pattern, string $subject, ?int $limit = -1, int $flags = 0) : array
46
    {
47
        $parts = php_preg_split($pattern, $subject, $limit, $flags);
48
        if ($parts === false) {
49
            throw PcreException::createFromPhpError(preg_last_error());
50
        }
51
52
        return $parts;
53
    }
54
}
55