NameNormalizer::camelCaseToSnakeCase()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
4
namespace Sysbot\Telegram\Common;
5
6
7
/**
8
 * Class NameNormalizer
9
 * @package Sysbot\Telegram\Common
10
 */
11
class NameNormalizer
12
{
13
14
    /**
15
     * @param string $input
16
     * @return string
17
     */
18
    public static function camelCaseToSnakeCase(string $input): string
19
    {
20
        preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
21
        $ret = $matches[0];
22
        foreach ($ret as &$match) {
23
            $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
24
        }
25
        return implode('_', $ret);
26
    }
27
28
}