Passed
Push — 4.9 ( 8f0f3a...f6adb4 )
by Mikhail
01:57
created

arguments()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 26
rs 9.7
c 1
b 0
f 0
cc 4
nc 1
nop 1
1
<?php
2
3
/**
4
 * Explode string by lines
5
 * @param string $multiline_string - string to be exploded
6
 *
7
 * @return array - array of lines
8
 */
9
function explode_by_new_line(string $multiline_string): array
10
{
11
    return preg_split("/\r\n|\n|\r/", $multiline_string);
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_split('/ | | /', $multiline_string) could return the type false which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
12
}
13
14
/**
15
 * Parses srings like main_settings_section to Main settings section
16
 */
17
function parse_to_readable(string $source): string
18
{
19
    return ucfirst(
20
        preg_replace(
21
            '/_+/',
22
            ' ',
23
            mb_strtolower($source)
24
        )
25
    );
26
}
27
28
/**
29
 * Parses strings like addon-xml to addonXml
30
 * @param string $string 
31
 * 
32
 * @return string
33
 */
34
function to_camel_case(string $string): string
35
{
36
    return preg_replace_callback('/(-(\w+))/', function($matches) {
37
        return ucfirst($matches[2]);
38
    }, $string);
39
}
40
41
/**
42
 * Parse command line like arguments into array
43
 * 
44
 * @param string $command
45
 * 
46
 * @return array
47
 */
48
function arguments(string $command) {
49
    $arguments = [];
50
51
    preg_replace_callback(
52
        '/--([\w\.\-_]+)\s*("([^"\\\]*(\\\.[^"\\\]*)*)"|[\w\d\.]+)?/ius',
53
        function($matches) use (&$arguments) {
54
            $key = $matches[1];
55
            $value = true;
56
57
            switch(count($matches)) {
58
                case 4:
59
                case 5:
60
                    $value = $matches[3];
61
                break;
62
                case 3:
63
                    $value = $matches[2];
64
                break;
65
            }
66
            
67
            $arguments[$key] = $value;
68
        },
69
        // 'ccg.php addon/create --addon.id "new_addon" --langvar "say my name \"Daniel\"" --cur "" --developer mikhail ddfgd --test');
70
        $command
71
    );
72
73
    return $arguments;
74
}
75