Completed
Push — master ( 1b8eb5...fcb010 )
by Martijn
26s
created
SwaggerGen/SwaggerGen.php 2 patches
Indentation   +210 added lines, -210 removed lines patch added patch discarded remove patch
@@ -38,215 +38,215 @@
 block discarded – undo
38 38
 class SwaggerGen
39 39
 {
40 40
 
41
-    const FORMAT_ARRAY = '';
42
-    const FORMAT_JSON = 'json';
43
-    const FORMAT_JSON_PRETTY = 'json+';
44
-    const FORMAT_YAML = 'yaml';
45
-
46
-    private $host;
47
-    private $basePath;
48
-    private $dirs;
49
-    private $defines = [];
50
-
51
-    /**
52
-     * @var TypeRegistry
53
-     */
54
-    private $typeRegistry;
55
-
56
-    /**
57
-     * Create a new SwaggerGen instance
58
-     *
59
-     * @param string $host
60
-     * @param string $basePath
61
-     * @param string[] $dirs
62
-     * @param TypeRegistry $typeRegistry
63
-     */
64
-    public function __construct($host = '', $basePath = '', $dirs = [], $typeRegistry = null)
65
-    {
66
-        $this->host = $host;
67
-        $this->basePath = $basePath;
68
-        $this->dirs = $dirs;
69
-        $this->typeRegistry = $typeRegistry;
70
-    }
71
-
72
-    /**
73
-     * Set a new type registry
74
-     *
75
-     * @param TypeRegistry $typeRegistry
76
-     */
77
-    public function setTypeRegistry($typeRegistry = null)
78
-    {
79
-        $this->typeRegistry = $typeRegistry;
80
-    }
81
-
82
-    /**
83
-     * @param string $name
84
-     */
85
-    public function define($name, $value = 1)
86
-    {
87
-        $this->defines[$name] = $value;
88
-    }
89
-
90
-    /**
91
-     * @param string $name
92
-     */
93
-    public function undefine($name)
94
-    {
95
-        unset($this->defines[$name]);
96
-    }
97
-
98
-    /**
99
-     * @param string $file
100
-     * @param string[] $dirs
101
-     * @return Statement[]
102
-     * @throws Exception
103
-     */
104
-    private function parsePhpFile(string $file, array $dirs): array
105
-    {
106
-        return (new Parser())->parse($file, $dirs, $this->defines);
107
-    }
108
-
109
-    /**
110
-     * @param string $file
111
-     * @param string[] $dirs
112
-     * @return Statement[]
113
-     */
114
-    private function parseTextFile($file, $dirs)
115
-    {
116
-        return (new TextParser())->parse($file, $dirs, $this->defines);
117
-    }
118
-
119
-    /**
120
-     * @param string $text
121
-     * @param string[] $dirs
122
-     * @return Statement[]
123
-     */
124
-    private function parseText(string $text, array $dirs): array
125
-    {
126
-        return (new TextParser())->parseText($text, $dirs, $this->defines);
127
-    }
128
-
129
-    /**
130
-     * Creates Swagger\Swagger object and populates it with statements
131
-     *
132
-     * This effectively converts the linear list of statements into parse-tree
133
-     * like structure, performing some checks (like rejecting unknown
134
-     * subcommands) during the process. Returned Swagger\Swagger object is
135
-     * ready for serialization with {@see Swagger\Swagger::toArray}
136
-     *
137
-     * @param string $host
138
-     * @param string $basePath
139
-     * @param Statement[] $statements
140
-     * @return Swagger
141
-     * @throws StatementException
142
-     */
143
-    public function parseStatements($host, $basePath, $statements)
144
-    {
145
-        $swagger = new Swagger($host, $basePath, $this->typeRegistry);
146
-
147
-        $stack = array($swagger);
148
-        /* @var AbstractObject[] $stack */
149
-        foreach ($statements as $statement) {
150
-            try {
151
-                $top = end($stack);
152
-
153
-                do {
154
-                    $result = $top->handleCommand($statement->getCommand(), $statement->getData());
155
-
156
-                    if ($result) {
157
-                        if ($result !== $top) {
158
-                            // Remove all similar classes from array first!
159
-                            $classname = get_class($result);
160
-                            $stack = array_filter($stack, static function ($class) use ($classname) {
161
-                                return !(is_a($class, $classname));
162
-                            });
163
-
164
-                            $stack[] = $result;
165
-                        }
166
-                    } else {
167
-                        $top = prev($stack);
168
-                    }
169
-                } while (!$result && $top);
170
-            } catch (Exception $e) {
171
-                throw new StatementException($e->getMessage(), $e->getCode(), $e, $statement);
172
-            }
173
-
174
-            if (!$result && !$top) {
175
-                $messages = array("Unsupported or unknown command: {$statement->getCommand()} {$statement->getData()}");
176
-
177
-                $stacktrace = [];
178
-                foreach ($stack as $object) {
179
-                    $stacktrace[] = (string)$object;
180
-                }
181
-                $messages[] = implode(', ' . PHP_EOL, $stacktrace);
182
-
183
-                throw new StatementException(implode('. ', $messages), 0, null, $statement);
184
-            }
185
-        }
186
-
187
-        return $swagger;
188
-    }
189
-
190
-    /**
191
-     * Get Swagger 2.x output
192
-     *
193
-     * @param string[] $files
194
-     * @param string[] $dirs
195
-     * @param string $format
196
-     * @return array|false|string
197
-     * @throws Exception
198
-     * @throws StatementException
199
-     */
200
-    public function getSwagger(array $files, array $dirs = [], string $format = self::FORMAT_ARRAY)
201
-    {
202
-        $dirs = array_merge($this->dirs, $dirs);
203
-
204
-        $statements = [];
205
-        foreach ($files as $file) {
206
-            switch (pathinfo($file, PATHINFO_EXTENSION)) {
207
-                case 'php':
208
-                    $fileStatements = $this->parsePhpFile($file, $dirs);
209
-                    break;
210
-
211
-                case 'txt':
212
-                    $fileStatements = $this->parseTextFile($file, $dirs);
213
-                    break;
214
-
215
-                default:
216
-                    $fileStatements = $this->parseText($file, $dirs);
217
-                    break;
218
-            }
219
-
220
-            $statements[] = $fileStatements;
221
-        }
222
-        $statements = array_merge(...$statements);
223
-
224
-        $output = $this->parseStatements($this->host, $this->basePath, $statements)->toArray();
225
-
226
-        switch ($format) {
227
-            case self::FORMAT_JSON:
228
-                $output = json_encode($output);
229
-                break;
230
-
231
-            case self::FORMAT_JSON_PRETTY:
232
-                $flags = (defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0); // Since PHP 5.4.0
233
-                $output = json_encode($output, $flags);
234
-                break;
235
-
236
-            case self::FORMAT_YAML:
237
-                if (!function_exists('yaml_emit')) {
238
-                    throw new Exception('YAML extension not installed.');
239
-                }
240
-                array_walk_recursive($output, static function (&$value) {
241
-                    if (is_object($value)) {
242
-                        $value = (array)$value;
243
-                    }
244
-                });
245
-                $output = yaml_emit($output, YAML_UTF8_ENCODING, YAML_LN_BREAK);
246
-                break;
247
-        }
248
-
249
-        return $output;
250
-    }
41
+	const FORMAT_ARRAY = '';
42
+	const FORMAT_JSON = 'json';
43
+	const FORMAT_JSON_PRETTY = 'json+';
44
+	const FORMAT_YAML = 'yaml';
45
+
46
+	private $host;
47
+	private $basePath;
48
+	private $dirs;
49
+	private $defines = [];
50
+
51
+	/**
52
+	 * @var TypeRegistry
53
+	 */
54
+	private $typeRegistry;
55
+
56
+	/**
57
+	 * Create a new SwaggerGen instance
58
+	 *
59
+	 * @param string $host
60
+	 * @param string $basePath
61
+	 * @param string[] $dirs
62
+	 * @param TypeRegistry $typeRegistry
63
+	 */
64
+	public function __construct($host = '', $basePath = '', $dirs = [], $typeRegistry = null)
65
+	{
66
+		$this->host = $host;
67
+		$this->basePath = $basePath;
68
+		$this->dirs = $dirs;
69
+		$this->typeRegistry = $typeRegistry;
70
+	}
71
+
72
+	/**
73
+	 * Set a new type registry
74
+	 *
75
+	 * @param TypeRegistry $typeRegistry
76
+	 */
77
+	public function setTypeRegistry($typeRegistry = null)
78
+	{
79
+		$this->typeRegistry = $typeRegistry;
80
+	}
81
+
82
+	/**
83
+	 * @param string $name
84
+	 */
85
+	public function define($name, $value = 1)
86
+	{
87
+		$this->defines[$name] = $value;
88
+	}
89
+
90
+	/**
91
+	 * @param string $name
92
+	 */
93
+	public function undefine($name)
94
+	{
95
+		unset($this->defines[$name]);
96
+	}
97
+
98
+	/**
99
+	 * @param string $file
100
+	 * @param string[] $dirs
101
+	 * @return Statement[]
102
+	 * @throws Exception
103
+	 */
104
+	private function parsePhpFile(string $file, array $dirs): array
105
+	{
106
+		return (new Parser())->parse($file, $dirs, $this->defines);
107
+	}
108
+
109
+	/**
110
+	 * @param string $file
111
+	 * @param string[] $dirs
112
+	 * @return Statement[]
113
+	 */
114
+	private function parseTextFile($file, $dirs)
115
+	{
116
+		return (new TextParser())->parse($file, $dirs, $this->defines);
117
+	}
118
+
119
+	/**
120
+	 * @param string $text
121
+	 * @param string[] $dirs
122
+	 * @return Statement[]
123
+	 */
124
+	private function parseText(string $text, array $dirs): array
125
+	{
126
+		return (new TextParser())->parseText($text, $dirs, $this->defines);
127
+	}
128
+
129
+	/**
130
+	 * Creates Swagger\Swagger object and populates it with statements
131
+	 *
132
+	 * This effectively converts the linear list of statements into parse-tree
133
+	 * like structure, performing some checks (like rejecting unknown
134
+	 * subcommands) during the process. Returned Swagger\Swagger object is
135
+	 * ready for serialization with {@see Swagger\Swagger::toArray}
136
+	 *
137
+	 * @param string $host
138
+	 * @param string $basePath
139
+	 * @param Statement[] $statements
140
+	 * @return Swagger
141
+	 * @throws StatementException
142
+	 */
143
+	public function parseStatements($host, $basePath, $statements)
144
+	{
145
+		$swagger = new Swagger($host, $basePath, $this->typeRegistry);
146
+
147
+		$stack = array($swagger);
148
+		/* @var AbstractObject[] $stack */
149
+		foreach ($statements as $statement) {
150
+			try {
151
+				$top = end($stack);
152
+
153
+				do {
154
+					$result = $top->handleCommand($statement->getCommand(), $statement->getData());
155
+
156
+					if ($result) {
157
+						if ($result !== $top) {
158
+							// Remove all similar classes from array first!
159
+							$classname = get_class($result);
160
+							$stack = array_filter($stack, static function ($class) use ($classname) {
161
+								return !(is_a($class, $classname));
162
+							});
163
+
164
+							$stack[] = $result;
165
+						}
166
+					} else {
167
+						$top = prev($stack);
168
+					}
169
+				} while (!$result && $top);
170
+			} catch (Exception $e) {
171
+				throw new StatementException($e->getMessage(), $e->getCode(), $e, $statement);
172
+			}
173
+
174
+			if (!$result && !$top) {
175
+				$messages = array("Unsupported or unknown command: {$statement->getCommand()} {$statement->getData()}");
176
+
177
+				$stacktrace = [];
178
+				foreach ($stack as $object) {
179
+					$stacktrace[] = (string)$object;
180
+				}
181
+				$messages[] = implode(', ' . PHP_EOL, $stacktrace);
182
+
183
+				throw new StatementException(implode('. ', $messages), 0, null, $statement);
184
+			}
185
+		}
186
+
187
+		return $swagger;
188
+	}
189
+
190
+	/**
191
+	 * Get Swagger 2.x output
192
+	 *
193
+	 * @param string[] $files
194
+	 * @param string[] $dirs
195
+	 * @param string $format
196
+	 * @return array|false|string
197
+	 * @throws Exception
198
+	 * @throws StatementException
199
+	 */
200
+	public function getSwagger(array $files, array $dirs = [], string $format = self::FORMAT_ARRAY)
201
+	{
202
+		$dirs = array_merge($this->dirs, $dirs);
203
+
204
+		$statements = [];
205
+		foreach ($files as $file) {
206
+			switch (pathinfo($file, PATHINFO_EXTENSION)) {
207
+				case 'php':
208
+					$fileStatements = $this->parsePhpFile($file, $dirs);
209
+					break;
210
+
211
+				case 'txt':
212
+					$fileStatements = $this->parseTextFile($file, $dirs);
213
+					break;
214
+
215
+				default:
216
+					$fileStatements = $this->parseText($file, $dirs);
217
+					break;
218
+			}
219
+
220
+			$statements[] = $fileStatements;
221
+		}
222
+		$statements = array_merge(...$statements);
223
+
224
+		$output = $this->parseStatements($this->host, $this->basePath, $statements)->toArray();
225
+
226
+		switch ($format) {
227
+			case self::FORMAT_JSON:
228
+				$output = json_encode($output);
229
+				break;
230
+
231
+			case self::FORMAT_JSON_PRETTY:
232
+				$flags = (defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0); // Since PHP 5.4.0
233
+				$output = json_encode($output, $flags);
234
+				break;
235
+
236
+			case self::FORMAT_YAML:
237
+				if (!function_exists('yaml_emit')) {
238
+					throw new Exception('YAML extension not installed.');
239
+				}
240
+				array_walk_recursive($output, static function (&$value) {
241
+					if (is_object($value)) {
242
+						$value = (array)$value;
243
+					}
244
+				});
245
+				$output = yaml_emit($output, YAML_UTF8_ENCODING, YAML_LN_BREAK);
246
+				break;
247
+		}
248
+
249
+		return $output;
250
+	}
251 251
 
252 252
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -157,7 +157,7 @@  discard block
 block discarded – undo
157 157
                         if ($result !== $top) {
158 158
                             // Remove all similar classes from array first!
159 159
                             $classname = get_class($result);
160
-                            $stack = array_filter($stack, static function ($class) use ($classname) {
160
+                            $stack = array_filter($stack, static function($class) use ($classname) {
161 161
                                 return !(is_a($class, $classname));
162 162
                             });
163 163
 
@@ -176,7 +176,7 @@  discard block
 block discarded – undo
176 176
 
177 177
                 $stacktrace = [];
178 178
                 foreach ($stack as $object) {
179
-                    $stacktrace[] = (string)$object;
179
+                    $stacktrace[] = (string) $object;
180 180
                 }
181 181
                 $messages[] = implode(', ' . PHP_EOL, $stacktrace);
182 182
 
@@ -237,9 +237,9 @@  discard block
 block discarded – undo
237 237
                 if (!function_exists('yaml_emit')) {
238 238
                     throw new Exception('YAML extension not installed.');
239 239
                 }
240
-                array_walk_recursive($output, static function (&$value) {
240
+                array_walk_recursive($output, static function(&$value) {
241 241
                     if (is_object($value)) {
242
-                        $value = (array)$value;
242
+                        $value = (array) $value;
243 243
                     }
244 244
                 });
245 245
                 $output = yaml_emit($output, YAML_UTF8_ENCODING, YAML_LN_BREAK);
Please login to merge, or discard this patch.
SwaggerGen/Parser/Php/Preprocessor.php 1 patch
Indentation   +57 added lines, -57 removed lines patch added patch discarded remove patch
@@ -18,72 +18,72 @@
 block discarded – undo
18 18
 class Preprocessor extends AbstractPreprocessor
19 19
 {
20 20
 
21
-    private $prefix = 'rest';
21
+	private $prefix = 'rest';
22 22
 
23
-    public function __construct($prefix = null)
24
-    {
25
-        parent::__construct();
26
-        if (!empty($prefix)) {
27
-            $this->prefix = $prefix;
28
-        }
29
-    }
23
+	public function __construct($prefix = null)
24
+	{
25
+		parent::__construct();
26
+		if (!empty($prefix)) {
27
+			$this->prefix = $prefix;
28
+		}
29
+	}
30 30
 
31
-    public function getPrefix()
32
-    {
33
-        return $this->prefix;
34
-    }
31
+	public function getPrefix()
32
+	{
33
+		return $this->prefix;
34
+	}
35 35
 
36
-    public function setPrefix($prefix)
37
-    {
38
-        $this->prefix = $prefix;
39
-    }
36
+	public function setPrefix($prefix)
37
+	{
38
+		$this->prefix = $prefix;
39
+	}
40 40
 
41
-    protected function parseContent($content)
42
-    {
43
-        $pattern = '/@' . preg_quote($this->getPrefix(), '/') . '\\\\([a-z]+)\\s*(.*)$/';
41
+	protected function parseContent($content)
42
+	{
43
+		$pattern = '/@' . preg_quote($this->getPrefix(), '/') . '\\\\([a-z]+)\\s*(.*)$/';
44 44
 
45
-        $output_file = '';
45
+		$output_file = '';
46 46
 
47
-        foreach (token_get_all($content) as $token) {
48
-            $output = '';
47
+		foreach (token_get_all($content) as $token) {
48
+			$output = '';
49 49
 
50
-            if (is_array($token)) {
51
-                switch ($token[0]) {
52
-                    case T_DOC_COMMENT:
53
-                    case T_COMMENT:
54
-                        foreach (preg_split('/(\\R)/m', $token[1], -1, PREG_SPLIT_DELIM_CAPTURE) as $index => $line) {
55
-                            if ($index % 2) {
56
-                                $output .= $line;
57
-                            } else {
58
-                                $match = array();
59
-                                if (preg_match($pattern, $line, $match) === 1) {
60
-                                    if (!$this->handle($match[1], $match[2]) && $this->getState()) {
61
-                                        $output .= $line;
62
-                                    } else {
63
-                                        $output .= str_replace('@' . $this->getPrefix() . '\\', '@!' . $this->getPrefix() . '\\', $line);
64
-                                    }
65
-                                } else {
66
-                                    $output .= $line;
67
-                                }
68
-                            }
69
-                        }
70
-                        break;
50
+			if (is_array($token)) {
51
+				switch ($token[0]) {
52
+					case T_DOC_COMMENT:
53
+					case T_COMMENT:
54
+						foreach (preg_split('/(\\R)/m', $token[1], -1, PREG_SPLIT_DELIM_CAPTURE) as $index => $line) {
55
+							if ($index % 2) {
56
+								$output .= $line;
57
+							} else {
58
+								$match = array();
59
+								if (preg_match($pattern, $line, $match) === 1) {
60
+									if (!$this->handle($match[1], $match[2]) && $this->getState()) {
61
+										$output .= $line;
62
+									} else {
63
+										$output .= str_replace('@' . $this->getPrefix() . '\\', '@!' . $this->getPrefix() . '\\', $line);
64
+									}
65
+								} else {
66
+									$output .= $line;
67
+								}
68
+							}
69
+						}
70
+						break;
71 71
 
72
-                    default:
73
-                        $output .= $token[1];
74
-                }
75
-            } else {
76
-                $output .= $token;
77
-            }
72
+					default:
73
+						$output .= $token[1];
74
+				}
75
+			} else {
76
+				$output .= $token;
77
+			}
78 78
 
79
-            if ($this->getState()) {
80
-                $output_file .= $output;
81
-            } else {
82
-                $output_file .= '/* ' . $output . ' */';
83
-            }
84
-        }
79
+			if ($this->getState()) {
80
+				$output_file .= $output;
81
+			} else {
82
+				$output_file .= '/* ' . $output . ' */';
83
+			}
84
+		}
85 85
 
86
-        return $output_file;
87
-    }
86
+		return $output_file;
87
+	}
88 88
 
89 89
 }
Please login to merge, or discard this patch.
SwaggerGen/Parser/Php/Entity/ParserFunction.php 1 patch
Indentation   +62 added lines, -62 removed lines patch added patch discarded remove patch
@@ -16,77 +16,77 @@
 block discarded – undo
16 16
 class ParserFunction extends AbstractEntity
17 17
 {
18 18
 
19
-    public $name = null;
20
-    private $lastStatements = null;
19
+	public $name = null;
20
+	private $lastStatements = null;
21 21
 
22
-    public function __construct(Parser $Parser, &$tokens, $Statements)
23
-    {
24
-        if ($Statements) {
25
-            $this->Statements = array_merge($this->Statements, $Statements);
26
-        }
22
+	public function __construct(Parser $Parser, &$tokens, $Statements)
23
+	{
24
+		if ($Statements) {
25
+			$this->Statements = array_merge($this->Statements, $Statements);
26
+		}
27 27
 
28
-        $depth = 0;
28
+		$depth = 0;
29 29
 
30
-        $token = current($tokens);
31
-        while ($token) {
32
-            switch ($token[0]) {
33
-                case T_STRING:
34
-                    if (empty($this->name)) {
35
-                        $this->name = $token[1];
36
-                    }
37
-                    break;
30
+		$token = current($tokens);
31
+		while ($token) {
32
+			switch ($token[0]) {
33
+				case T_STRING:
34
+					if (empty($this->name)) {
35
+						$this->name = $token[1];
36
+					}
37
+					break;
38 38
 
39
-                case '{':
40
-                case T_CURLY_OPEN:
41
-                case T_DOLLAR_OPEN_CURLY_BRACES:
42
-                case T_STRING_VARNAME:
43
-                    ++$depth;
44
-                    break;
39
+				case '{':
40
+				case T_CURLY_OPEN:
41
+				case T_DOLLAR_OPEN_CURLY_BRACES:
42
+				case T_STRING_VARNAME:
43
+					++$depth;
44
+					break;
45 45
 
46
-                case '}':
47
-                    --$depth;
48
-                    if ($depth == 0) {
49
-                        if ($this->lastStatements) {
50
-                            $this->Statements = array_merge($this->Statements, $this->lastStatements);
51
-                            $this->lastStatements = null;
52
-                        }
53
-                        return;
54
-                    }
55
-                    break;
46
+				case '}':
47
+					--$depth;
48
+					if ($depth == 0) {
49
+						if ($this->lastStatements) {
50
+							$this->Statements = array_merge($this->Statements, $this->lastStatements);
51
+							$this->lastStatements = null;
52
+						}
53
+						return;
54
+					}
55
+					break;
56 56
 
57
-                case T_COMMENT:
58
-                    if ($this->lastStatements) {
59
-                        $this->Statements = array_merge($this->Statements, $this->lastStatements);
60
-                        $this->lastStatements = null;
61
-                    }
62
-                    $Statements = $Parser->tokenToStatements($token);
63
-                    $Parser->queueClassesFromComments($Statements);
64
-                    $this->Statements = array_merge($this->Statements, $Statements);
65
-                    break;
57
+				case T_COMMENT:
58
+					if ($this->lastStatements) {
59
+						$this->Statements = array_merge($this->Statements, $this->lastStatements);
60
+						$this->lastStatements = null;
61
+					}
62
+					$Statements = $Parser->tokenToStatements($token);
63
+					$Parser->queueClassesFromComments($Statements);
64
+					$this->Statements = array_merge($this->Statements, $Statements);
65
+					break;
66 66
 
67
-                case T_DOC_COMMENT:
68
-                    if ($this->lastStatements) {
69
-                        $this->Statements = array_merge($this->Statements, $this->lastStatements);
70
-                    }
71
-                    $Statements = $Parser->tokenToStatements($token);
72
-                    $Parser->queueClassesFromComments($Statements);
73
-                    $this->lastStatements = $Statements;
74
-                    break;
75
-            }
67
+				case T_DOC_COMMENT:
68
+					if ($this->lastStatements) {
69
+						$this->Statements = array_merge($this->Statements, $this->lastStatements);
70
+					}
71
+					$Statements = $Parser->tokenToStatements($token);
72
+					$Parser->queueClassesFromComments($Statements);
73
+					$this->lastStatements = $Statements;
74
+					break;
75
+			}
76 76
 
77
-            $token = next($tokens);
78
-        }
77
+			$token = next($tokens);
78
+		}
79 79
 
80
-        if ($this->lastStatements) {
81
-            $this->Statements = array_merge($this->Statements, $this->lastStatements);
82
-            $this->lastStatements = null;
83
-        }
84
-    }
80
+		if ($this->lastStatements) {
81
+			$this->Statements = array_merge($this->Statements, $this->lastStatements);
82
+			$this->lastStatements = null;
83
+		}
84
+	}
85 85
 
86
-    public function getStatements(): array
87
-    {
88
-        // inherit
89
-        return $this->Statements;
90
-    }
86
+	public function getStatements(): array
87
+	{
88
+		// inherit
89
+		return $this->Statements;
90
+	}
91 91
 
92 92
 }
Please login to merge, or discard this patch.
SwaggerGen/Parser/Php/Entity/AbstractEntity.php 1 patch
Indentation   +25 added lines, -25 removed lines patch added patch discarded remove patch
@@ -15,30 +15,30 @@
 block discarded – undo
15 15
 class AbstractEntity
16 16
 {
17 17
 
18
-    /**
19
-     * @var Statement[]
20
-     */
21
-    public $Statements = array();
22
-
23
-    /**
24
-     * Returns true if a statement with the specified command exists.
25
-     * @param string $command
26
-     * @return boolean
27
-     */
28
-    public function hasCommand($command): bool
29
-    {
30
-        foreach ($this->Statements as $Statement) {
31
-            if ($Statement->getCommand() === $command) {
32
-                return true;
33
-            }
34
-        }
35
-
36
-        return false;
37
-    }
38
-
39
-    public function getStatements(): array
40
-    {
41
-        return $this->Statements;
42
-    }
18
+	/**
19
+	 * @var Statement[]
20
+	 */
21
+	public $Statements = array();
22
+
23
+	/**
24
+	 * Returns true if a statement with the specified command exists.
25
+	 * @param string $command
26
+	 * @return boolean
27
+	 */
28
+	public function hasCommand($command): bool
29
+	{
30
+		foreach ($this->Statements as $Statement) {
31
+			if ($Statement->getCommand() === $command) {
32
+				return true;
33
+			}
34
+		}
35
+
36
+		return false;
37
+	}
38
+
39
+	public function getStatements(): array
40
+	{
41
+		return $this->Statements;
42
+	}
43 43
 
44 44
 }
Please login to merge, or discard this patch.
SwaggerGen/Parser/Php/Parser.php 1 patch
Indentation   +364 added lines, -364 removed lines patch added patch discarded remove patch
@@ -20,374 +20,374 @@
 block discarded – undo
20 20
 class Parser extends Entity\AbstractEntity implements IParser
21 21
 {
22 22
 
23
-    const COMMENT_TAG = 'rest';
23
+	const COMMENT_TAG = 'rest';
24 24
 
25 25
 // transient
26 26
 
27
-    private $current_file = null;
28
-    private $files_queued = [];
29
-    private $files_done = [];
30
-    private $dirs = [];
27
+	private $current_file = null;
28
+	private $files_queued = [];
29
+	private $files_done = [];
30
+	private $dirs = [];
31 31
 // States
32 32
 
33
-    /** @var Statement[] */
34
-    public $statements = [];
35
-
36
-    /**
37
-     * @var Statement[]|null
38
-     */
39
-    private $lastStatements = [];
40
-
41
-    /**
42
-     * @var Entity\ParserClass[]
43
-     */
44
-    public $Classes = [];
45
-
46
-    /**
47
-     * @var Entity\ParserFunction[]
48
-     */
49
-    public $Functions = [];
50
-
51
-    /**
52
-     * @var AbstractPreprocessor
53
-     */
54
-    private $Preprocessor;
55
-
56
-    /**
57
-     * Directories available to all parse calls
58
-     *
59
-     * @var string[]
60
-     */
61
-    protected $common_dirs = [];
62
-
63
-    public function __construct(array $dirs = [])
64
-    {
65
-        foreach ($dirs as $dir) {
66
-            $this->common_dirs[] = realpath($dir);
67
-        }
68
-
69
-        $this->Preprocessor = new Preprocessor(self::COMMENT_TAG);
70
-    }
71
-
72
-    public function addDirs(array $dirs)
73
-    {
74
-        foreach ($dirs as $dir) {
75
-            $this->common_dirs[] = realpath($dir);
76
-        }
77
-    }
78
-
79
-    private function extractStatements()
80
-    {
81
-        // Core comments
82
-        $Statements = $this->statements;
83
-
84
-        // Functions
85
-        foreach ($this->Functions as $Function) {
86
-            if ($Function->hasCommand('method')) {
87
-                $Statements = array_merge($Statements, $Function->Statements);
88
-            }
89
-        }
90
-
91
-        // Classes
92
-        foreach ($this->Classes as $Class) {
93
-            $Statements = array_merge($Statements, $Class->Statements);
94
-            foreach ($Class->Methods as $Method) {
95
-                if ($Method->hasCommand('method')) {
96
-                    $Statements = array_merge($Statements, $Method->Statements);
97
-                }
98
-            }
99
-        }
100
-
101
-        return $Statements;
102
-    }
103
-
104
-    /**
105
-     * @throws Exception
106
-     */
107
-    public function parse($file, array $dirs = [], array $defines = [])
108
-    {
109
-        $this->dirs = $this->common_dirs;
110
-        foreach ($dirs as $dir) {
111
-            $this->dirs[] = realpath($dir);
112
-        }
113
-
114
-        $this->parseFiles(array($file), $defines);
115
-
116
-        // Inherit classes
117
-        foreach ($this->Classes as $Class) {
118
-            $this->inherit($Class);
119
-        }
120
-
121
-        // Expand functions with used and seen functions/methods.
122
-        foreach ($this->Classes as $Class) {
123
-            foreach ($Class->Methods as $Method) {
124
-                $Method->Statements = $this->expand($Method->Statements, $Class);
125
-            }
126
-        }
127
-
128
-        return $this->extractStatements();
129
-    }
130
-
131
-    /**
132
-     * Convert a T_*_COMMENT string to an array of Statements
133
-     * @param array $token
134
-     * @return Statement[]
135
-     */
136
-    public function tokenToStatements($token)
137
-    {
138
-        list($comment, $commentLineNumber) = $token;
139
-        $commentLines = [];
140
-
141
-        $match = [];
142
-        if (preg_match('~^/\*\*?\s*(.*)\s*\*\/$~sm', $comment, $match) === 1) {
143
-            $lines = explode("\n", $match[0]);
144
-            foreach ($lines as $line) {
145
-                if ((preg_match('~^\s*\*?\s*(.*?)\s*$~', $line, $match) === 1)
146
-                    && !empty($match[1])) {
147
-                    $commentLines[] = trim($match[1]);
148
-                }
149
-            }
150
-        } elseif (preg_match('~^//\s*(.*)$~', $comment, $match) === 1) {
151
-            $commentLines[] = trim($match[1]);
152
-        }
153
-        // to commands
154
-        $match = [];
155
-        $command = null;
156
-        $data = '';
157
-        $commandLineNumber = 0;
158
-        $statements = [];
159
-        foreach ($commentLines as $lineNumber => $line) {
160
-            // If new @-command, store any old and start new
161
-            if ($command !== null && chr(ord($line)) === '@') {
162
-                $statements[] = new Statement($command, $data, $this->current_file, $commentLineNumber + $commandLineNumber);
163
-                $command = null;
164
-                $data = '';
165
-            }
166
-
167
-            if (preg_match('~^@' . preg_quote(self::COMMENT_TAG, '~') . '\\\\([a-z][-a-z]*[?!]?)\\s*(.*)$~', $line, $match) === 1) {
168
-                list($command, $data) = $match;
169
-                $commandLineNumber = $lineNumber;
170
-            } elseif ($command !== null) {
171
-                if ($lineNumber < count($commentLines) - 1) {
172
-                    $data .= ' ' . $line;
173
-                } else {
174
-                    $data .= preg_replace('~\s*\**\/\s*$~', '', $line);
175
-                }
176
-            }
177
-        }
178
-
179
-        if ($command !== null) {
180
-            $statements[] = new Statement($command, $data, $this->current_file, $commentLineNumber + $commandLineNumber);
181
-        }
182
-
183
-        return $statements;
184
-    }
185
-
186
-    public function queueClass($classname)
187
-    {
188
-        foreach ($this->dirs as $dir) {
189
-            $paths = array(
190
-                $dir . DIRECTORY_SEPARATOR . $classname . '.php',
191
-                $dir . DIRECTORY_SEPARATOR . $classname . '.class.php',
192
-            );
193
-
194
-            foreach ($paths as $path) {
195
-                $realpath = realpath($path);
196
-                if (in_array($realpath, $this->files_done)) {
197
-                    return;
198
-                }
199
-
200
-                if (is_file($realpath)) {
201
-                    $this->files_queued[] = $realpath;
202
-                    return;
203
-                }
204
-            }
205
-        }
206
-
207
-        // assume it's a class;
208
-    }
209
-
210
-    /**
211
-     * Add to the queue any classes based on the commands.
212
-     * @param Statement[] $Statements
213
-     */
214
-    public function queueClassesFromComments(array $Statements)
215
-    {
216
-        foreach ($Statements as $Statement) {
217
-            if (in_array($Statement->getCommand(), array('uses', 'see'))) {
218
-                $match = [];
219
-                if ((preg_match('~^(\w+)(::|->)?(\w+)?(?:\(\))?$~', $Statement->getData(), $match) === 1)
220
-                    && !in_array($match[1], array('self', '$this'))) {
221
-                    $this->queueClass($match[1]);
222
-                }
223
-            }
224
-        }
225
-    }
226
-
227
-    private function parseTokens($source)
228
-    {
229
-        $mode = null;
230
-        $namespace = '';
231
-
232
-        $tokens = token_get_all($source);
233
-        $token = reset($tokens);
234
-        while ($token) {
235
-            switch ($token[0]) {
236
-                case T_NAMESPACE:
237
-                    $mode = T_NAMESPACE;
238
-                    break;
239
-
240
-                case T_NS_SEPARATOR:
241
-                case T_STRING:
242
-                    if ($mode === T_NAMESPACE) {
243
-                        $namespace .= $token[1];
244
-                    }
245
-                    break;
246
-
247
-                case ';':
248
-                    $mode = null;
249
-                    break;
250
-
251
-                case T_CLASS:
252
-                case T_INTERFACE:
253
-                    $Class = new Entity\ParserClass($this, $tokens, $this->lastStatements);
254
-                    $this->Classes[strtolower($Class->name)] = $Class;
255
-                    $this->lastStatements = null;
256
-                    break;
257
-
258
-                case T_FUNCTION:
259
-                    $Function = new Entity\ParserFunction($this, $tokens, $this->lastStatements);
260
-                    $this->Functions[strtolower($Function->name)] = $Function;
261
-                    $this->lastStatements = null;
262
-                    break;
263
-
264
-                case T_COMMENT:
265
-                    if ($this->lastStatements !== null) {
266
-                        $this->statements = array_merge($this->statements, $this->lastStatements);
267
-                        $this->lastStatements = null;
268
-                    }
269
-                    $Statements = $this->tokenToStatements($token);
270
-                    $this->queueClassesFromComments($Statements);
271
-                    $this->statements = array_merge($this->statements, $Statements);
272
-                    break;
273
-
274
-                case T_DOC_COMMENT:
275
-                    if ($this->lastStatements !== null) {
276
-                        $this->statements = array_merge($this->statements, $this->lastStatements);
277
-                    }
278
-                    $Statements = $this->tokenToStatements($token);
279
-                    $this->queueClassesFromComments($Statements);
280
-                    $this->lastStatements = $Statements;
281
-                    break;
282
-            }
283
-
284
-            $token = next($tokens);
285
-        }
286
-    }
287
-
288
-    private function parseFiles(array $files, array $defines = [])
289
-    {
290
-        $this->files_queued = $files;
291
-
292
-        $index = 0;
293
-        while (($file = array_shift($this->files_queued)) !== null) {
294
-            $file = realpath($file);
295
-
296
-            // @todo Test if this works
297
-            if (in_array($file, $this->files_done)) {
298
-                continue;
299
-            }
300
-
301
-            $this->current_file = $file;
302
-            $this->files_done[] = $file;
303
-            ++$index;
304
-
305
-            $this->Preprocessor->resetDefines();
306
-            $this->Preprocessor->addDefines($defines);
307
-            $source = $this->Preprocessor->preprocessFile($file);
308
-
309
-            $this->parseTokens($source);
310
-
311
-            if ($this->lastStatements !== null) {
312
-                $this->statements = array_merge($this->statements, $this->lastStatements);
313
-                $this->lastStatements = null;
314
-            }
315
-        }
316
-
317
-        $this->current_file = null;
318
-    }
319
-
320
-    /**
321
-     * Inherit the statements
322
-     * @param ParserClass $Class
323
-     */
324
-    private function inherit(Entity\ParserClass $Class)
325
-    {
326
-        $inherits = array_merge(array($Class->extends), $Class->implements);
327
-        while (($inherit = array_shift($inherits)) !== null) {
328
-            if (isset($this->Classes[strtolower($inherit)])) {
329
-                $inheritedClass = $this->Classes[strtolower($inherit)];
330
-                $this->inherit($inheritedClass);
331
-
332
-                foreach ($inheritedClass->Methods as $name => $Method) {
333
-                    if (!isset($Class->Methods[$name])) {
334
-                        $Class->Methods[$name] = $Method;
335
-                    }
336
-                }
337
-            }
338
-        }
339
-    }
340
-
341
-    /**
342
-     * Expands a set of comments with comments of methods referred to by rest\uses statements.
343
-     *
344
-     * @param Statement[] $Statements
345
-     * @return Statement[]
346
-     * @throws Exception
347
-     * @throws Exception
348
-     * @throws Exception
349
-     */
350
-    private function expand(array $Statements, ?ParserClass $Self = null)
351
-    {
352
-        $output = [];
353
-
354
-        $match = [];
355
-        foreach ($Statements as $Statement) {
356
-            if (in_array($Statement->getCommand(), array('uses', 'see'))) {
357
-                if (preg_match('/^((?:\\w+)|\$this)(?:(::|->)(\\w+))?(?:\\(\\))?$/', strtolower($Statement->getData()), $match) === 1) {
358
-                    if (count($match) >= 3) {
359
-                        $Class = null;
360
-                        if (in_array($match[1], array('$this', 'self', 'static'))) {
361
-                            $Class = $Self;
362
-                        } elseif (isset($this->Classes[$match[1]])) {
363
-                            $Class = $this->Classes[$match[1]];
364
-                        }
365
-
366
-                        if ($Class) {
367
-                            if (isset($Class->Methods[$match[3]])) {
368
-                                $Method = $Class->Methods[$match[3]];
369
-                                $Method->Statements = $this->expand($Method->Statements, $Class);
370
-                                $output = array_merge($output, $Method->Statements);
371
-                            } else {
372
-                                throw new Exception("Method '{$match[3]}' for class '{$match[1]}' not found");
373
-                            }
374
-                        } else {
375
-                            throw new Exception("Class '{$match[1]}' not found");
376
-                        }
377
-                    } elseif (isset($this->Functions[$match[1]])) {
378
-                        $Function = $this->Functions[$match[1]];
379
-                        $Function->Statements = $this->expand($Function->Statements);
380
-                        $output = array_merge($output, $Function->Statements);
381
-                    } else {
382
-                        throw new Exception("Function '{$match[1]}' not found");
383
-                    }
384
-                }
385
-            } else {
386
-                $output[] = $Statement;
387
-            }
388
-        }
389
-
390
-        return $output;
391
-    }
33
+	/** @var Statement[] */
34
+	public $statements = [];
35
+
36
+	/**
37
+	 * @var Statement[]|null
38
+	 */
39
+	private $lastStatements = [];
40
+
41
+	/**
42
+	 * @var Entity\ParserClass[]
43
+	 */
44
+	public $Classes = [];
45
+
46
+	/**
47
+	 * @var Entity\ParserFunction[]
48
+	 */
49
+	public $Functions = [];
50
+
51
+	/**
52
+	 * @var AbstractPreprocessor
53
+	 */
54
+	private $Preprocessor;
55
+
56
+	/**
57
+	 * Directories available to all parse calls
58
+	 *
59
+	 * @var string[]
60
+	 */
61
+	protected $common_dirs = [];
62
+
63
+	public function __construct(array $dirs = [])
64
+	{
65
+		foreach ($dirs as $dir) {
66
+			$this->common_dirs[] = realpath($dir);
67
+		}
68
+
69
+		$this->Preprocessor = new Preprocessor(self::COMMENT_TAG);
70
+	}
71
+
72
+	public function addDirs(array $dirs)
73
+	{
74
+		foreach ($dirs as $dir) {
75
+			$this->common_dirs[] = realpath($dir);
76
+		}
77
+	}
78
+
79
+	private function extractStatements()
80
+	{
81
+		// Core comments
82
+		$Statements = $this->statements;
83
+
84
+		// Functions
85
+		foreach ($this->Functions as $Function) {
86
+			if ($Function->hasCommand('method')) {
87
+				$Statements = array_merge($Statements, $Function->Statements);
88
+			}
89
+		}
90
+
91
+		// Classes
92
+		foreach ($this->Classes as $Class) {
93
+			$Statements = array_merge($Statements, $Class->Statements);
94
+			foreach ($Class->Methods as $Method) {
95
+				if ($Method->hasCommand('method')) {
96
+					$Statements = array_merge($Statements, $Method->Statements);
97
+				}
98
+			}
99
+		}
100
+
101
+		return $Statements;
102
+	}
103
+
104
+	/**
105
+	 * @throws Exception
106
+	 */
107
+	public function parse($file, array $dirs = [], array $defines = [])
108
+	{
109
+		$this->dirs = $this->common_dirs;
110
+		foreach ($dirs as $dir) {
111
+			$this->dirs[] = realpath($dir);
112
+		}
113
+
114
+		$this->parseFiles(array($file), $defines);
115
+
116
+		// Inherit classes
117
+		foreach ($this->Classes as $Class) {
118
+			$this->inherit($Class);
119
+		}
120
+
121
+		// Expand functions with used and seen functions/methods.
122
+		foreach ($this->Classes as $Class) {
123
+			foreach ($Class->Methods as $Method) {
124
+				$Method->Statements = $this->expand($Method->Statements, $Class);
125
+			}
126
+		}
127
+
128
+		return $this->extractStatements();
129
+	}
130
+
131
+	/**
132
+	 * Convert a T_*_COMMENT string to an array of Statements
133
+	 * @param array $token
134
+	 * @return Statement[]
135
+	 */
136
+	public function tokenToStatements($token)
137
+	{
138
+		list($comment, $commentLineNumber) = $token;
139
+		$commentLines = [];
140
+
141
+		$match = [];
142
+		if (preg_match('~^/\*\*?\s*(.*)\s*\*\/$~sm', $comment, $match) === 1) {
143
+			$lines = explode("\n", $match[0]);
144
+			foreach ($lines as $line) {
145
+				if ((preg_match('~^\s*\*?\s*(.*?)\s*$~', $line, $match) === 1)
146
+					&& !empty($match[1])) {
147
+					$commentLines[] = trim($match[1]);
148
+				}
149
+			}
150
+		} elseif (preg_match('~^//\s*(.*)$~', $comment, $match) === 1) {
151
+			$commentLines[] = trim($match[1]);
152
+		}
153
+		// to commands
154
+		$match = [];
155
+		$command = null;
156
+		$data = '';
157
+		$commandLineNumber = 0;
158
+		$statements = [];
159
+		foreach ($commentLines as $lineNumber => $line) {
160
+			// If new @-command, store any old and start new
161
+			if ($command !== null && chr(ord($line)) === '@') {
162
+				$statements[] = new Statement($command, $data, $this->current_file, $commentLineNumber + $commandLineNumber);
163
+				$command = null;
164
+				$data = '';
165
+			}
166
+
167
+			if (preg_match('~^@' . preg_quote(self::COMMENT_TAG, '~') . '\\\\([a-z][-a-z]*[?!]?)\\s*(.*)$~', $line, $match) === 1) {
168
+				list($command, $data) = $match;
169
+				$commandLineNumber = $lineNumber;
170
+			} elseif ($command !== null) {
171
+				if ($lineNumber < count($commentLines) - 1) {
172
+					$data .= ' ' . $line;
173
+				} else {
174
+					$data .= preg_replace('~\s*\**\/\s*$~', '', $line);
175
+				}
176
+			}
177
+		}
178
+
179
+		if ($command !== null) {
180
+			$statements[] = new Statement($command, $data, $this->current_file, $commentLineNumber + $commandLineNumber);
181
+		}
182
+
183
+		return $statements;
184
+	}
185
+
186
+	public function queueClass($classname)
187
+	{
188
+		foreach ($this->dirs as $dir) {
189
+			$paths = array(
190
+				$dir . DIRECTORY_SEPARATOR . $classname . '.php',
191
+				$dir . DIRECTORY_SEPARATOR . $classname . '.class.php',
192
+			);
193
+
194
+			foreach ($paths as $path) {
195
+				$realpath = realpath($path);
196
+				if (in_array($realpath, $this->files_done)) {
197
+					return;
198
+				}
199
+
200
+				if (is_file($realpath)) {
201
+					$this->files_queued[] = $realpath;
202
+					return;
203
+				}
204
+			}
205
+		}
206
+
207
+		// assume it's a class;
208
+	}
209
+
210
+	/**
211
+	 * Add to the queue any classes based on the commands.
212
+	 * @param Statement[] $Statements
213
+	 */
214
+	public function queueClassesFromComments(array $Statements)
215
+	{
216
+		foreach ($Statements as $Statement) {
217
+			if (in_array($Statement->getCommand(), array('uses', 'see'))) {
218
+				$match = [];
219
+				if ((preg_match('~^(\w+)(::|->)?(\w+)?(?:\(\))?$~', $Statement->getData(), $match) === 1)
220
+					&& !in_array($match[1], array('self', '$this'))) {
221
+					$this->queueClass($match[1]);
222
+				}
223
+			}
224
+		}
225
+	}
226
+
227
+	private function parseTokens($source)
228
+	{
229
+		$mode = null;
230
+		$namespace = '';
231
+
232
+		$tokens = token_get_all($source);
233
+		$token = reset($tokens);
234
+		while ($token) {
235
+			switch ($token[0]) {
236
+				case T_NAMESPACE:
237
+					$mode = T_NAMESPACE;
238
+					break;
239
+
240
+				case T_NS_SEPARATOR:
241
+				case T_STRING:
242
+					if ($mode === T_NAMESPACE) {
243
+						$namespace .= $token[1];
244
+					}
245
+					break;
246
+
247
+				case ';':
248
+					$mode = null;
249
+					break;
250
+
251
+				case T_CLASS:
252
+				case T_INTERFACE:
253
+					$Class = new Entity\ParserClass($this, $tokens, $this->lastStatements);
254
+					$this->Classes[strtolower($Class->name)] = $Class;
255
+					$this->lastStatements = null;
256
+					break;
257
+
258
+				case T_FUNCTION:
259
+					$Function = new Entity\ParserFunction($this, $tokens, $this->lastStatements);
260
+					$this->Functions[strtolower($Function->name)] = $Function;
261
+					$this->lastStatements = null;
262
+					break;
263
+
264
+				case T_COMMENT:
265
+					if ($this->lastStatements !== null) {
266
+						$this->statements = array_merge($this->statements, $this->lastStatements);
267
+						$this->lastStatements = null;
268
+					}
269
+					$Statements = $this->tokenToStatements($token);
270
+					$this->queueClassesFromComments($Statements);
271
+					$this->statements = array_merge($this->statements, $Statements);
272
+					break;
273
+
274
+				case T_DOC_COMMENT:
275
+					if ($this->lastStatements !== null) {
276
+						$this->statements = array_merge($this->statements, $this->lastStatements);
277
+					}
278
+					$Statements = $this->tokenToStatements($token);
279
+					$this->queueClassesFromComments($Statements);
280
+					$this->lastStatements = $Statements;
281
+					break;
282
+			}
283
+
284
+			$token = next($tokens);
285
+		}
286
+	}
287
+
288
+	private function parseFiles(array $files, array $defines = [])
289
+	{
290
+		$this->files_queued = $files;
291
+
292
+		$index = 0;
293
+		while (($file = array_shift($this->files_queued)) !== null) {
294
+			$file = realpath($file);
295
+
296
+			// @todo Test if this works
297
+			if (in_array($file, $this->files_done)) {
298
+				continue;
299
+			}
300
+
301
+			$this->current_file = $file;
302
+			$this->files_done[] = $file;
303
+			++$index;
304
+
305
+			$this->Preprocessor->resetDefines();
306
+			$this->Preprocessor->addDefines($defines);
307
+			$source = $this->Preprocessor->preprocessFile($file);
308
+
309
+			$this->parseTokens($source);
310
+
311
+			if ($this->lastStatements !== null) {
312
+				$this->statements = array_merge($this->statements, $this->lastStatements);
313
+				$this->lastStatements = null;
314
+			}
315
+		}
316
+
317
+		$this->current_file = null;
318
+	}
319
+
320
+	/**
321
+	 * Inherit the statements
322
+	 * @param ParserClass $Class
323
+	 */
324
+	private function inherit(Entity\ParserClass $Class)
325
+	{
326
+		$inherits = array_merge(array($Class->extends), $Class->implements);
327
+		while (($inherit = array_shift($inherits)) !== null) {
328
+			if (isset($this->Classes[strtolower($inherit)])) {
329
+				$inheritedClass = $this->Classes[strtolower($inherit)];
330
+				$this->inherit($inheritedClass);
331
+
332
+				foreach ($inheritedClass->Methods as $name => $Method) {
333
+					if (!isset($Class->Methods[$name])) {
334
+						$Class->Methods[$name] = $Method;
335
+					}
336
+				}
337
+			}
338
+		}
339
+	}
340
+
341
+	/**
342
+	 * Expands a set of comments with comments of methods referred to by rest\uses statements.
343
+	 *
344
+	 * @param Statement[] $Statements
345
+	 * @return Statement[]
346
+	 * @throws Exception
347
+	 * @throws Exception
348
+	 * @throws Exception
349
+	 */
350
+	private function expand(array $Statements, ?ParserClass $Self = null)
351
+	{
352
+		$output = [];
353
+
354
+		$match = [];
355
+		foreach ($Statements as $Statement) {
356
+			if (in_array($Statement->getCommand(), array('uses', 'see'))) {
357
+				if (preg_match('/^((?:\\w+)|\$this)(?:(::|->)(\\w+))?(?:\\(\\))?$/', strtolower($Statement->getData()), $match) === 1) {
358
+					if (count($match) >= 3) {
359
+						$Class = null;
360
+						if (in_array($match[1], array('$this', 'self', 'static'))) {
361
+							$Class = $Self;
362
+						} elseif (isset($this->Classes[$match[1]])) {
363
+							$Class = $this->Classes[$match[1]];
364
+						}
365
+
366
+						if ($Class) {
367
+							if (isset($Class->Methods[$match[3]])) {
368
+								$Method = $Class->Methods[$match[3]];
369
+								$Method->Statements = $this->expand($Method->Statements, $Class);
370
+								$output = array_merge($output, $Method->Statements);
371
+							} else {
372
+								throw new Exception("Method '{$match[3]}' for class '{$match[1]}' not found");
373
+							}
374
+						} else {
375
+							throw new Exception("Class '{$match[1]}' not found");
376
+						}
377
+					} elseif (isset($this->Functions[$match[1]])) {
378
+						$Function = $this->Functions[$match[1]];
379
+						$Function->Statements = $this->expand($Function->Statements);
380
+						$output = array_merge($output, $Function->Statements);
381
+					} else {
382
+						throw new Exception("Function '{$match[1]}' not found");
383
+					}
384
+				}
385
+			} else {
386
+				$output[] = $Statement;
387
+			}
388
+		}
389
+
390
+		return $output;
391
+	}
392 392
 
393 393
 }
Please login to merge, or discard this patch.
SwaggerGen/Swagger/Operation.php 1 patch
Indentation   +225 added lines, -225 removed lines patch added patch discarded remove patch
@@ -16,230 +16,230 @@
 block discarded – undo
16 16
 class Operation extends AbstractDocumentableObject
17 17
 {
18 18
 
19
-    private $tags = array();
20
-    private $summary;
21
-    private $description;
22
-    private $consumes = array();
23
-    private $produces = array();
24
-
25
-    /**
26
-     * @var IParameter[]
27
-     */
28
-    private $parameters = array();
29
-    private $responses = array();
30
-    private $schemes = array();
31
-    private $deprecated = false;
32
-    private $security = array();
33
-
34
-    /**
35
-     * @var string
36
-     */
37
-    private $operationId = null;
38
-
39
-    public function getConsumes(): array
40
-    {
41
-        return $this->consumes;
42
-    }
43
-
44
-    /**
45
-     * @param string $summary
46
-     */
47
-    public function __construct(AbstractObject $parent, $summary = null, ?Tag $tag = null)
48
-    {
49
-        parent::__construct($parent);
50
-        $this->summary = $summary;
51
-        if ($tag) {
52
-            $this->tags[] = $tag->getName();
53
-        }
54
-    }
55
-
56
-    /**
57
-     * @param string $command
58
-     * @param string $data
59
-     * @return AbstractObject|boolean
60
-     * @throws Exception
61
-     * @throws Exception
62
-     * @throws Exception
63
-     * @throws Exception
64
-     * @throws Exception
65
-     * @throws Exception
66
-     * @throws Exception
67
-     */
68
-    public function handleCommand($command, $data = null)
69
-    {
70
-        switch (strtolower($command)) {
71
-            // string
72
-            case 'summary':
73
-            case 'description':
74
-                $this->$command = $data;
75
-                return $this;
76
-
77
-            // string[]
78
-            case 'tags':
79
-            case 'schemes':
80
-                $this->$command = array_merge($this->$command, self::wordSplit($data));
81
-                return $this;
82
-
83
-            // MIME[]
84
-            case 'consumes':
85
-            case 'produces':
86
-                $this->$command = array_merge($this->$command, self::translateMimeTypes(self::wordSplit($data)));
87
-                return $this;
88
-
89
-            // boolean
90
-            case 'deprecated':
91
-                $this->deprecated = true;
92
-                return $this;
93
-
94
-            case 'error':
95
-                $code = self::wordShift($data);
96
-                $reasoncode = Response::getCode($code);
97
-                if ($reasoncode === null) {
98
-                    throw new Exception("Invalid error code: '$code'");
99
-                }
100
-                $description = $data;
101
-                $Error = new Error($this, $reasoncode, $description);
102
-                $this->responses[$reasoncode] = $Error;
103
-                return $Error;
104
-
105
-            case 'errors':
106
-                foreach (self::wordSplit($data) as $code) {
107
-                    $reasoncode = Response::getCode($code);
108
-                    if ($reasoncode === null) {
109
-                        throw new Exception("Invalid error code: '$code'");
110
-                    }
111
-                    $this->responses[$reasoncode] = new Error($this, $reasoncode);
112
-                }
113
-                return $this;
114
-
115
-            case 'path':
116
-            case 'query':
117
-            case 'query?':
118
-            case 'header':
119
-            case 'header?':
120
-            case 'form':
121
-            case 'form?':
122
-                $in = rtrim($command, '?');
123
-                $parameter = new Parameter($this, $in, $data, substr($command, -1) !== '?');
124
-                $this->parameters[$parameter->getName()] = $parameter;
125
-                return $parameter;
126
-
127
-            case 'body':
128
-            case 'body?':
129
-                $parameter = new BodyParameter($this, $data, substr($command, -1) !== '?');
130
-                $this->parameters[$parameter->getName()] = $parameter;
131
-                return $parameter;
132
-
133
-            case 'param':
134
-            case 'parameter':
135
-                $parameter = new ParameterReference($this, $data);
136
-                $this->parameters[$parameter->getName()] = $parameter;
137
-                return $this;
138
-
139
-            case 'response':
140
-                $code = self::wordShift($data);
141
-                $reasoncode = Response::getCode($code);
142
-                if ($reasoncode === null) {
143
-                    $reference = $code;
144
-                    $code = self::wordShift($data);
145
-                    $reasoncode = Response::getCode($code);
146
-                    if ($reasoncode === null) {
147
-                        throw new Exception("Invalid response code: '$reference'");
148
-                    }
149
-                    $this->responses[$reasoncode] = new ResponseReference($this, $reference);
150
-                    return $this;
151
-                }
152
-
153
-                $definition = self::wordShift($data);
154
-                $description = $data;
155
-                $Response = new Response($this, $reasoncode, $definition, $description);
156
-                $this->responses[$reasoncode] = $Response;
157
-                return $Response;
158
-
159
-            case 'require':
160
-                $name = self::wordShift($data);
161
-                if (empty($name)) {
162
-                    throw new Exception('Empty security requirement name');
163
-                }
164
-                $scopes = self::wordSplit($data);
165
-                sort($scopes);
166
-                $this->security[] = array(
167
-                    $name => empty($scopes) ? array() : $scopes,
168
-                );
169
-                return $this;
170
-
171
-            case 'id':
172
-                $operationId = self::trim($data);
173
-                if ($this->getSwagger()->hasOperationId($operationId)) {
174
-                    throw new Exception("Duplicate operation id '{$operationId}'");
175
-                }
176
-                $this->operationId = $operationId;
177
-                return $this;
178
-        }
179
-
180
-        return parent::handleCommand($command, $data);
181
-    }
182
-
183
-    /**
184
-     * @throws Exception
185
-     */
186
-    public function toArray(): array
187
-    {
188
-        if (empty($this->responses)) {
189
-            throw new Exception('No response defined for operation');
190
-        }
191
-        ksort($this->responses);
192
-
193
-        $tags = array_unique($this->tags);
194
-        sort($tags);
195
-
196
-        $schemes = array_unique($this->schemes);
197
-        sort($schemes);
198
-
199
-        $consumes = array_unique($this->consumes);
200
-        sort($consumes);
201
-
202
-        $produces = array_unique($this->produces);
203
-        sort($produces);
204
-
205
-        foreach ($this->security as $security) {
206
-            foreach ($security as $name => $scope) {
207
-                if ($this->getSwagger()->getSecurity($name) === false) {
208
-                    throw new Exception("Required security scheme not defined: '{$name}'");
209
-                }
210
-            }
211
-        }
212
-
213
-        $parameters = $this->parameters ? array_values($this->parameters) : null;
214
-
215
-        return self::arrayFilterNull(array_merge(array(
216
-            'deprecated' => $this->deprecated ? true : null,
217
-            'tags' => $tags,
218
-            'summary' => empty($this->summary) ? null : $this->summary,
219
-            'description' => empty($this->description) ? null : $this->description,
220
-            'operationId' => $this->operationId,
221
-            'consumes' => $consumes,
222
-            'produces' => $produces,
223
-            'parameters' => $parameters ? self::objectsToArray($parameters) : null,
224
-            'schemes' => $schemes,
225
-            'responses' => $this->responses ? self::objectsToArray($this->responses) : null,
226
-            'security' => $this->security,
227
-        ), parent::toArray()));
228
-    }
229
-
230
-    /**
231
-     * Return the operation ID
232
-     *
233
-     * @return string
234
-     */
235
-    public function getId()
236
-    {
237
-        return $this->operationId;
238
-    }
239
-
240
-    public function __toString()
241
-    {
242
-        return __CLASS__ . ' ' . $this->summary;
243
-    }
19
+	private $tags = array();
20
+	private $summary;
21
+	private $description;
22
+	private $consumes = array();
23
+	private $produces = array();
24
+
25
+	/**
26
+	 * @var IParameter[]
27
+	 */
28
+	private $parameters = array();
29
+	private $responses = array();
30
+	private $schemes = array();
31
+	private $deprecated = false;
32
+	private $security = array();
33
+
34
+	/**
35
+	 * @var string
36
+	 */
37
+	private $operationId = null;
38
+
39
+	public function getConsumes(): array
40
+	{
41
+		return $this->consumes;
42
+	}
43
+
44
+	/**
45
+	 * @param string $summary
46
+	 */
47
+	public function __construct(AbstractObject $parent, $summary = null, ?Tag $tag = null)
48
+	{
49
+		parent::__construct($parent);
50
+		$this->summary = $summary;
51
+		if ($tag) {
52
+			$this->tags[] = $tag->getName();
53
+		}
54
+	}
55
+
56
+	/**
57
+	 * @param string $command
58
+	 * @param string $data
59
+	 * @return AbstractObject|boolean
60
+	 * @throws Exception
61
+	 * @throws Exception
62
+	 * @throws Exception
63
+	 * @throws Exception
64
+	 * @throws Exception
65
+	 * @throws Exception
66
+	 * @throws Exception
67
+	 */
68
+	public function handleCommand($command, $data = null)
69
+	{
70
+		switch (strtolower($command)) {
71
+			// string
72
+			case 'summary':
73
+			case 'description':
74
+				$this->$command = $data;
75
+				return $this;
76
+
77
+			// string[]
78
+			case 'tags':
79
+			case 'schemes':
80
+				$this->$command = array_merge($this->$command, self::wordSplit($data));
81
+				return $this;
82
+
83
+			// MIME[]
84
+			case 'consumes':
85
+			case 'produces':
86
+				$this->$command = array_merge($this->$command, self::translateMimeTypes(self::wordSplit($data)));
87
+				return $this;
88
+
89
+			// boolean
90
+			case 'deprecated':
91
+				$this->deprecated = true;
92
+				return $this;
93
+
94
+			case 'error':
95
+				$code = self::wordShift($data);
96
+				$reasoncode = Response::getCode($code);
97
+				if ($reasoncode === null) {
98
+					throw new Exception("Invalid error code: '$code'");
99
+				}
100
+				$description = $data;
101
+				$Error = new Error($this, $reasoncode, $description);
102
+				$this->responses[$reasoncode] = $Error;
103
+				return $Error;
104
+
105
+			case 'errors':
106
+				foreach (self::wordSplit($data) as $code) {
107
+					$reasoncode = Response::getCode($code);
108
+					if ($reasoncode === null) {
109
+						throw new Exception("Invalid error code: '$code'");
110
+					}
111
+					$this->responses[$reasoncode] = new Error($this, $reasoncode);
112
+				}
113
+				return $this;
114
+
115
+			case 'path':
116
+			case 'query':
117
+			case 'query?':
118
+			case 'header':
119
+			case 'header?':
120
+			case 'form':
121
+			case 'form?':
122
+				$in = rtrim($command, '?');
123
+				$parameter = new Parameter($this, $in, $data, substr($command, -1) !== '?');
124
+				$this->parameters[$parameter->getName()] = $parameter;
125
+				return $parameter;
126
+
127
+			case 'body':
128
+			case 'body?':
129
+				$parameter = new BodyParameter($this, $data, substr($command, -1) !== '?');
130
+				$this->parameters[$parameter->getName()] = $parameter;
131
+				return $parameter;
132
+
133
+			case 'param':
134
+			case 'parameter':
135
+				$parameter = new ParameterReference($this, $data);
136
+				$this->parameters[$parameter->getName()] = $parameter;
137
+				return $this;
138
+
139
+			case 'response':
140
+				$code = self::wordShift($data);
141
+				$reasoncode = Response::getCode($code);
142
+				if ($reasoncode === null) {
143
+					$reference = $code;
144
+					$code = self::wordShift($data);
145
+					$reasoncode = Response::getCode($code);
146
+					if ($reasoncode === null) {
147
+						throw new Exception("Invalid response code: '$reference'");
148
+					}
149
+					$this->responses[$reasoncode] = new ResponseReference($this, $reference);
150
+					return $this;
151
+				}
152
+
153
+				$definition = self::wordShift($data);
154
+				$description = $data;
155
+				$Response = new Response($this, $reasoncode, $definition, $description);
156
+				$this->responses[$reasoncode] = $Response;
157
+				return $Response;
158
+
159
+			case 'require':
160
+				$name = self::wordShift($data);
161
+				if (empty($name)) {
162
+					throw new Exception('Empty security requirement name');
163
+				}
164
+				$scopes = self::wordSplit($data);
165
+				sort($scopes);
166
+				$this->security[] = array(
167
+					$name => empty($scopes) ? array() : $scopes,
168
+				);
169
+				return $this;
170
+
171
+			case 'id':
172
+				$operationId = self::trim($data);
173
+				if ($this->getSwagger()->hasOperationId($operationId)) {
174
+					throw new Exception("Duplicate operation id '{$operationId}'");
175
+				}
176
+				$this->operationId = $operationId;
177
+				return $this;
178
+		}
179
+
180
+		return parent::handleCommand($command, $data);
181
+	}
182
+
183
+	/**
184
+	 * @throws Exception
185
+	 */
186
+	public function toArray(): array
187
+	{
188
+		if (empty($this->responses)) {
189
+			throw new Exception('No response defined for operation');
190
+		}
191
+		ksort($this->responses);
192
+
193
+		$tags = array_unique($this->tags);
194
+		sort($tags);
195
+
196
+		$schemes = array_unique($this->schemes);
197
+		sort($schemes);
198
+
199
+		$consumes = array_unique($this->consumes);
200
+		sort($consumes);
201
+
202
+		$produces = array_unique($this->produces);
203
+		sort($produces);
204
+
205
+		foreach ($this->security as $security) {
206
+			foreach ($security as $name => $scope) {
207
+				if ($this->getSwagger()->getSecurity($name) === false) {
208
+					throw new Exception("Required security scheme not defined: '{$name}'");
209
+				}
210
+			}
211
+		}
212
+
213
+		$parameters = $this->parameters ? array_values($this->parameters) : null;
214
+
215
+		return self::arrayFilterNull(array_merge(array(
216
+			'deprecated' => $this->deprecated ? true : null,
217
+			'tags' => $tags,
218
+			'summary' => empty($this->summary) ? null : $this->summary,
219
+			'description' => empty($this->description) ? null : $this->description,
220
+			'operationId' => $this->operationId,
221
+			'consumes' => $consumes,
222
+			'produces' => $produces,
223
+			'parameters' => $parameters ? self::objectsToArray($parameters) : null,
224
+			'schemes' => $schemes,
225
+			'responses' => $this->responses ? self::objectsToArray($this->responses) : null,
226
+			'security' => $this->security,
227
+		), parent::toArray()));
228
+	}
229
+
230
+	/**
231
+	 * Return the operation ID
232
+	 *
233
+	 * @return string
234
+	 */
235
+	public function getId()
236
+	{
237
+		return $this->operationId;
238
+	}
239
+
240
+	public function __toString()
241
+	{
242
+		return __CLASS__ . ' ' . $this->summary;
243
+	}
244 244
 
245 245
 }
Please login to merge, or discard this patch.