Helpers::decamelize()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.7666
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XL2TP;
6
7
class Helpers
8
{
9
    /**
10
     * Convert string like "makeMeHappy" to "make me happy"
11
     *
12
     * @param string $input
13
     *
14
     * @return string
15
     */
16 18
    public static function decamelize(string $input): string
17
    {
18
        // It's for global section
19 18
        if (in_array($input, ['listenAddr', 'listen-addr'])) {
20 7
            return 'listen-addr';
21
        }
22
23 17
        preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
24 17
        $result = $matches[0];
25 17
        foreach ($result as &$match) {
26 17
            $match = $match === strtoupper($match) ? strtolower($match) : lcfirst($match);
27
        }
28
29 17
        return implode(' ', $result);
30
    }
31
}
32