Completed
Push — master ( 9abbc7...a687e5 )
by Fabrizio
03:03
created

ArtisanOptionsParser::parse()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 39
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 39
rs 8.439
cc 5
eloc 16
nc 6
nop 1
1
<?php
2
3
namespace Fenos\Notifynder\Parsers;
4
5
/**
6
 * Class ArtisanOptionsParser.
7
 */
8
class ArtisanOptionsParser
9
{
10
    /**
11
     * Parse a string of fields, like
12
     * name, name2, name3.
13
     *
14
     * @param  string $fields
15
     * @return array
16
     */
17
    public function parse($fields)
18
    {
19
        if (! $fields) {
20
            return [];
21
        }
22
23
        // name:string, age:integer
24
        // name:string(10,2), age:integer
25
        $fields = preg_split('/\s?,\s/', $fields);
26
27
        $parsed = [];
28
29
        foreach ($fields as $index => $field) {
30
            // Example:
31
            // name:string:nullable => ['name', 'string', 'nullable']
32
            // name:string(15):nullable
33
            $chunks = preg_split('/\s?:\s?/', $field, null);
34
35
            // The first item will be our property
36
            $property = array_shift($chunks);
37
38
            $args = null;
39
40
            // Finally, anything that remains will
41
            // be our decorators
42
            $decorators = $chunks;
43
44
            $parsed[$index] = $property;
45
46
            if (isset($args)) {
47
                $parsed[$index]['args'] = $args;
48
            }
49
            if ($decorators) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $decorators of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
50
                $parsed[$index]['decorators'] = $decorators;
51
            }
52
        }
53
54
        return $parsed;
55
    }
56
}
57