Passed
Push — master ( 5d6601...391c2f )
by Gabor
04:19
created

CamelCaseToUnderScoreTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 20
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A camelCaseToUnderscore() 0 11 3
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Traits;
15
16
/**
17
 * Class CamelCaseToUnderScoreTrait.
18
 */
19
trait CamelCaseToUnderScoreTrait
20
{
21
    /**
22
     * Converts CamelCase text to under_score equivalent.
23
     *
24
     * @param string $input
25
     * @return string
26
     */
27 1
    protected function camelCaseToUnderscore(string $input) : string
28
    {
29 1
        preg_match_all('/([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)/', $input, $matches);
30 1
        $output = $matches[0];
31
32 1
        foreach ($output as &$match) {
33 1
            $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
34
        }
35
36 1
        return implode('_', $output);
37
    }
38
}
39