Completed
Push — master ( 0e6e64...9192b1 )
by Viacheslav
12s
created

PhpCode   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
eloc 68
dl 0
loc 125
ccs 60
cts 72
cp 0.8333
rs 10
c 0
b 0
f 0
wmc 28

9 Methods

Rating   Name   Duplication   Size   Complexity  
A toCamelCase() 0 10 3
A toString() 0 14 4
A addSnippet() 0 8 2
A makePhpName() 0 10 3
A makePhpConstantName() 0 16 4
A makePhpClassName() 0 7 2
A fromCamelCase() 0 14 5
A makePhpNamespaceName() 0 15 4
A varExport() 0 7 1
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 10
    public function addSnippet($code, $prepend = false)
12
    {
13 10
        if ($prepend) {
14
            array_unshift($this->snippets, $code);
15
        } else {
16 10
            $this->snippets[] = $code;
17
        }
18 10
        return $this;
19
    }
20
21 10
    protected function toString()
22
    {
23 10
        $result = '';
24 10
        if ($this->snippets === null) {
25 4
            return '';
26
        }
27 10
        foreach ($this->snippets as $code) {
28 10
            if ($code instanceof AbstractTemplate) {
29 10
                $result .= $code->render();
30
            } else {
31 10
                $result .= $code;
32
            }
33
        }
34 10
        return $result;
35
    }
36
37 10
    public static function toCamelCase($string, $lowerFirst = false)
38
    {
39 10
        $result = implode('', array_map('ucfirst', explode('_', $string)));
40 10
        if (!$result) {
41 4
            return '';
42
        }
43 10
        if ($lowerFirst) {
44 9
            $result[0] = strtolower($result[0]);
45
        }
46 10
        return $result;
47
    }
48
49 9
    public static function fromCamelCase($input)
50
    {
51 9
        preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
52 9
        $ret = $matches[0];
53 9
        $index = array();
54 9
        foreach ($ret as &$match) {
55 5
            $index[$match] = '_' . ($match == strtoupper($match) ? strtolower($match) : lcfirst($match));
56
        }
57 9
        krsort($index);
58 9
        $result = strtr($input, $index);
59 9
        if ($input[0] !== '_' && $result[0] === '_') {
60 4
            $result = substr($result, 1);
61
        }
62 9
        return preg_replace('/_+/', '_', $result);
63
    }
64
65
66 10
    public static function makePhpName($rawName, $lowerFirst = true)
67
    {
68 10
        $phpName = preg_replace("/([^a-zA-Z0-9_]+)/", "_", $rawName);
69 10
        $phpName = self::toCamelCase($phpName, $lowerFirst);
70 10
        if (!$phpName) {
71 4
            $phpName = 'property' . substr(md5($rawName), 0, 6);
72 10
        } elseif (is_numeric($phpName[0])) {
73 1
            $phpName = 'property' . $phpName;
74
        }
75 10
        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 10
    public static function makePhpClassName($rawName)
81
    {
82 10
        $result = self::makePhpName($rawName, false);
83 10
        if (in_array(strtolower($result), self::$keywords)) {
84 1
            $result .= 'Class';
85
        }
86 10
        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 8
    public static function makePhpConstantName($rawName)
107
    {
108 8
        $phpName = preg_replace("/([^a-zA-Z0-9_]+)/", "_", $rawName);
109 8
        $phpName = trim($phpName, '_');
110 8
        $phpName = self::fromCamelCase($phpName);
111
112 8
        if (in_array(strtolower($phpName), self::$keywords)) {
113 2
            $phpName = '_' . $phpName;
114
        }
115
116 8
        if (!$phpName) {
117
            $phpName = 'const_' . substr(md5($rawName), 0, 6);
118 8
        } elseif (is_numeric($phpName[0])) {
119 2
            $phpName = 'const_' . $phpName;
120
        }
121 8
        return strtoupper($phpName);
122
    }
123
124
125 6
    public static function varExport($value)
126
    {
127 6
        $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...
128 6
        $result = str_replace("array (\n", "array(\n", $result);
129 6
        $result = str_replace('  ', '    ', $result);
130 6
        $result = str_replace("array(\n)", "array()", $result);
131 6
        return $result;
132
    }
133
134
}