StringUtil::joinCommaSeparatedList()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Xml\Impl\Util;
4
5
class StringUtil
6
{
7
    public const PATTERN = '/(\w[^,]*)|([#$]\{[^}]*\})/mu';
8
9
    public static function splitCommaSeparatedList(?string $text): array
10
    {
11
        if (empty($text)) {
12
            return [];
13
        }
14
        preg_match_all(self::PATTERN, $text, $matches);
15
        $parts = [];
16
        foreach ($matches[0] as $match) {
17
            $parts[] = trim($match);
18
        }
19
        return $parts;
20
    }
21
22
    public static function joinCommaSeparatedList(?array $list): ?string
23
    {
24
        return self::joinList($list, ", ");
25
    }
26
27
    public static function splitListBySeparator(?string $text, string $separator): array
28
    {
29
        if (!empty($text)) {
30
            return explode($separator, $text);
31
        }
32
        return [];
33
    }
34
35
    public static function joinList(?array $list, string $separator): ?string
36
    {
37
        return $list === null ? null : implode($separator, $list);
0 ignored issues
show
introduced by
The condition $list === null is always false.
Loading history...
38
    }
39
}
40