ConsoleHelper   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 60.87%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 64
ccs 14
cts 23
cp 0.6087
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A stringToArray() 0 24 4
A arrayToString() 0 15 4
1
<?php
2
3
namespace Slides\Saml2\Helpers;
4
5
use Illuminate\Support\Arr;
6
7
/**
8
 * Class ConsoleHelper
9
 *
10
 * @package App\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 1
    public static function stringToArray(?string $string = null, string $valueDelimiter = ':', string $itemDelimiter = ',')
26
    {
27 1
        if(is_null($string)) {
28 1
            return [];
29
        }
30
31 1
        $values = [];
32 1
        $items = preg_split('/' . preg_quote($itemDelimiter) . '/', $string, -1, PREG_SPLIT_NO_EMPTY);
33
34 1
        foreach ($items as $index => $item) {
35 1
            $item = explode($valueDelimiter, $item);
36
37 1
            $key = Arr::get($item, 0);
38 1
            $value = Arr::get($item, 1);
39
40 1
            if(is_null($value)) {
41 1
                $value = $key;
42 1
                $key = $index;
43
            }
44
45 1
            $values[trim($key)] = trim($value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null and string[]; however, parameter $string 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 1
        return $values;
49
    }
50
51
    /**
52
     * Converts an array to string
53
     *
54
     * ['one', 'two', 'three'] to 'one, two, three',
55
     * ['one' => 1, 'two' => 2, 'three' => 3] to 'one:1,two:2,three:3'
56
     *
57
     * @param array $array
58
     *
59
     * @return string
60
     */
61
    public static function arrayToString(array $array): string
62
    {
63
        $values = [];
64
65
        foreach ($array as $key => $value) {
66
            if(is_array($value)) {
67
                continue;
68
            }
69
70
            $values[] = is_string($key)
71
                ? $key . ':' . $value
72
                : $value;
73
        }
74
75
        return implode(',', $values);
76
    }
77
}