ConsoleHelper::stringToArray()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 13
c 1
b 0
f 1
nc 4
nop 3
dl 0
loc 24
rs 9.8333
ccs 0
cts 14
cp 0
crap 20
1
<?php
2
3
namespace Slides\Connector\Auth\Helpers;
4
5
use Illuminate\Support\Arr;
6
7
/**
8
 * Class ConsoleHelper
9
 *
10
 * @package Slides\Connector\Auth\Helpers
11
 */
12
class ConsoleHelper
13
{
14
    /**
15
     * Convert a string like "field1:value1,field2:value" to the array
16
     *
17
     * Also supports one-dimensional array in the representation "value1,value2,value3"
18
     *
19
     * @param string|null $string
20
     * @param string $valueDelimiter
21
     * @param string $itemDelimiter
22
     *
23
     * @return array
24
     */
25
    public static function stringToArray(string $string = null, string $valueDelimiter = ':', string $itemDelimiter = ',')
26
    {
27
        if(is_null($string)) {
28
            return [];
29
        }
30
31
        $values = [];
32
        $items = preg_split('/' . preg_quote($itemDelimiter) . '/', $string, -1, PREG_SPLIT_NO_EMPTY);
33
34
        foreach ($items as $index => $item) {
35
            $item = explode($valueDelimiter, $item);
36
37
            $key = Arr::get($item, 0);
38
            $value = Arr::get($item, 1);
39
40
            if(is_null($value)) {
41
                $value = $key;
42
                $key = $index;
43
            }
44
45
            $values[trim($key)] = trim($value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type string[]; however, parameter $str of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
            $values[trim($key)] = trim(/** @scrutinizer ignore-type */ $value);
Loading history...
46
        }
47
48
        return $values;
49
    }
50
}