Completed
Push — master ( 0feaba...3f0447 )
by Viacheslav
10:58 queued 01:04
created

PhpCode::varExport()   B

Complexity

Conditions 9
Paths 15

Size

Total Lines 45
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 29
c 1
b 0
f 0
nc 15
nop 1
dl 0
loc 45
ccs 3
cts 3
cp 1
crap 9
rs 8.0555
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 11
    public function addSnippet($code, $prepend = false)
12
    {
13 11
        if ($prepend) {
14
            array_unshift($this->snippets, $code);
15
        } else {
16 11
            $this->snippets[] = $code;
17
        }
18 11
        return $this;
19
    }
20
21 11
    protected function toString()
22
    {
23 11
        $result = '';
24 11
        if ($this->snippets === null) {
25 5
            return '';
26
        }
27 11
        foreach ($this->snippets as $code) {
28 11
            if ($code instanceof AbstractTemplate) {
29 11
                $result .= $code->render();
30
            } else {
31 11
                $result .= $code;
32
            }
33
        }
34 11
        return $result;
35
    }
36
37 11
    public static function toCamelCase($string, $lowerFirst = false)
38
    {
39 11
        $result = implode('', array_map('ucfirst', explode('_', $string)));
40 11
        if (!$result) {
41 5
            return '';
42
        }
43 11
        if ($lowerFirst) {
44 11
            $result[0] = strtolower($result[0]);
45
        }
46 11
        return $result;
47
    }
48
49 11
    public static function fromCamelCase($input)
50
    {
51 11
        preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
52 11
        $ret = $matches[0];
53 11
        $index = array();
54 11
        foreach ($ret as &$match) {
55 7
            $index[$match] = '_' . ($match == strtoupper($match) ? strtolower($match) : lcfirst($match));
56
        }
57 11
        krsort($index);
58 11
        $result = strtr($input, $index);
59 11
        if ($input[0] !== '_' && $result[0] === '_') {
60 6
            $result = substr($result, 1);
61
        }
62 11
        return preg_replace('/_+/', '_', $result);
63
    }
64
65
66 11
    public static function makePhpName($rawName, $lowerFirst = true)
67
    {
68 11
        $phpName = preg_replace("/([^a-zA-Z0-9_]+)/", "_", $rawName);
69 11
        $phpName = self::toCamelCase($phpName, $lowerFirst);
70 11
        if (!$phpName) {
71 5
            $phpName = 'Property' . substr(md5($rawName), 0, 6);
72 11
        } elseif (is_numeric($phpName[0])) {
73 1
            $phpName = 'Property' . $phpName;
74
        }
75 11
        if ($lowerFirst) {
76
            $phpName[0] = strtolower($phpName[0]);
77
        }
78
        return $phpName;
79
    }
80 11
81
    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');
82 11
83 11
    public static function makePhpClassName($rawName)
84 2
    {
85
        $result = self::makePhpName($rawName, false);
86 11
        if (in_array(strtolower($result), self::$keywords)) {
87
            $result .= 'Class';
88
        }
89
        return $result;
90
    }
91
92
    public static function makePhpNamespaceName(array $nsItems)
93
    {
94
        $result = array();
95
        foreach ($nsItems as $nsItem) {
96
            $nsItem = self::makePhpName($nsItem, false);
97
            if (!$nsItem) {
98
                continue;
99
            }
100
            if (in_array(strtolower($nsItem), self::$keywords)) {
101
                $nsItem .= 'Ns';
102
            }
103
            $result[] = $nsItem;
104
        }
105
106 10
        return '\\' . implode('\\', $result);
107
    }
108 10
109 10
    public static function makePhpConstantName($rawName)
110
    {
111 10
        $phpName = preg_replace("/([^a-zA-Z0-9_]+)/", "_", $rawName);
112 10
        if (is_string($phpName)) {
113
            $phpName = trim($phpName, '_');
114 10
        }
115 3
116
        if ($phpName) {
117
            $phpName = self::fromCamelCase($phpName);
118 10
119 10
            if (in_array(strtolower($phpName), self::$keywords)) {
120
                $phpName = '_' . $phpName;
121
            }
122 1
123
            if (is_numeric($phpName[0])) {
124
                $phpName = 'const_' . $phpName;
125 10
            }
126
        } else {
127
            $phpName = 'const_' . substr(md5($rawName), 0, 6);
128
        }
129 7
130
        return strtoupper($phpName);
131 7
    }
132 7
133 7
134 7
    public static function varExport($value)
135 7
    {
136 7
        /** @var string $result */
137
        $result = '';
138
139
        if (is_array($value)) {
140
            if (count($value) === 0) {
141
                return '[]';
142
            }
143
144
            $i = 0;
145
            $sequential = true;
146
            foreach ($value as $k => $v) {
147
                if ($k !== $i) {
148
                    $sequential = false;
149
                    break;
150
                }
151
                ++$i;
152
            }
153
            $t = new self();
154
            $result .= "[\n";
155
            if ($sequential) {
156
                foreach ($value as $item) {
157
                    $result .= $t->padLines('    ', self::varExport($item), false) . ",\n";
158
                }
159
            } else {
160
                foreach ($value as $key => $item) {
161
                    $result .= $t->padLines('    ', self::varExport($key) . ' => ' . self::varExport($item), false) . ",\n";
162
                }
163
            }
164
            $result .= "]";
165
            return $result;
166
        }
167
168
        if ($value instanceof \stdClass) {
169
            return '(object)' . self::varExport((array)$value);
170
        }
171
172
173
        $result = var_export($value, true);
174
        $result = str_replace("stdClass::__set_state", "(object)", $result);
175
        $result = str_replace("array (\n", "array(\n", $result);
176
        $result = str_replace('  ', '    ', $result);
177
        $result = str_replace("array(\n)", "array()", $result);
178
        return $result;
179
    }
180
181
}
182