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

Inflector   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A convertToSnakeCase() 0 15 4
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