Completed
Pull Request — master (#139)
by Kévin
03:44
created

Inflector::convertToSnakeCase()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 3
nop 1
dl 0
loc 15
ccs 10
cts 10
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Kévin Gomez https://github.com/K-Phoen <[email protected]>
4
 */
5
6
namespace PHPSA\Analyzer\Helper;
7
8
class Inflector
9
{
10
    /**
11
     * Converts a CamelCase string to its snake_case equivalent.
12
     *
13
     * @param string $string
14
     * @return string
15
     */
16 1
    public static function convertToSnakeCase($string)
17
    {
18 1
        $result = '';
19 1
        $len = strlen($string);
20
21 1
        for ($i = 0; $i < $len; ++$i) {
22 1
            if ($i !== 0 && ctype_upper($string[$i])) {
23 1
                $result .= '_'.strtolower($string[$i]);
24 1
            } else {
25 1
                $result .= strtolower($string[$i]);
26
            }
27 1
        }
28
29 1
        return $result;
30
    }
31
}
32