Completed
Push — master ( 51b71e...0f383e )
by Viacheslav
12s
created

PhpCode::makePhpConstantName()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 5
nop 1
dl 0
loc 20
ccs 11
cts 11
cp 1
crap 4
rs 9.9
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 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
        return $phpName;
76
    }
77
78
    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');
79
80 11
    public static function makePhpClassName($rawName)
81
    {
82 11
        $result = self::makePhpName($rawName, false);
83 11
        if (in_array(strtolower($result), self::$keywords)) {
84 2
            $result .= 'Class';
85
        }
86 11
        return $result;
87
    }
88
89
    public static function makePhpNamespaceName(array $nsItems)
90
    {
91
        $result = array();
92
        foreach ($nsItems as $nsItem) {
93
            $nsItem = self::makePhpName($nsItem, false);
94
            if (!$nsItem) {
95
                continue;
96
            }
97
            if (in_array(strtolower($nsItem), self::$keywords)) {
98
                $nsItem .= 'Ns';
99
            }
100
            $result[] = $nsItem;
101
        }
102
103
        return '\\' . implode('\\', $result);
104
    }
105
106 10
    public static function makePhpConstantName($rawName)
107
    {
108 10
        $phpName = preg_replace("/([^a-zA-Z0-9_]+)/", "_", $rawName);
109 10
        $phpName = trim($phpName, '_');
110
111 10
        if ($phpName) {
112 10
            $phpName = self::fromCamelCase($phpName);
113
114 10
            if (in_array(strtolower($phpName), self::$keywords)) {
115 3
                $phpName = '_' . $phpName;
116
            }
117
118 10
            if (is_numeric($phpName[0])) {
119 10
                $phpName = 'const_' . $phpName;
120
            }
121
        } else {
122 1
            $phpName = 'const_' . substr(md5($rawName), 0, 6);
123
        }
124
125 10
        return strtoupper($phpName);
126
    }
127
128
129 7
    public static function varExport($value)
130
    {
131 7
        $result = var_export($value, 1);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as var_export($value, 1) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
132 7
        $result = str_replace("stdClass::__set_state", "(object)", $result);
133 7
        $result = str_replace("array (\n", "array(\n", $result);
134 7
        $result = str_replace('  ', '    ', $result);
135 7
        $result = str_replace("array(\n)", "array()", $result);
136 7
        return $result;
137
    }
138
139
}
140