Completed
Push — master ( d7e260...fc7aea )
by Artem
10:19
created

ConsoleHelper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 37
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A stringToArray() 0 24 4
1
<?php
2
3
namespace Slides\Connector\Auth\Helpers;
4
5
/**
6
 * Class ConsoleHelper
7
 *
8
 * @package Slides\Connector\Auth\Helpers
9
 */
10
class ConsoleHelper
11
{
12
    /**
13
     * Convert a string like "field1:value1,field2:value" to the array
14
     *
15
     * Also supports one-dimensional array in the representation "value1,value2,value3"
16
     *
17
     * @param string|null $string
18
     * @param string $valueDelimiter
19
     * @param string $itemDelimiter
20
     *
21
     * @return array
22
     */
23
    public static function stringToArray(string $string = null, string $valueDelimiter = ':', string $itemDelimiter = ',')
24
    {
25
        if(is_null($string)) {
26
            return [];
27
        }
28
29
        $values = [];
30
        $items = preg_split('/' . preg_quote($itemDelimiter) . '/', $string, -1, PREG_SPLIT_NO_EMPTY);
31
32
        foreach ($items as $index => $item) {
33
            $item = explode($valueDelimiter, $item);
34
35
            $key = array_get($item, 0);
36
            $value = array_get($item, 1);
37
38
            if(is_null($value)) {
39
                $value = $key;
40
                $key = $index;
41
            }
42
43
            $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

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