Completed
Push — 4.9 ( f399a5...eb33e7 )
by Mikhail
01:38
created

to_lower_case()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
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
 * Converts 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
 * Converts strings like addonXml to addon-xml
43
 * @param string $string
44
 * 
45
 * @return string
46
 */
47
function to_lower_case(string $string): string
48
{
49
    return preg_replace_callback('/([A-Z]+)/', function($matches) {
50
        return '-' . strtolower($matches[1]);
51
    }, $string);
52
}
53
54
/**
55
 * Parse command line like arguments into array
56
 * 
57
 * @param string $command
58
 * 
59
 * @return array
60
 */
61
function arguments(string $command) {
62
    $arguments = [];
63
64
    preg_replace_callback(
65
        '/--([\w\.\-_]+)\s*("([^"\\\]*(\\\.[^"\\\]*)*)"|[\w\d][\w\d\.\-_]*)?/ius',
66
        function($matches) use (&$arguments) {
67
            $key = $matches[1];
68
            $value = true;
69
70
            switch(count($matches)) {
71
                case 4:
72
                case 5:
73
                    $value = $matches[3];
74
                break;
75
                case 3:
76
                    $value = $matches[2];
77
                break;
78
            }
79
            
80
            $arguments[$key] = $value;
81
        },
82
        // 'ccg.php addon/create --addon.id "new_addon" --langvar "say my name \"Daniel\"" --cur "" --developer mikhail ddfgd --test');
83
        $command
84
    );
85
86
    return $arguments;
87
}
88