@@ -58,7 +58,8 @@ discard block |
||
| 58 | 58 | public function addElement(DeclarationInterface $element): NamespaceDeclaration |
| 59 | 59 | { |
| 60 | 60 | $this->elements->add($element); |
| 61 | - if ($element instanceof DependedInterface) { |
|
| 61 | + if ($element instanceof DependedInterface) |
|
| 62 | + { |
|
| 62 | 63 | $this->addUses($element->getDependencies()); |
| 63 | 64 | } |
| 64 | 65 | |
@@ -85,22 +86,26 @@ discard block |
||
| 85 | 86 | $result = ''; |
| 86 | 87 | $indentShift = 0; |
| 87 | 88 | |
| 88 | - if (!$this->docComment->isEmpty()) { |
|
| 89 | + if (!$this->docComment->isEmpty()) |
|
| 90 | + { |
|
| 89 | 91 | $result .= $this->docComment->render($indentLevel) . "\n"; |
| 90 | 92 | } |
| 91 | 93 | |
| 92 | - if (!empty($this->getName())) { |
|
| 94 | + if (!empty($this->getName())) |
|
| 95 | + { |
|
| 93 | 96 | $result .= $this->addIndent("namespace {$this->getName()} {", $indentLevel) . "\n"; |
| 94 | 97 | $indentShift = 1; |
| 95 | 98 | } |
| 96 | 99 | |
| 97 | - if (!empty($this->uses)) { |
|
| 100 | + if (!empty($this->uses)) |
|
| 101 | + { |
|
| 98 | 102 | $result .= $this->renderUses($indentLevel + $indentShift) . "\n\n"; |
| 99 | 103 | } |
| 100 | 104 | |
| 101 | 105 | $result .= $this->elements->render($indentLevel + $indentShift); |
| 102 | 106 | |
| 103 | - if (!empty($this->getName())) { |
|
| 107 | + if (!empty($this->getName())) |
|
| 108 | + { |
|
| 104 | 109 | $result .= "\n" . $this->addIndent('}', $indentLevel); |
| 105 | 110 | } |
| 106 | 111 | |
@@ -106,7 +106,8 @@ discard block |
||
| 106 | 106 | public function addElement(DeclarationInterface $element): FileDeclaration |
| 107 | 107 | { |
| 108 | 108 | $this->elements->add($element); |
| 109 | - if ($element instanceof DependedInterface) { |
|
| 109 | + if ($element instanceof DependedInterface) |
|
| 110 | + { |
|
| 110 | 111 | $this->addUses($element->getDependencies()); |
| 111 | 112 | } |
| 112 | 113 | |
@@ -132,22 +133,27 @@ discard block |
||
| 132 | 133 | { |
| 133 | 134 | $result = "<?php\n"; |
| 134 | 135 | |
| 135 | - if (!$this->docComment->isEmpty()) { |
|
| 136 | + if (!$this->docComment->isEmpty()) |
|
| 137 | + { |
|
| 136 | 138 | $result .= $this->docComment->render($indentLevel) . "\n"; |
| 137 | 139 | } |
| 138 | 140 | |
| 139 | - if ($this->directives !== null && !empty($this->directives->render())) { |
|
| 141 | + if ($this->directives !== null && !empty($this->directives->render())) |
|
| 142 | + { |
|
| 140 | 143 | $result .= $this->directives->render() . "\n\n"; |
| 141 | 144 | } |
| 142 | 145 | |
| 143 | - if (!empty($this->namespace)) { |
|
| 144 | - if ($this->docComment->isEmpty()) { |
|
| 146 | + if (!empty($this->namespace)) |
|
| 147 | + { |
|
| 148 | + if ($this->docComment->isEmpty()) |
|
| 149 | + { |
|
| 145 | 150 | $result .= "\n"; |
| 146 | 151 | } |
| 147 | 152 | $result .= "namespace {$this->namespace};\n\n"; |
| 148 | 153 | } |
| 149 | 154 | |
| 150 | - if (!empty($this->uses)) { |
|
| 155 | + if (!empty($this->uses)) |
|
| 156 | + { |
|
| 151 | 157 | $result .= $this->renderUses($indentLevel) . "\n\n"; |
| 152 | 158 | } |
| 153 | 159 | |
@@ -64,7 +64,8 @@ discard block |
||
| 64 | 64 | ) { |
| 65 | 65 | $this->setName($name); |
| 66 | 66 | |
| 67 | - if (! empty($extends)) { |
|
| 67 | + if (! empty($extends)) |
|
| 68 | + { |
|
| 68 | 69 | $this->setExtends($extends); |
| 69 | 70 | } |
| 70 | 71 | |
@@ -157,7 +158,8 @@ discard block |
||
| 157 | 158 | public function setInterfaces(array $interfaces): ClassDeclaration |
| 158 | 159 | { |
| 159 | 160 | $this->interfaces = []; |
| 160 | - foreach ($interfaces as $interface) { |
|
| 161 | + foreach ($interfaces as $interface) |
|
| 162 | + { |
|
| 161 | 163 | $this->addInterface($interface); |
| 162 | 164 | } |
| 163 | 165 | |
@@ -203,7 +205,8 @@ discard block |
||
| 203 | 205 | public function setTraits(array $traits): ClassDeclaration |
| 204 | 206 | { |
| 205 | 207 | $this->traits = []; |
| 206 | - foreach ($traits as $trait) { |
|
| 208 | + foreach ($traits as $trait) |
|
| 209 | + { |
|
| 207 | 210 | $this->addTrait($trait); |
| 208 | 211 | } |
| 209 | 212 | |
@@ -293,7 +296,8 @@ discard block |
||
| 293 | 296 | { |
| 294 | 297 | $result = ''; |
| 295 | 298 | |
| 296 | - if (! $this->docComment->isEmpty()) { |
|
| 299 | + if (! $this->docComment->isEmpty()) |
|
| 300 | + { |
|
| 297 | 301 | $result .= $this->docComment->render($indentLevel) . "\n"; |
| 298 | 302 | } |
| 299 | 303 | |
@@ -301,11 +305,13 @@ discard block |
||
| 301 | 305 | $header = "class {$this->getName()}"; |
| 302 | 306 | |
| 303 | 307 | //Rendering extends |
| 304 | - if (! empty($this->extends)) { |
|
| 308 | + if (! empty($this->extends)) |
|
| 309 | + { |
|
| 305 | 310 | $header .= " extends {$this->extends}"; |
| 306 | 311 | } |
| 307 | 312 | |
| 308 | - if (! empty($this->interfaces)) { |
|
| 313 | + if (! empty($this->interfaces)) |
|
| 314 | + { |
|
| 309 | 315 | $interfaces = implode(', ', array_keys($this->interfaces)); |
| 310 | 316 | $header .= " implements {$interfaces}"; |
| 311 | 317 | } |
@@ -329,19 +335,23 @@ discard block |
||
| 329 | 335 | protected function renderBody(int $indentLevel): string |
| 330 | 336 | { |
| 331 | 337 | $result = ''; |
| 332 | - if (! empty($this->traits)) { |
|
| 338 | + if (! empty($this->traits)) |
|
| 339 | + { |
|
| 333 | 340 | $result .= $this->renderTraits($indentLevel + 1) . "\n\n"; |
| 334 | 341 | } |
| 335 | 342 | |
| 336 | - if (! $this->constants->isEmpty()) { |
|
| 343 | + if (! $this->constants->isEmpty()) |
|
| 344 | + { |
|
| 337 | 345 | $result .= $this->constants->render($indentLevel + 1) . "\n\n"; |
| 338 | 346 | } |
| 339 | 347 | |
| 340 | - if (! $this->properties->isEmpty()) { |
|
| 348 | + if (! $this->properties->isEmpty()) |
|
| 349 | + { |
|
| 341 | 350 | $result .= $this->properties->render($indentLevel + 1) . "\n\n"; |
| 342 | 351 | } |
| 343 | 352 | |
| 344 | - if (! $this->methods->isEmpty()) { |
|
| 353 | + if (! $this->methods->isEmpty()) |
|
| 354 | + { |
|
| 345 | 355 | $result .= $this->methods->render($indentLevel + 1) . "\n\n"; |
| 346 | 356 | } |
| 347 | 357 | |
@@ -355,7 +365,8 @@ discard block |
||
| 355 | 365 | private function renderTraits(int $indentLevel = 0): string |
| 356 | 366 | { |
| 357 | 367 | $lines = []; |
| 358 | - foreach ($this->traits as $class => $options) { |
|
| 368 | + foreach ($this->traits as $class => $options) |
|
| 369 | + { |
|
| 359 | 370 | $lines[] = $this->addIndent("use {$class};", $indentLevel); |
| 360 | 371 | } |
| 361 | 372 | |
@@ -42,10 +42,14 @@ discard block |
||
| 42 | 42 | */ |
| 43 | 43 | public function setComment($comment): self |
| 44 | 44 | { |
| 45 | - if (!empty($comment)) { |
|
| 46 | - if (is_array($comment)) { |
|
| 45 | + if (!empty($comment)) |
|
| 46 | + { |
|
| 47 | + if (is_array($comment)) |
|
| 48 | + { |
|
| 47 | 49 | $this->docComment->setLines($comment); |
| 48 | - } elseif (is_string($comment)) { |
|
| 50 | + } |
|
| 51 | + elseif (is_string($comment)) |
|
| 52 | + { |
|
| 49 | 53 | $this->docComment->setString($comment); |
| 50 | 54 | } |
| 51 | 55 | } |
@@ -60,7 +64,8 @@ discard block |
||
| 60 | 64 | */ |
| 61 | 65 | private function initComment($comment): void |
| 62 | 66 | { |
| 63 | - if (empty($this->docComment)) { |
|
| 67 | + if (empty($this->docComment)) |
|
| 68 | + { |
|
| 64 | 69 | $this->docComment = new Comment(); |
| 65 | 70 | } |
| 66 | 71 | |
@@ -56,7 +56,8 @@ discard block |
||
| 56 | 56 | */ |
| 57 | 57 | public function addUses(array $uses): self |
| 58 | 58 | { |
| 59 | - foreach ($uses as $class => $alias) { |
|
| 59 | + foreach ($uses as $class => $alias) |
|
| 60 | + { |
|
| 60 | 61 | $this->addUse($class, $alias); |
| 61 | 62 | } |
| 62 | 63 | |
@@ -112,12 +113,16 @@ discard block |
||
| 112 | 113 | private function renderUses(int $indentLevel = 0): string |
| 113 | 114 | { |
| 114 | 115 | $lines = []; |
| 115 | - foreach ($this->getUses() as $class => $alias) { |
|
| 116 | + foreach ($this->getUses() as $class => $alias) |
|
| 117 | + { |
|
| 116 | 118 | $line = "use {$class}"; |
| 117 | 119 | |
| 118 | - if (!empty($alias)) { |
|
| 120 | + if (!empty($alias)) |
|
| 121 | + { |
|
| 119 | 122 | $line .= " as {$alias};"; |
| 120 | - } else { |
|
| 123 | + } |
|
| 124 | + else |
|
| 125 | + { |
|
| 121 | 126 | $line .= ';'; |
| 122 | 127 | } |
| 123 | 128 | |
@@ -35,7 +35,8 @@ discard block |
||
| 35 | 35 | */ |
| 36 | 36 | public function serialize($value): string |
| 37 | 37 | { |
| 38 | - if (is_array($value)) { |
|
| 38 | + if (is_array($value)) |
|
| 39 | + { |
|
| 39 | 40 | return $this->packArray($value); |
| 40 | 41 | } |
| 41 | 42 | |
@@ -50,7 +51,8 @@ discard block |
||
| 50 | 51 | */ |
| 51 | 52 | protected function packArray(array $array, int $level = 0): string |
| 52 | 53 | { |
| 53 | - if ($array === []) { |
|
| 54 | + if ($array === []) |
|
| 55 | + { |
|
| 54 | 56 | return '[]'; |
| 55 | 57 | } |
| 56 | 58 | //Delimiters between rows and sub-arrays. |
@@ -60,15 +62,19 @@ discard block |
||
| 60 | 62 | $associated = array_diff_key($array, array_keys(array_keys($array))); |
| 61 | 63 | $result = []; |
| 62 | 64 | $innerIndent = 0; |
| 63 | - if (!empty($associated)) { |
|
| 64 | - foreach ($array as $key => $value) { |
|
| 65 | + if (!empty($associated)) |
|
| 66 | + { |
|
| 67 | + foreach ($array as $key => $value) |
|
| 68 | + { |
|
| 65 | 69 | //Based on biggest key length |
| 66 | 70 | $innerIndent = max(strlen(var_export($key, true)), $innerIndent); |
| 67 | 71 | } |
| 68 | 72 | } |
| 69 | - foreach ($array as $key => $value) { |
|
| 73 | + foreach ($array as $key => $value) |
|
| 74 | + { |
|
| 70 | 75 | $prefix = ''; |
| 71 | - if (!empty($associated)) { |
|
| 76 | + if (!empty($associated)) |
|
| 77 | + { |
|
| 72 | 78 | //Key prefix |
| 73 | 79 | $prefix = str_pad( |
| 74 | 80 | var_export($key, true), |
@@ -77,18 +83,21 @@ discard block |
||
| 77 | 83 | STR_PAD_RIGHT |
| 78 | 84 | ) . ' => '; |
| 79 | 85 | } |
| 80 | - if (!is_array($value)) { |
|
| 86 | + if (!is_array($value)) |
|
| 87 | + { |
|
| 81 | 88 | $result[] = $prefix . $this->packValue($value); |
| 82 | 89 | continue; |
| 83 | 90 | } |
| 84 | - if ($value === []) { |
|
| 91 | + if ($value === []) |
|
| 92 | + { |
|
| 85 | 93 | $result[] = $prefix . '[]'; |
| 86 | 94 | continue; |
| 87 | 95 | } |
| 88 | 96 | $subArray = $this->packArray($value, $level + 1); |
| 89 | 97 | $result[] = $prefix . "[{$subIndent}" . $subArray . "{$keyIndent}]"; |
| 90 | 98 | } |
| 91 | - if ($level !== 0) { |
|
| 99 | + if ($level !== 0) |
|
| 100 | + { |
|
| 92 | 101 | return $result ? implode(",{$keyIndent}", $result) : ''; |
| 93 | 102 | } |
| 94 | 103 | |
@@ -105,30 +114,37 @@ discard block |
||
| 105 | 114 | */ |
| 106 | 115 | protected function packValue($value): string |
| 107 | 116 | { |
| 108 | - if ($value instanceof DeclarationInterface) { |
|
| 117 | + if ($value instanceof DeclarationInterface) |
|
| 118 | + { |
|
| 109 | 119 | //No indentation here |
| 110 | 120 | return $value->render(); |
| 111 | 121 | } |
| 112 | 122 | |
| 113 | - if ($value === null) { |
|
| 123 | + if ($value === null) |
|
| 124 | + { |
|
| 114 | 125 | return 'null'; |
| 115 | 126 | } |
| 116 | 127 | |
| 117 | - if (is_bool($value)) { |
|
| 128 | + if (is_bool($value)) |
|
| 129 | + { |
|
| 118 | 130 | return ($value ? 'true' : 'false'); |
| 119 | 131 | } |
| 120 | 132 | |
| 121 | - if (is_object($value) && method_exists($value, '__set_state')) { |
|
| 133 | + if (is_object($value) && method_exists($value, '__set_state')) |
|
| 134 | + { |
|
| 122 | 135 | return '\\' . var_export($value, true); |
| 123 | 136 | } |
| 124 | 137 | |
| 125 | - if (!is_string($value) && !is_numeric($value)) { |
|
| 138 | + if (!is_string($value) && !is_numeric($value)) |
|
| 139 | + { |
|
| 126 | 140 | throw new SerializeException('Unable to pack non scalar value'); |
| 127 | 141 | } |
| 128 | 142 | |
| 129 | - if (is_string($value) && class_exists($value)) { |
|
| 143 | + if (is_string($value) && class_exists($value)) |
|
| 144 | + { |
|
| 130 | 145 | $reflection = new ReflectionClass($value); |
| 131 | - if ($value === $reflection->getName()) { |
|
| 146 | + if ($value === $reflection->getName()) |
|
| 147 | + { |
|
| 132 | 148 | return '\\' . $reflection->getName() . '::class'; |
| 133 | 149 | } |
| 134 | 150 | } |
@@ -38,12 +38,14 @@ discard block |
||
| 38 | 38 | */ |
| 39 | 39 | public function render(int $indentLevel = 0): string |
| 40 | 40 | { |
| 41 | - if ($this->isEmpty()) { |
|
| 41 | + if ($this->isEmpty()) |
|
| 42 | + { |
|
| 42 | 43 | return ''; |
| 43 | 44 | } |
| 44 | 45 | |
| 45 | 46 | $result = $this->addIndent("/**\n", $indentLevel); |
| 46 | - foreach ($this->getLines() as $line) { |
|
| 47 | + foreach ($this->getLines() as $line) |
|
| 48 | + { |
|
| 47 | 49 | $result .= $this->addIndent(" * {$line}\n", $indentLevel); |
| 48 | 50 | } |
| 49 | 51 | |
@@ -58,7 +60,8 @@ discard block |
||
| 58 | 60 | protected function prepareLine(string $line): ?string |
| 59 | 61 | { |
| 60 | 62 | $line = trim($line); |
| 61 | - if (in_array($line, ['/*', '/**', '*/'], true)) { |
|
| 63 | + if (in_array($line, ['/*', '/**', '*/'], true)) |
|
| 64 | + { |
|
| 62 | 65 | return null; |
| 63 | 66 | } |
| 64 | 67 | |
@@ -89,14 +89,16 @@ discard block |
||
| 89 | 89 | public function render(int $indentLevel = 0): string |
| 90 | 90 | { |
| 91 | 91 | $result = ''; |
| 92 | - if (!$this->docComment->isEmpty()) { |
|
| 92 | + if (!$this->docComment->isEmpty()) |
|
| 93 | + { |
|
| 93 | 94 | $result .= $this->docComment->render($indentLevel) . "\n"; |
| 94 | 95 | } |
| 95 | 96 | |
| 96 | 97 | $result .= $this->addIndent("{$this->access} const {$this->getName()} = ", $indentLevel); |
| 97 | 98 | |
| 98 | 99 | $value = $this->getSerializer()->serialize($this->value); |
| 99 | - if (is_array($this->value)) { |
|
| 100 | + if (is_array($this->value)) |
|
| 101 | + { |
|
| 100 | 102 | $value = $this->mountIndents($value, $indentLevel); |
| 101 | 103 | } |
| 102 | 104 | |
@@ -113,7 +115,8 @@ discard block |
||
| 113 | 115 | private function mountIndents(string $serialized, int $indentLevel): string |
| 114 | 116 | { |
| 115 | 117 | $lines = explode("\n", $serialized); |
| 116 | - foreach ($lines as &$line) { |
|
| 118 | + foreach ($lines as &$line) |
|
| 119 | + { |
|
| 117 | 120 | $line = $this->addIndent($line, $indentLevel); |
| 118 | 121 | unset($line); |
| 119 | 122 | } |
@@ -148,13 +148,15 @@ |
||
| 148 | 148 | public function render(int $indentLevel = 0): string |
| 149 | 149 | { |
| 150 | 150 | $type = ''; |
| 151 | - if (!empty($this->type)) { |
|
| 151 | + if (!empty($this->type)) |
|
| 152 | + { |
|
| 152 | 153 | $type = $this->type . ' '; |
| 153 | 154 | } |
| 154 | 155 | |
| 155 | 156 | $result = $type . ($this->pdb ? '&' : '') . '$' . $this->getName(); |
| 156 | 157 | |
| 157 | - if (!$this->isOptional) { |
|
| 158 | + if (!$this->isOptional) |
|
| 159 | + { |
|
| 158 | 160 | return $result; |
| 159 | 161 | } |
| 160 | 162 | |