Completed
Push — master ( d75c1f...eed677 )
by Viacheslav
12s
created

PhpCode   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 76.67%

Importance

Changes 0
Metric Value
dl 0
loc 109
ccs 46
cts 60
cp 0.7667
rs 10
c 0
b 0
f 0
wmc 25

8 Methods

Rating   Name   Duplication   Size   Complexity  
A toCamelCase() 0 10 3
A makePhpName() 0 10 3
A makePhpConstantName() 0 16 4
A makePhpClassName() 0 7 2
A fromCamelCase() 0 8 3
A makePhpNamespaceName() 0 15 4
A toString() 0 14 4
A addSnippet() 0 8 2
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 4
    public function addSnippet($code, $prepend = false)
12
    {
13 4
        if ($prepend) {
14
            array_unshift($this->snippets, $code);
15
        } else {
16 4
            $this->snippets[] = $code;
17
        }
18 4
        return $this;
19
    }
20
21 4
    protected function toString()
22
    {
23 4
        $result = '';
24 4
        if ($this->snippets === null) {
25
            return '';
26
        }
27 4
        foreach ($this->snippets as $code) {
28 4
            if ($code instanceof AbstractTemplate) {
29 4
                $result .= $code->render();
30
            } else {
31 4
                $result .= $code;
32
            }
33
        }
34 4
        return $result;
35
    }
36
37 4
    public static function toCamelCase($string, $lowerFirst = false)
38
    {
39 4
        $result = implode('', array_map('ucfirst', explode('_', $string)));
40 4
        if (!$result) {
41 3
            return '';
42
        }
43 4
        if ($lowerFirst) {
44 4
            $result[0] = strtolower($result[0]);
45
        }
46 4
        return $result;
47
    }
48
49 2
    public static function fromCamelCase($input)
50
    {
51 2
        preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
52 2
        $ret = $matches[0];
53 2
        foreach ($ret as &$match) {
54 2
            $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
55
        }
56 2
        return implode('_', $ret);
57
    }
58
59
60 4
    public static function makePhpName($rawName, $lowerFirst = true)
61
    {
62 4
        $phpName = preg_replace("/([^a-zA-Z0-9_]+)/", "_", $rawName);
63 4
        $phpName = self::toCamelCase($phpName, $lowerFirst);
64 4
        if (!$phpName) {
65 3
            $phpName = 'property' . substr(md5($rawName), 0, 6);
66 4
        } elseif (is_numeric($phpName[0])) {
67
            $phpName = 'property' . $phpName;
68
        }
69 4
        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 4
    public static function makePhpClassName($rawName)
75
    {
76 4
        $result = self::makePhpName($rawName, false);
77 4
        if (in_array(strtolower($result), self::$keywords)) {
78 1
            $result .= 'Class';
79
        }
80 4
        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 2
    public static function makePhpConstantName($rawName)
101
    {
102 2
        $phpName = preg_replace("/([^a-zA-Z0-9_]+)/", "_", $rawName);
103
104 2
        $phpName = self::fromCamelCase($phpName);
105
106 2
        if (in_array(strtolower($phpName), self::$keywords)) {
107 2
            $phpName = '_' . $phpName;
108
        }
109
110 2
        if (!$phpName) {
111 2
            $phpName = 'const_' . substr(md5($rawName), 0, 6);
112 2
        } elseif (is_numeric($phpName[0])) {
113
            $phpName = 'const_' . $phpName;
114
        }
115 2
        return strtoupper($phpName);
116
    }
117
118
}