Completed
Push — master ( 21de16...00bb12 )
by Viacheslav
02:37
created

PhpCode::makePhpName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 10
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Swaggest\PhpCodeBuilder;
4
5
use Swaggest\CodeBuilder\AbstractTemplate;
6
7
class PhpCode extends PhpTemplate
8
{
9
    public $snippets;
10
11 8
    public function addSnippet($code, $prepend = false)
12
    {
13 8
        if ($prepend) {
14
            array_unshift($this->snippets, $code);
15
        } else {
16 8
            $this->snippets[] = $code;
17
        }
18 8
        return $this;
19
    }
20
21 8
    protected function toString()
22
    {
23 8
        $result = '';
24 8
        if ($this->snippets === null) {
25 4
            return '';
26
        }
27 8
        foreach ($this->snippets as $code) {
28 8
            if ($code instanceof AbstractTemplate) {
29 8
                $result .= $code->render();
30
            } else {
31 8
                $result .= $code;
32
            }
33
        }
34 8
        return $result;
35
    }
36
37 8
    public static function toCamelCase($string, $lowerFirst = false)
38
    {
39 8
        $result = implode('', array_map('ucfirst', explode('_', $string)));
40 8
        if (!$result) {
41 3
            return '';
42
        }
43 8
        if ($lowerFirst) {
44 8
            $result[0] = strtolower($result[0]);
45
        }
46 8
        return $result;
47
    }
48
49 6
    public static function fromCamelCase($input)
50
    {
51 6
        preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
52 6
        $ret = $matches[0];
53 6
        foreach ($ret as &$match) {
54 2
            $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
55
        }
56 6
        return implode('_', $ret);
57
    }
58
59
60 8
    public static function makePhpName($rawName, $lowerFirst = true)
61
    {
62 8
        $phpName = preg_replace("/([^a-zA-Z0-9_]+)/", "_", $rawName);
63 8
        $phpName = self::toCamelCase($phpName, $lowerFirst);
64 8
        if (!$phpName) {
65 3
            $phpName = 'property' . substr(md5($rawName), 0, 6);
66 8
        } elseif (is_numeric($phpName[0])) {
67
            $phpName = 'property' . $phpName;
68
        }
69 8
        return $phpName;
70
    }
71
72
    private static $keywords = array('__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor');
73
74 8
    public static function makePhpClassName($rawName)
75
    {
76 8
        $result = self::makePhpName($rawName, false);
77 8
        if (in_array(strtolower($result), self::$keywords)) {
78 1
            $result .= 'Class';
79
        }
80 8
        return $result;
81
    }
82
83
    public static function makePhpNamespaceName(array $nsItems)
84
    {
85
        $result = array();
86
        foreach ($nsItems as $nsItem) {
87
            $nsItem = self::makePhpName($nsItem, false);
88
            if (!$nsItem) {
89
                continue;
90
            }
91
            if (in_array(strtolower($nsItem), self::$keywords)) {
92
                $nsItem .= 'Ns';
93
            }
94
            $result[] = $nsItem;
95
        }
96
97
        return '\\' . implode('\\', $result);
98
    }
99
100 6
    public static function makePhpConstantName($rawName)
101
    {
102 6
        $phpName = preg_replace("/([^a-zA-Z0-9_]+)/", "_", $rawName);
103
104 6
        $phpName = self::fromCamelCase($phpName);
105
106 6
        if (in_array(strtolower($phpName), self::$keywords)) {
107 2
            $phpName = '_' . $phpName;
108
        }
109
110 6
        if (!$phpName) {
111 6
            $phpName = 'const_' . substr(md5($rawName), 0, 6);
112 2
        } elseif (is_numeric($phpName[0])) {
113
            $phpName = 'const_' . $phpName;
114
        }
115 6
        return strtoupper($phpName);
116
    }
117
118
}