| Total Complexity | 25 |
| Total Lines | 109 |
| Duplicated Lines | 0 % |
| Coverage | 78.33% |
| Changes | 0 | ||
| 1 | <?php |
||
| 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) |
|
| 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) |
|
| 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) |
|
| 116 | } |
||
| 117 | |||
| 118 | } |