1 | <?php declare(strict_types=1); |
||
13 | abstract class AbstractGenerator |
||
14 | { |
||
15 | /** @var AbstractGenerator Parent class generator */ |
||
16 | protected $parent; |
||
17 | |||
18 | /** @var array Generated code grouped by generator class name */ |
||
19 | protected $generatedCode = []; |
||
20 | |||
21 | /** @var int Indentation level */ |
||
22 | protected $indentation = 0; |
||
23 | |||
24 | /** @var bool Flag that class has already had generated its code */ |
||
25 | protected $isGenerated = false; |
||
26 | |||
27 | /** |
||
28 | * AbstractGenerator constructor. |
||
29 | * |
||
30 | * @param AbstractGenerator $parent Parent generator |
||
31 | */ |
||
32 | 50 | public function __construct(AbstractGenerator $parent = null) |
|
36 | |||
37 | /** |
||
38 | * Close current generator and return parent. |
||
39 | * |
||
40 | * @return AbstractGenerator|ClassGenerator|FunctionGenerator|MethodGenerator|PropertyGenerator|ClassConstantGenerator|ConditionGenerator|IfGenerator |
||
41 | */ |
||
42 | 9 | public function end() : AbstractGenerator |
|
64 | |||
65 | /** |
||
66 | * Generate code. |
||
67 | * |
||
68 | * @return string Generated code |
||
69 | */ |
||
70 | abstract public function code(): string; |
||
71 | |||
72 | /** |
||
73 | * Set Comments block. |
||
74 | * |
||
75 | * @return CommentsGenerator Comments block generator |
||
76 | */ |
||
77 | 1 | public function defComment() : CommentsGenerator |
|
81 | |||
82 | /** |
||
83 | * Decrease indentation. |
||
84 | * |
||
85 | * @param int $indentation |
||
86 | * |
||
87 | * @return $this|AbstractGenerator|ClassGenerator |
||
88 | */ |
||
89 | 21 | public function setIndentation(int $indentation): AbstractGenerator |
|
95 | |||
96 | /** |
||
97 | * Build nested class code array. |
||
98 | * |
||
99 | * @param string $className Nested class name |
||
100 | * @param array $formattedCode Collection of code |
||
101 | * |
||
102 | * @return array Collection of code with added nested class code |
||
103 | */ |
||
104 | 20 | protected function buildNestedCode(string $className, array $formattedCode = []): array |
|
113 | |||
114 | /** |
||
115 | * Get generated nested code. |
||
116 | * |
||
117 | * @param string $className Nested class name |
||
118 | * |
||
119 | * @return string Generated nested code or empty string |
||
120 | */ |
||
121 | 29 | protected function getNestedCode(string $className): string |
|
129 | |||
130 | /** |
||
131 | * Generate correct value. |
||
132 | * |
||
133 | * Method handles arrays, numerics, strings and constants. |
||
134 | * |
||
135 | * @param mixed $value Value |
||
136 | * |
||
137 | * @return mixed Value |
||
138 | */ |
||
139 | 4 | protected function parseValue($value) |
|
157 | |||
158 | /** |
||
159 | * Get array values definition. |
||
160 | * |
||
161 | * @param array $items Array key-value pairs collection |
||
162 | * |
||
163 | * @return string Array value definition |
||
164 | */ |
||
165 | protected function arrayValue(array $items = array()) |
||
188 | |||
189 | /** |
||
190 | * Increase indentation. |
||
191 | * |
||
192 | * @return $this|AbstractGenerator |
||
193 | */ |
||
194 | 6 | public function increaseIndentation(): AbstractGenerator |
|
198 | |||
199 | /** |
||
200 | * Get indentation string. |
||
201 | * |
||
202 | * @param int $indentation Code level |
||
203 | * |
||
204 | * @return string Indentation string |
||
205 | */ |
||
206 | 41 | protected function indentation(int $indentation = 0): string |
|
210 | |||
211 | /** |
||
212 | * Decrease indentation. |
||
213 | * |
||
214 | * @return $this|AbstractGenerator |
||
215 | */ |
||
216 | public function decreaseIndentation(): AbstractGenerator |
||
220 | } |
||
221 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.