Completed
Push — master ( fcb010...ba73f2 )
by Martijn
19s
created
tests/output/OutputTest.php 1 patch
Indentation   +52 added lines, -52 removed lines patch added patch discarded remove patch
@@ -7,57 +7,57 @@
 block discarded – undo
7 7
 class OutputTest extends TestCase
8 8
 {
9 9
 
10
-    /**
11
-     * @covers       \SwaggerGen\Exception::__construct
12
-     * @dataProvider provideAllCases
13
-     */
14
-    public function testAllCases($name, $files, $expected)
15
-    {
16
-        $SwaggerGen = new SwaggerGen('example.com', '/base');
17
-        $actual = $SwaggerGen->getSwagger($files, array(), SwaggerGen::FORMAT_JSON);
18
-        $this->assertJsonStringEqualsJsonString($expected, $this->normalizeJson($actual), $name);
19
-    }
20
-
21
-    /**
22
-     * Normalizes and pretty-prints json (whitespace mostly)
23
-     *
24
-     * This is useful to get better diff results when assertions fail.
25
-     *
26
-     * @param string $json
27
-     * @return string
28
-     */
29
-    private function normalizeJson($json)
30
-    {
31
-        return json_encode(
32
-            json_decode($json),
33
-            PHP_VERSION_ID >= 50400 ? constant('JSON_PRETTY_PRINT') : 0
34
-        );
35
-    }
36
-
37
-    public function provideAllCases()
38
-    {
39
-        $cases = array();
40
-
41
-        foreach (glob(__DIR__ . '/*', GLOB_ONLYDIR) as $dir) {
42
-            $path = realpath($dir);
43
-            $json = $this->normalizeJson(file_get_contents($path . '/expected.json'));
44
-
45
-            $files = array();
46
-            if (file_exists($path . '/source.php')) {
47
-                $files[] = $path . '/source.php';
48
-            }
49
-            if (file_exists($path . '/source.txt')) {
50
-                $files[] = $path . '/source.txt';
51
-            }
52
-
53
-            $cases[] = array(
54
-                basename($dir),
55
-                $files,
56
-                $json
57
-            );
58
-        }
59
-
60
-        return $cases;
61
-    }
10
+	/**
11
+	 * @covers       \SwaggerGen\Exception::__construct
12
+	 * @dataProvider provideAllCases
13
+	 */
14
+	public function testAllCases($name, $files, $expected)
15
+	{
16
+		$SwaggerGen = new SwaggerGen('example.com', '/base');
17
+		$actual = $SwaggerGen->getSwagger($files, array(), SwaggerGen::FORMAT_JSON);
18
+		$this->assertJsonStringEqualsJsonString($expected, $this->normalizeJson($actual), $name);
19
+	}
20
+
21
+	/**
22
+	 * Normalizes and pretty-prints json (whitespace mostly)
23
+	 *
24
+	 * This is useful to get better diff results when assertions fail.
25
+	 *
26
+	 * @param string $json
27
+	 * @return string
28
+	 */
29
+	private function normalizeJson($json)
30
+	{
31
+		return json_encode(
32
+			json_decode($json),
33
+			PHP_VERSION_ID >= 50400 ? constant('JSON_PRETTY_PRINT') : 0
34
+		);
35
+	}
36
+
37
+	public function provideAllCases()
38
+	{
39
+		$cases = array();
40
+
41
+		foreach (glob(__DIR__ . '/*', GLOB_ONLYDIR) as $dir) {
42
+			$path = realpath($dir);
43
+			$json = $this->normalizeJson(file_get_contents($path . '/expected.json'));
44
+
45
+			$files = array();
46
+			if (file_exists($path . '/source.php')) {
47
+				$files[] = $path . '/source.php';
48
+			}
49
+			if (file_exists($path . '/source.txt')) {
50
+				$files[] = $path . '/source.txt';
51
+			}
52
+
53
+			$cases[] = array(
54
+				basename($dir),
55
+				$files,
56
+				$json
57
+			);
58
+		}
59
+
60
+		return $cases;
61
+	}
62 62
 
63 63
 }
Please login to merge, or discard this patch.
tests/StatementTest.php 1 patch
Indentation   +22 added lines, -22 removed lines patch added patch discarded remove patch
@@ -6,29 +6,29 @@
 block discarded – undo
6 6
 class StatementTest extends TestCase
7 7
 {
8 8
 
9
-    /**
10
-     * @covers \SwaggerGen\Statement::__construct
11
-     */
12
-    public function testConstructor()
13
-    {
14
-        $object = new \SwaggerGen\Statement('command', 'some data');
9
+	/**
10
+	 * @covers \SwaggerGen\Statement::__construct
11
+	 */
12
+	public function testConstructor()
13
+	{
14
+		$object = new \SwaggerGen\Statement('command', 'some data');
15 15
 
16
-        $this->assertInstanceOf('\SwaggerGen\Statement', $object);
17
-        $this->assertSame('command', $object->getCommand());
18
-        $this->assertSame('some data', $object->getData());
19
-    }
16
+		$this->assertInstanceOf('\SwaggerGen\Statement', $object);
17
+		$this->assertSame('command', $object->getCommand());
18
+		$this->assertSame('some data', $object->getData());
19
+	}
20 20
 
21
-    /**
22
-     * @covers \SwaggerGen\Statement::__construct
23
-     */
24
-    public function testConstructor_File()
25
-    {
26
-        $object = new \SwaggerGen\Statement('command', 'some data', 'file', 123);
27
-        $this->assertInstanceOf('\SwaggerGen\Statement', $object);
21
+	/**
22
+	 * @covers \SwaggerGen\Statement::__construct
23
+	 */
24
+	public function testConstructor_File()
25
+	{
26
+		$object = new \SwaggerGen\Statement('command', 'some data', 'file', 123);
27
+		$this->assertInstanceOf('\SwaggerGen\Statement', $object);
28 28
 
29
-        $this->assertSame('command', $object->getCommand());
30
-        $this->assertSame('some data', $object->getData());
31
-        $this->assertSame('file', $object->getFile());
32
-        $this->assertSame(123, $object->getLine());
33
-    }
29
+		$this->assertSame('command', $object->getCommand());
30
+		$this->assertSame('some data', $object->getData());
31
+		$this->assertSame('file', $object->getFile());
32
+		$this->assertSame(123, $object->getLine());
33
+	}
34 34
 }
Please login to merge, or discard this patch.
tests/ExceptionTest.php 1 patch
Indentation   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -6,34 +6,34 @@
 block discarded – undo
6 6
 class ExceptionTest extends TestCase
7 7
 {
8 8
 
9
-    /**
10
-     * @covers \SwaggerGen\Exception::__construct
11
-     */
12
-    public function testConstructor0()
13
-    {
14
-        $this->expectException('\SwaggerGen\Exception');
15
-
16
-        throw new \SwaggerGen\Exception('', 0);
17
-    }
18
-
19
-    /**
20
-     * @covers \SwaggerGen\Exception::__construct
21
-     */
22
-    public function testConstructor1()
23
-    {
24
-        $this->expectException('\SwaggerGen\Exception', 'This is a message');
25
-
26
-        throw new \SwaggerGen\Exception('This is a message', 0);
27
-    }
28
-
29
-    /**
30
-     * @covers \SwaggerGen\Exception::__construct
31
-     */
32
-    public function testConstructor2()
33
-    {
34
-        $this->expectException('\SwaggerGen\Exception', 'This is a message', 1234);
35
-
36
-        throw new \SwaggerGen\Exception('This is a message', 1234);
37
-    }
9
+	/**
10
+	 * @covers \SwaggerGen\Exception::__construct
11
+	 */
12
+	public function testConstructor0()
13
+	{
14
+		$this->expectException('\SwaggerGen\Exception');
15
+
16
+		throw new \SwaggerGen\Exception('', 0);
17
+	}
18
+
19
+	/**
20
+	 * @covers \SwaggerGen\Exception::__construct
21
+	 */
22
+	public function testConstructor1()
23
+	{
24
+		$this->expectException('\SwaggerGen\Exception', 'This is a message');
25
+
26
+		throw new \SwaggerGen\Exception('This is a message', 0);
27
+	}
28
+
29
+	/**
30
+	 * @covers \SwaggerGen\Exception::__construct
31
+	 */
32
+	public function testConstructor2()
33
+	{
34
+		$this->expectException('\SwaggerGen\Exception', 'This is a message', 1234);
35
+
36
+		throw new \SwaggerGen\Exception('This is a message', 1234);
37
+	}
38 38
 
39 39
 }
Please login to merge, or discard this patch.
tests/SwaggerGenTest.php 2 patches
Indentation   +124 added lines, -124 removed lines patch added patch discarded remove patch
@@ -5,94 +5,94 @@  discard block
 block discarded – undo
5 5
 class SwaggerGenTest extends \PHPUnit\Framework\TestCase
6 6
 {
7 7
 
8
-    /**
9
-     * @covers \SwaggerGen\SwaggerGen::__construct
10
-     */
11
-    public function testConstructor_Empty()
12
-    {
13
-        $object = new SwaggerGen();
14
-        $this->assertInstanceof('\SwaggerGen\SwaggerGen', $object);
15
-
16
-        $this->expectException('\SwaggerGen\Exception', 'No path defined');
17
-        $object->getSwagger(array());
18
-    }
19
-
20
-    /**
21
-     * @covers \SwaggerGen\SwaggerGen::getSwagger
22
-     */
23
-    public function testGetSwagger_ShortestPossible()
24
-    {
25
-        $object = new SwaggerGen();
26
-        $this->assertInstanceof('\SwaggerGen\SwaggerGen', $object);
27
-
28
-        $array = $object->getSwagger(array('
8
+	/**
9
+	 * @covers \SwaggerGen\SwaggerGen::__construct
10
+	 */
11
+	public function testConstructor_Empty()
12
+	{
13
+		$object = new SwaggerGen();
14
+		$this->assertInstanceof('\SwaggerGen\SwaggerGen', $object);
15
+
16
+		$this->expectException('\SwaggerGen\Exception', 'No path defined');
17
+		$object->getSwagger(array());
18
+	}
19
+
20
+	/**
21
+	 * @covers \SwaggerGen\SwaggerGen::getSwagger
22
+	 */
23
+	public function testGetSwagger_ShortestPossible()
24
+	{
25
+		$object = new SwaggerGen();
26
+		$this->assertInstanceof('\SwaggerGen\SwaggerGen', $object);
27
+
28
+		$array = $object->getSwagger(array('
29 29
 			endpoint
30 30
 			method GET
31 31
 			response 202
32 32
 		'));
33 33
 
34
-        $this->assertSame('{"swagger":2,"info":{"title":"undefined","version":0},"paths":{"\/":{"get":{"responses":{"202":{"description":"Accepted"}}}}}}', json_encode($array, JSON_NUMERIC_CHECK));
35
-    }
34
+		$this->assertSame('{"swagger":2,"info":{"title":"undefined","version":0},"paths":{"\/":{"get":{"responses":{"202":{"description":"Accepted"}}}}}}', json_encode($array, JSON_NUMERIC_CHECK));
35
+	}
36
+
37
+	/**
38
+	 * @covers \SwaggerGen\SwaggerGen::__construct
39
+	 */
40
+	public function testConstructor_BadContext()
41
+	{
42
+		$object = new SwaggerGen();
43
+		$this->assertInstanceof('\SwaggerGen\SwaggerGen', $object);
36 44
 
37
-    /**
38
-     * @covers \SwaggerGen\SwaggerGen::__construct
39
-     */
40
-    public function testConstructor_BadContext()
41
-    {
42
-        $object = new SwaggerGen();
43
-        $this->assertInstanceof('\SwaggerGen\SwaggerGen', $object);
44
-
45
-        $this->expectException('\SwaggerGen\StatementException', 'Invalid error code: \'\'');
46
-        try {
47
-            $object->getSwagger(array('
45
+		$this->expectException('\SwaggerGen\StatementException', 'Invalid error code: \'\'');
46
+		try {
47
+			$object->getSwagger(array('
48 48
 				endpoint
49 49
 				method GET
50 50
 				error
51 51
 			'));
52
-        } catch (\SwaggerGen\StatementException $e) {
53
-            $this->assertSame("Invalid error code: ''", $e->getMessage());
54
-            $this->assertSame(3, $e->getStatement()->getLine());
55
-
56
-            throw $e; // rethrow to satisfy expected exception check
57
-        }
58
-    }
59
-
60
-    /**
61
-     * @covers \SwaggerGen\SwaggerGen::getSwagger
62
-     */
63
-    public function testGetSwagger_JSON()
64
-    {
65
-        $object = new SwaggerGen();
66
-        $this->assertInstanceof('\SwaggerGen\SwaggerGen', $object);
67
-
68
-        $output = $object->getSwagger(array('
52
+		} catch (\SwaggerGen\StatementException $e) {
53
+			$this->assertSame("Invalid error code: ''", $e->getMessage());
54
+			$this->assertSame(3, $e->getStatement()->getLine());
55
+
56
+			throw $e; // rethrow to satisfy expected exception check
57
+		}
58
+	}
59
+
60
+	/**
61
+	 * @covers \SwaggerGen\SwaggerGen::getSwagger
62
+	 */
63
+	public function testGetSwagger_JSON()
64
+	{
65
+		$object = new SwaggerGen();
66
+		$this->assertInstanceof('\SwaggerGen\SwaggerGen', $object);
67
+
68
+		$output = $object->getSwagger(array('
69 69
 			endpoint
70 70
 			method GET
71 71
 			response 202
72 72
 		'), array(), SwaggerGen::FORMAT_JSON);
73 73
 
74
-        $this->assertSame('{"swagger":"2.0","info":{"title":"undefined","version":"0"},"paths":{"\/":{"get":{"responses":{"202":{"description":"Accepted"}}}}}}', $output);
75
-    }
74
+		$this->assertSame('{"swagger":"2.0","info":{"title":"undefined","version":"0"},"paths":{"\/":{"get":{"responses":{"202":{"description":"Accepted"}}}}}}', $output);
75
+	}
76 76
 
77
-    /**
78
-     * @covers \SwaggerGen\SwaggerGen::getSwagger
79
-     */
80
-    public function testGetSwagger_JSON_Pretty()
81
-    {
82
-        if (!defined('JSON_PRETTY_PRINT')) {
83
-            $this->markTestSkipped('JSON_PRETTY_PRINT available since PHP 5.4.0');
84
-        }
77
+	/**
78
+	 * @covers \SwaggerGen\SwaggerGen::getSwagger
79
+	 */
80
+	public function testGetSwagger_JSON_Pretty()
81
+	{
82
+		if (!defined('JSON_PRETTY_PRINT')) {
83
+			$this->markTestSkipped('JSON_PRETTY_PRINT available since PHP 5.4.0');
84
+		}
85 85
 
86
-        $object = new SwaggerGen();
87
-        $this->assertInstanceof('\SwaggerGen\SwaggerGen', $object);
86
+		$object = new SwaggerGen();
87
+		$this->assertInstanceof('\SwaggerGen\SwaggerGen', $object);
88 88
 
89
-        $output = $object->getSwagger(array('
89
+		$output = $object->getSwagger(array('
90 90
 			endpoint
91 91
 			method GET
92 92
 			response 202
93 93
 		'), array(), SwaggerGen::FORMAT_JSON_PRETTY);
94 94
 
95
-        $this->assertStringEqualsStringIgnoringLineEndings(<<<JSON
95
+		$this->assertStringEqualsStringIgnoringLineEndings(<<<JSON
96 96
 {
97 97
     "swagger": "2.0",
98 98
     "info": {
@@ -112,26 +112,26 @@  discard block
 block discarded – undo
112 112
     }
113 113
 }
114 114
 JSON, $output);
115
-    }
116
-
117
-    /**
118
-     * @covers \SwaggerGen\SwaggerGen::getSwagger
119
-     */
120
-    public function testGetSwagger_YAML()
121
-    {
122
-        if (!extension_loaded('yaml')) {
123
-            $this->markTestSkipped('The YAML extension is not available.');
124
-        } else {
125
-            $object = new SwaggerGen();
126
-            $this->assertInstanceof('\SwaggerGen\SwaggerGen', $object);
127
-
128
-            $output = $object->getSwagger(array('
115
+	}
116
+
117
+	/**
118
+	 * @covers \SwaggerGen\SwaggerGen::getSwagger
119
+	 */
120
+	public function testGetSwagger_YAML()
121
+	{
122
+		if (!extension_loaded('yaml')) {
123
+			$this->markTestSkipped('The YAML extension is not available.');
124
+		} else {
125
+			$object = new SwaggerGen();
126
+			$this->assertInstanceof('\SwaggerGen\SwaggerGen', $object);
127
+
128
+			$output = $object->getSwagger(array('
129 129
 				endpoint
130 130
 				method GET
131 131
 				response 202
132 132
 			'), array(), SwaggerGen::FORMAT_YAML);
133 133
 
134
-            $this->assertSame('---
134
+			$this->assertSame('---
135 135
 swagger: "2.0"
136 136
 info:
137 137
   title: undefined
@@ -144,18 +144,18 @@  discard block
 block discarded – undo
144 144
           description: Accepted
145 145
 ...
146 146
 ', $output);
147
-        }
148
-    }
149
-
150
-    /**
151
-     * @covers \SwaggerGen\SwaggerGen::define
152
-     */
153
-    public function testDefine_NotDefined()
154
-    {
155
-        $object = new SwaggerGen();
156
-        $this->assertInstanceof('\SwaggerGen\SwaggerGen', $object);
157
-
158
-        $array = $object->getSwagger(array('
147
+		}
148
+	}
149
+
150
+	/**
151
+	 * @covers \SwaggerGen\SwaggerGen::define
152
+	 */
153
+	public function testDefine_NotDefined()
154
+	{
155
+		$object = new SwaggerGen();
156
+		$this->assertInstanceof('\SwaggerGen\SwaggerGen', $object);
157
+
158
+		$array = $object->getSwagger(array('
159 159
 			endpoint
160 160
 			method GET
161 161
 			if d
@@ -164,20 +164,20 @@  discard block
 block discarded – undo
164 164
 			response 204
165 165
 			endif
166 166
 		'));
167
-        $this->assertArrayNotHasKey(202, $array['paths']['/']['get']['responses']);
168
-        $this->assertArrayHasKey(204, $array['paths']['/']['get']['responses']);
169
-    }
170
-
171
-    /**
172
-     * @covers \SwaggerGen\SwaggerGen::define
173
-     */
174
-    public function testDefine_Defined()
175
-    {
176
-        $object = new SwaggerGen();
177
-        $this->assertInstanceof('\SwaggerGen\SwaggerGen', $object);
178
-
179
-        $object->define('d');
180
-        $array = $object->getSwagger(array('
167
+		$this->assertArrayNotHasKey(202, $array['paths']['/']['get']['responses']);
168
+		$this->assertArrayHasKey(204, $array['paths']['/']['get']['responses']);
169
+	}
170
+
171
+	/**
172
+	 * @covers \SwaggerGen\SwaggerGen::define
173
+	 */
174
+	public function testDefine_Defined()
175
+	{
176
+		$object = new SwaggerGen();
177
+		$this->assertInstanceof('\SwaggerGen\SwaggerGen', $object);
178
+
179
+		$object->define('d');
180
+		$array = $object->getSwagger(array('
181 181
 			endpoint
182 182
 			method GET
183 183
 			if d
@@ -186,21 +186,21 @@  discard block
 block discarded – undo
186 186
 			response 204
187 187
 			endif
188 188
 		'));
189
-        $this->assertArrayHasKey(202, $array['paths']['/']['get']['responses']);
190
-        $this->assertArrayNotHasKey(204, $array['paths']['/']['get']['responses']);
191
-    }
192
-
193
-    /**
194
-     * @covers \SwaggerGen\SwaggerGen::define
195
-     */
196
-    public function testUndefine()
197
-    {
198
-        $object = new SwaggerGen();
199
-        $this->assertInstanceof('\SwaggerGen\SwaggerGen', $object);
200
-
201
-        $object->define('d');
202
-        $object->undefine('d');
203
-        $array = $object->getSwagger(array('
189
+		$this->assertArrayHasKey(202, $array['paths']['/']['get']['responses']);
190
+		$this->assertArrayNotHasKey(204, $array['paths']['/']['get']['responses']);
191
+	}
192
+
193
+	/**
194
+	 * @covers \SwaggerGen\SwaggerGen::define
195
+	 */
196
+	public function testUndefine()
197
+	{
198
+		$object = new SwaggerGen();
199
+		$this->assertInstanceof('\SwaggerGen\SwaggerGen', $object);
200
+
201
+		$object->define('d');
202
+		$object->undefine('d');
203
+		$array = $object->getSwagger(array('
204 204
 			endpoint
205 205
 			method GET
206 206
 			if d
@@ -209,7 +209,7 @@  discard block
 block discarded – undo
209 209
 			response 204
210 210
 			endif
211 211
 		'));
212
-        $this->assertArrayNotHasKey(202, $array['paths']['/']['get']['responses']);
213
-        $this->assertArrayHasKey(204, $array['paths']['/']['get']['responses']);
214
-    }
212
+		$this->assertArrayNotHasKey(202, $array['paths']['/']['get']['responses']);
213
+		$this->assertArrayHasKey(204, $array['paths']['/']['get']['responses']);
214
+	}
215 215
 }
Please login to merge, or discard this patch.
Upper-Lower-Casing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -111,7 +111,7 @@
 block discarded – undo
111 111
         }
112 112
     }
113 113
 }
114
-JSON, $output);
114
+json, $output);
115 115
     }
116 116
 
117 117
     /**
Please login to merge, or discard this patch.
SwaggerGen/Parser/AbstractPreprocessor.php 1 patch
Indentation   +119 added lines, -119 removed lines patch added patch discarded remove patch
@@ -13,123 +13,123 @@
 block discarded – undo
13 13
 abstract class AbstractPreprocessor
14 14
 {
15 15
 
16
-    private $defines = [];
17
-    private $stack = [];
18
-
19
-    public function __construct()
20
-    {
21
-        $this->resetDefines();
22
-    }
23
-
24
-    public function resetDefines()
25
-    {
26
-        $this->defines = [];
27
-    }
28
-
29
-    public function addDefines(array $defines)
30
-    {
31
-        $this->defines = array_merge($this->defines, $defines);
32
-    }
33
-
34
-    public function define($name, $value = 1)
35
-    {
36
-        $this->defines[$name] = $value;
37
-    }
38
-
39
-    public function undefine($name)
40
-    {
41
-        unset($this->defines[$name]);
42
-    }
43
-
44
-    public function preprocessFile($filename)
45
-    {
46
-        return $this->preprocess(file_get_contents($filename));
47
-    }
48
-
49
-    public function preprocess($content)
50
-    {
51
-        $this->stack = [];
52
-
53
-        return $this->parseContent($content);
54
-    }
55
-
56
-    abstract protected function parseContent($content);
57
-
58
-    protected function handle($command, $expression)
59
-    {
60
-        switch (strtolower($command)) {
61
-            case 'if':
62
-                $name = self::wordShift($expression);
63
-                $state = $this->getState();
64
-                if (empty($expression)) {
65
-                    $this->stack[] = $state && !empty($this->defines[$name]);
66
-                } else {
67
-                    $this->stack[] = $state && isset($this->defines[$name]) && $this->defines[$name] == $expression;
68
-                }
69
-                break;
70
-
71
-            case 'ifdef':
72
-                $this->stack[] = $this->getState() && isset($this->defines[$expression]);
73
-                break;
74
-
75
-            case 'ifndef':
76
-                $this->stack[] = $this->getState() && !isset($this->defines[$expression]);
77
-                break;
78
-
79
-            case 'else':
80
-                $state = $this->getState();
81
-                array_pop($this->stack);
82
-                $this->stack[] = !$state;
83
-                break;
84
-
85
-            case 'elif':
86
-                $name = self::wordShift($expression);
87
-                $state = $this->getState();
88
-                array_pop($this->stack);
89
-                if (empty($expression)) {
90
-                    $this->stack[] = !$state && !empty($this->defines[$name]);
91
-                } else {
92
-                    $this->stack[] = !$state && isset($this->defines[$name]) && $this->defines[$name] == $expression;
93
-                }
94
-                break;
95
-
96
-            case 'define':
97
-                $name = self::wordShift($expression);
98
-                $this->defines[$name] = $expression;
99
-                break;
100
-
101
-            case 'undef':
102
-                unset($this->defines[$expression]);
103
-                break;
104
-
105
-            case 'endif':
106
-                array_pop($this->stack);
107
-                break;
108
-
109
-            default:
110
-                return false;
111
-        }
112
-
113
-        return true;
114
-    }
115
-
116
-    /**
117
-     * Get the first word from a string and remove it from the string.
118
-     *
119
-     * @param string $data
120
-     * @return boolean|string
121
-     */
122
-    private static function wordShift(&$data)
123
-    {
124
-        if (preg_match('~^(\S+)\s*(.*)$~', $data, $matches) === 1) {
125
-            $data = $matches[2];
126
-            return $matches[1];
127
-        }
128
-        return false;
129
-    }
130
-
131
-    protected function getState()
132
-    {
133
-        return empty($this->stack) || end($this->stack);
134
-    }
16
+	private $defines = [];
17
+	private $stack = [];
18
+
19
+	public function __construct()
20
+	{
21
+		$this->resetDefines();
22
+	}
23
+
24
+	public function resetDefines()
25
+	{
26
+		$this->defines = [];
27
+	}
28
+
29
+	public function addDefines(array $defines)
30
+	{
31
+		$this->defines = array_merge($this->defines, $defines);
32
+	}
33
+
34
+	public function define($name, $value = 1)
35
+	{
36
+		$this->defines[$name] = $value;
37
+	}
38
+
39
+	public function undefine($name)
40
+	{
41
+		unset($this->defines[$name]);
42
+	}
43
+
44
+	public function preprocessFile($filename)
45
+	{
46
+		return $this->preprocess(file_get_contents($filename));
47
+	}
48
+
49
+	public function preprocess($content)
50
+	{
51
+		$this->stack = [];
52
+
53
+		return $this->parseContent($content);
54
+	}
55
+
56
+	abstract protected function parseContent($content);
57
+
58
+	protected function handle($command, $expression)
59
+	{
60
+		switch (strtolower($command)) {
61
+			case 'if':
62
+				$name = self::wordShift($expression);
63
+				$state = $this->getState();
64
+				if (empty($expression)) {
65
+					$this->stack[] = $state && !empty($this->defines[$name]);
66
+				} else {
67
+					$this->stack[] = $state && isset($this->defines[$name]) && $this->defines[$name] == $expression;
68
+				}
69
+				break;
70
+
71
+			case 'ifdef':
72
+				$this->stack[] = $this->getState() && isset($this->defines[$expression]);
73
+				break;
74
+
75
+			case 'ifndef':
76
+				$this->stack[] = $this->getState() && !isset($this->defines[$expression]);
77
+				break;
78
+
79
+			case 'else':
80
+				$state = $this->getState();
81
+				array_pop($this->stack);
82
+				$this->stack[] = !$state;
83
+				break;
84
+
85
+			case 'elif':
86
+				$name = self::wordShift($expression);
87
+				$state = $this->getState();
88
+				array_pop($this->stack);
89
+				if (empty($expression)) {
90
+					$this->stack[] = !$state && !empty($this->defines[$name]);
91
+				} else {
92
+					$this->stack[] = !$state && isset($this->defines[$name]) && $this->defines[$name] == $expression;
93
+				}
94
+				break;
95
+
96
+			case 'define':
97
+				$name = self::wordShift($expression);
98
+				$this->defines[$name] = $expression;
99
+				break;
100
+
101
+			case 'undef':
102
+				unset($this->defines[$expression]);
103
+				break;
104
+
105
+			case 'endif':
106
+				array_pop($this->stack);
107
+				break;
108
+
109
+			default:
110
+				return false;
111
+		}
112
+
113
+		return true;
114
+	}
115
+
116
+	/**
117
+	 * Get the first word from a string and remove it from the string.
118
+	 *
119
+	 * @param string $data
120
+	 * @return boolean|string
121
+	 */
122
+	private static function wordShift(&$data)
123
+	{
124
+		if (preg_match('~^(\S+)\s*(.*)$~', $data, $matches) === 1) {
125
+			$data = $matches[2];
126
+			return $matches[1];
127
+		}
128
+		return false;
129
+	}
130
+
131
+	protected function getState()
132
+	{
133
+		return empty($this->stack) || end($this->stack);
134
+	}
135 135
 }
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
-    protected function parseContent($content)
32
-    {
33
-        $pattern = '/@' . preg_quote($this->getPrefix(), '/') . '\\\\([a-z]+)\\s*(.*)$/';
31
+	protected function parseContent($content)
32
+	{
33
+		$pattern = '/@' . preg_quote($this->getPrefix(), '/') . '\\\\([a-z]+)\\s*(.*)$/';
34 34
 
35
-        $output_file = '';
35
+		$output_file = '';
36 36
 
37
-        foreach (token_get_all($content) as $token) {
38
-            $output = '';
37
+		foreach (token_get_all($content) as $token) {
38
+			$output = '';
39 39
 
40
-            if (is_array($token)) {
41
-                switch ($token[0]) {
42
-                    case T_DOC_COMMENT:
43
-                    case T_COMMENT:
44
-                        foreach (preg_split('/(\\R)/m', $token[1], -1, PREG_SPLIT_DELIM_CAPTURE) as $index => $line) {
45
-                            if ($index % 2) {
46
-                                $output .= $line;
47
-                            } else {
48
-                                $match = [];
49
-                                if (preg_match($pattern, $line, $match) === 1) {
50
-                                    if (!$this->handle($match[1], $match[2]) && $this->getState()) {
51
-                                        $output .= $line;
52
-                                    } else {
53
-                                        $output .= str_replace('@' . $this->getPrefix() . '\\', '@!' . $this->getPrefix() . '\\', $line);
54
-                                    }
55
-                                } else {
56
-                                    $output .= $line;
57
-                                }
58
-                            }
59
-                        }
60
-                        break;
40
+			if (is_array($token)) {
41
+				switch ($token[0]) {
42
+					case T_DOC_COMMENT:
43
+					case T_COMMENT:
44
+						foreach (preg_split('/(\\R)/m', $token[1], -1, PREG_SPLIT_DELIM_CAPTURE) as $index => $line) {
45
+							if ($index % 2) {
46
+								$output .= $line;
47
+							} else {
48
+								$match = [];
49
+								if (preg_match($pattern, $line, $match) === 1) {
50
+									if (!$this->handle($match[1], $match[2]) && $this->getState()) {
51
+										$output .= $line;
52
+									} else {
53
+										$output .= str_replace('@' . $this->getPrefix() . '\\', '@!' . $this->getPrefix() . '\\', $line);
54
+									}
55
+								} else {
56
+									$output .= $line;
57
+								}
58
+							}
59
+						}
60
+						break;
61 61
 
62
-                    default:
63
-                        $output .= $token[1];
64
-                }
65
-            } else {
66
-                $output .= $token;
67
-            }
62
+					default:
63
+						$output .= $token[1];
64
+				}
65
+			} else {
66
+				$output .= $token;
67
+			}
68 68
 
69
-            if ($this->getState()) {
70
-                $output_file .= $output;
71
-            } else {
72
-                $output_file .= '/* ' . $output . ' */';
73
-            }
74
-        }
69
+			if ($this->getState()) {
70
+				$output_file .= $output;
71
+			} else {
72
+				$output_file .= '/* ' . $output . ' */';
73
+			}
74
+		}
75 75
 
76
-        return $output_file;
77
-    }
76
+		return $output_file;
77
+	}
78 78
 
79
-    public function getPrefix()
80
-    {
81
-        return $this->prefix;
82
-    }
79
+	public function getPrefix()
80
+	{
81
+		return $this->prefix;
82
+	}
83 83
 
84
-    public function setPrefix($prefix)
85
-    {
86
-        $this->prefix = $prefix;
87
-    }
84
+	public function setPrefix($prefix)
85
+	{
86
+		$this->prefix = $prefix;
87
+	}
88 88
 
89 89
 }
Please login to merge, or discard this patch.
SwaggerGen/Parser/Php/Parser.php 1 patch
Indentation   +359 added lines, -359 removed lines patch added patch discarded remove patch
@@ -22,367 +22,367 @@
 block discarded – undo
22 22
 class Parser extends AbstractEntity implements IParser
23 23
 {
24 24
 
25
-    const COMMENT_TAG = 'rest';
25
+	const COMMENT_TAG = 'rest';
26 26
 
27 27
 // transient
28
-    /** @var Statement[] */
29
-    public $statements = [];
30
-    /**
31
-     * @var ParserClass[]
32
-     */
33
-    public $Classes = [];
34
-    /**
35
-     * @var ParserFunction[]
36
-     */
37
-    public $Functions = [];
38
-    /**
39
-     * Directories available to all parse calls
40
-     *
41
-     * @var string[]
42
-     */
43
-    protected $common_dirs = [];
28
+	/** @var Statement[] */
29
+	public $statements = [];
30
+	/**
31
+	 * @var ParserClass[]
32
+	 */
33
+	public $Classes = [];
34
+	/**
35
+	 * @var ParserFunction[]
36
+	 */
37
+	public $Functions = [];
38
+	/**
39
+	 * Directories available to all parse calls
40
+	 *
41
+	 * @var string[]
42
+	 */
43
+	protected $common_dirs = [];
44 44
 // States
45
-    private $current_file = null;
46
-    private $files_queued = [];
47
-    private $files_done = [];
48
-    private $dirs = [];
49
-    /**
50
-     * @var Statement[]|null
51
-     */
52
-    private $lastStatements = [];
53
-    /**
54
-     * @var AbstractPreprocessor
55
-     */
56
-    private $Preprocessor;
57
-
58
-    public function __construct(array $dirs = [])
59
-    {
60
-        foreach ($dirs as $dir) {
61
-            $this->common_dirs[] = realpath($dir);
62
-        }
63
-
64
-        $this->Preprocessor = new Preprocessor(self::COMMENT_TAG);
65
-    }
66
-
67
-    public function addDirs(array $dirs)
68
-    {
69
-        foreach ($dirs as $dir) {
70
-            $this->common_dirs[] = realpath($dir);
71
-        }
72
-    }
73
-
74
-    /**
75
-     * @throws Exception
76
-     */
77
-    public function parse($file, array $dirs = [], array $defines = [])
78
-    {
79
-        $this->dirs = $this->common_dirs;
80
-        foreach ($dirs as $dir) {
81
-            $this->dirs[] = realpath($dir);
82
-        }
83
-
84
-        $this->parseFiles(array($file), $defines);
85
-
86
-        // Inherit classes
87
-        foreach ($this->Classes as $Class) {
88
-            $this->inherit($Class);
89
-        }
90
-
91
-        // Expand functions with used and seen functions/methods.
92
-        foreach ($this->Classes as $Class) {
93
-            foreach ($Class->Methods as $Method) {
94
-                $Method->Statements = $this->expand($Method->Statements, $Class);
95
-            }
96
-        }
97
-
98
-        return $this->extractStatements();
99
-    }
100
-
101
-    private function parseFiles(array $files, array $defines = [])
102
-    {
103
-        $this->files_queued = $files;
104
-
105
-        $index = 0;
106
-        while (($file = array_shift($this->files_queued)) !== null) {
107
-            $file = realpath($file);
108
-
109
-            // @todo Test if this works
110
-            if (in_array($file, $this->files_done)) {
111
-                continue;
112
-            }
113
-
114
-            $this->current_file = $file;
115
-            $this->files_done[] = $file;
116
-            ++$index;
117
-
118
-            $this->Preprocessor->resetDefines();
119
-            $this->Preprocessor->addDefines($defines);
120
-            $source = $this->Preprocessor->preprocessFile($file);
121
-
122
-            $this->parseTokens($source);
123
-
124
-            if ($this->lastStatements !== null) {
125
-                $this->statements = array_merge($this->statements, $this->lastStatements);
126
-                $this->lastStatements = null;
127
-            }
128
-        }
129
-
130
-        $this->current_file = null;
131
-    }
132
-
133
-    private function parseTokens($source)
134
-    {
135
-        $mode = null;
136
-        $namespace = '';
137
-
138
-        $tokens = token_get_all($source);
139
-        $token = reset($tokens);
140
-        while ($token) {
141
-            switch ($token[0]) {
142
-                case T_NAMESPACE:
143
-                    $mode = T_NAMESPACE;
144
-                    break;
145
-
146
-                case T_NS_SEPARATOR:
147
-                case T_STRING:
148
-                    if ($mode === T_NAMESPACE) {
149
-                        $namespace .= $token[1];
150
-                    }
151
-                    break;
152
-
153
-                case ';':
154
-                    $mode = null;
155
-                    break;
156
-
157
-                case T_CLASS:
158
-                case T_INTERFACE:
159
-                    $Class = new Entity\ParserClass($this, $tokens, $this->lastStatements);
160
-                    $this->Classes[strtolower($Class->name)] = $Class;
161
-                    $this->lastStatements = null;
162
-                    break;
163
-
164
-                case T_FUNCTION:
165
-                    $Function = new ParserFunction($this, $tokens, $this->lastStatements);
166
-                    $this->Functions[strtolower($Function->name)] = $Function;
167
-                    $this->lastStatements = null;
168
-                    break;
169
-
170
-                case T_COMMENT:
171
-                    if ($this->lastStatements !== null) {
172
-                        $this->statements = array_merge($this->statements, $this->lastStatements);
173
-                        $this->lastStatements = null;
174
-                    }
175
-                    $Statements = $this->tokenToStatements($token);
176
-                    $this->queueClassesFromComments($Statements);
177
-                    $this->statements = array_merge($this->statements, $Statements);
178
-                    break;
179
-
180
-                case T_DOC_COMMENT:
181
-                    if ($this->lastStatements !== null) {
182
-                        $this->statements = array_merge($this->statements, $this->lastStatements);
183
-                    }
184
-                    $Statements = $this->tokenToStatements($token);
185
-                    $this->queueClassesFromComments($Statements);
186
-                    $this->lastStatements = $Statements;
187
-                    break;
188
-            }
189
-
190
-            $token = next($tokens);
191
-        }
192
-    }
193
-
194
-    /**
195
-     * Convert a T_*_COMMENT string to an array of Statements
196
-     * @param array $token
197
-     * @return Statement[]
198
-     */
199
-    public function tokenToStatements($token)
200
-    {
201
-        list(, $comment, $commentLineNumber) = $token;
202
-        $commentLines = [];
203
-
204
-        $match = [];
205
-        if (preg_match('~^/\*\*?\s*(.*)\s*\*\/$~sm', $comment, $match) === 1) {
206
-            $lines = explode("\n", $match[0]);
207
-            foreach ($lines as $line) {
208
-                if ((preg_match('~^\s*\*?\s*(.*?)\s*$~', $line, $match) === 1)
209
-                    && !empty($match[1])) {
210
-                    $commentLines[] = trim($match[1]);
211
-                }
212
-            }
213
-        } elseif (preg_match('~^//\s*(.*)$~', $comment, $match) === 1) {
214
-            $commentLines[] = trim($match[1]);
215
-        }
216
-        // to commands
217
-        $match = [];
218
-        $command = null;
219
-        $data = '';
220
-        $commandLineNumber = 0;
221
-        $statements = [];
222
-        foreach ($commentLines as $lineNumber => $line) {
223
-            // If new @-command, store any old and start new
224
-            if ($command !== null && chr(ord($line)) === '@') {
225
-                $statements[] = new Statement($command, $data, $this->current_file, $commentLineNumber + $commandLineNumber);
226
-                $command = null;
227
-                $data = '';
228
-            }
229
-
230
-            if (preg_match('~^@' . preg_quote(self::COMMENT_TAG, '~') . '\\\\([a-z][-a-z]*[?!]?)\\s*(.*)$~', $line, $match) === 1) {
231
-                list(, $command, $data) = $match;
232
-                $commandLineNumber = $lineNumber;
233
-            } elseif ($command !== null) {
234
-                if ($lineNumber < count($commentLines) - 1) {
235
-                    $data .= ' ' . $line;
236
-                } else {
237
-                    $data .= preg_replace('~\s*\**\/\s*$~', '', $line);
238
-                }
239
-            }
240
-        }
241
-
242
-        if ($command !== null) {
243
-            $statements[] = new Statement($command, $data, $this->current_file, $commentLineNumber + $commandLineNumber);
244
-        }
245
-
246
-        return $statements;
247
-    }
248
-
249
-    /**
250
-     * Add to the queue any classes based on the commands.
251
-     * @param Statement[] $Statements
252
-     */
253
-    public function queueClassesFromComments(array $Statements)
254
-    {
255
-        foreach ($Statements as $Statement) {
256
-            if (in_array($Statement->getCommand(), array('uses', 'see'))) {
257
-                $match = [];
258
-                if ((preg_match('~^(\w+)(::|->)?(\w+)?(?:\(\))?$~', $Statement->getData(), $match) === 1)
259
-                    && !in_array($match[1], array('self', '$this'))) {
260
-                    $this->queueClass($match[1]);
261
-                }
262
-            }
263
-        }
264
-    }
265
-
266
-    public function queueClass($classname)
267
-    {
268
-        foreach ($this->dirs as $dir) {
269
-            $paths = array(
270
-                $dir . DIRECTORY_SEPARATOR . $classname . '.php',
271
-                $dir . DIRECTORY_SEPARATOR . $classname . '.class.php',
272
-            );
273
-
274
-            foreach ($paths as $path) {
275
-                $realpath = realpath($path);
276
-                if (in_array($realpath, $this->files_done)) {
277
-                    return;
278
-                }
279
-
280
-                if (is_file($realpath)) {
281
-                    $this->files_queued[] = $realpath;
282
-                    return;
283
-                }
284
-            }
285
-        }
286
-
287
-        // assume it's a class;
288
-    }
289
-
290
-    /**
291
-     * Inherit the statements
292
-     * @param ParserClass $Class
293
-     */
294
-    private function inherit(Entity\ParserClass $Class)
295
-    {
296
-        $inherits = array_merge(array($Class->extends), $Class->implements);
297
-        while (($inherit = array_shift($inherits)) !== null) {
298
-            if (isset($this->Classes[strtolower($inherit)])) {
299
-                $inheritedClass = $this->Classes[strtolower($inherit)];
300
-                $this->inherit($inheritedClass);
301
-
302
-                foreach ($inheritedClass->Methods as $name => $Method) {
303
-                    if (!isset($Class->Methods[$name])) {
304
-                        $Class->Methods[$name] = $Method;
305
-                    }
306
-                }
307
-            }
308
-        }
309
-    }
310
-
311
-    /**
312
-     * Expands a set of comments with comments of methods referred to by rest\uses statements.
313
-     *
314
-     * @param Statement[] $Statements
315
-     * @return Statement[]
316
-     * @throws Exception
317
-     * @throws Exception
318
-     * @throws Exception
319
-     */
320
-    private function expand(array $Statements, ?ParserClass $Self = null)
321
-    {
322
-        $output = [];
323
-
324
-        $match = [];
325
-        foreach ($Statements as $Statement) {
326
-            if (in_array($Statement->getCommand(), array('uses', 'see'))) {
327
-                if (preg_match('/^((?:\\w+)|\$this)(?:(::|->)(\\w+))?(?:\\(\\))?$/', strtolower($Statement->getData()), $match) === 1) {
328
-                    if (count($match) >= 3) {
329
-                        $Class = null;
330
-                        if (in_array($match[1], array('$this', 'self', 'static'))) {
331
-                            $Class = $Self;
332
-                        } elseif (isset($this->Classes[$match[1]])) {
333
-                            $Class = $this->Classes[$match[1]];
334
-                        }
335
-
336
-                        if ($Class) {
337
-                            if (isset($Class->Methods[$match[3]])) {
338
-                                $Method = $Class->Methods[$match[3]];
339
-                                $Method->Statements = $this->expand($Method->Statements, $Class);
340
-                                $output = array_merge($output, $Method->Statements);
341
-                            } else {
342
-                                throw new Exception("Method '{$match[3]}' for class '{$match[1]}' not found");
343
-                            }
344
-                        } else {
345
-                            throw new Exception("Class '{$match[1]}' not found");
346
-                        }
347
-                    } elseif (isset($this->Functions[$match[1]])) {
348
-                        $Function = $this->Functions[$match[1]];
349
-                        $Function->Statements = $this->expand($Function->Statements);
350
-                        $output = array_merge($output, $Function->Statements);
351
-                    } else {
352
-                        throw new Exception("Function '{$match[1]}' not found");
353
-                    }
354
-                }
355
-            } else {
356
-                $output[] = $Statement;
357
-            }
358
-        }
359
-
360
-        return $output;
361
-    }
362
-
363
-    private function extractStatements()
364
-    {
365
-        // Core comments
366
-        $Statements = $this->statements;
367
-
368
-        // Functions
369
-        foreach ($this->Functions as $Function) {
370
-            if ($Function->hasCommand('method')) {
371
-                $Statements = array_merge($Statements, $Function->Statements);
372
-            }
373
-        }
374
-
375
-        // Classes
376
-        foreach ($this->Classes as $Class) {
377
-            $Statements = array_merge($Statements, $Class->Statements);
378
-            foreach ($Class->Methods as $Method) {
379
-                if ($Method->hasCommand('method')) {
380
-                    $Statements = array_merge($Statements, $Method->Statements);
381
-                }
382
-            }
383
-        }
384
-
385
-        return $Statements;
386
-    }
45
+	private $current_file = null;
46
+	private $files_queued = [];
47
+	private $files_done = [];
48
+	private $dirs = [];
49
+	/**
50
+	 * @var Statement[]|null
51
+	 */
52
+	private $lastStatements = [];
53
+	/**
54
+	 * @var AbstractPreprocessor
55
+	 */
56
+	private $Preprocessor;
57
+
58
+	public function __construct(array $dirs = [])
59
+	{
60
+		foreach ($dirs as $dir) {
61
+			$this->common_dirs[] = realpath($dir);
62
+		}
63
+
64
+		$this->Preprocessor = new Preprocessor(self::COMMENT_TAG);
65
+	}
66
+
67
+	public function addDirs(array $dirs)
68
+	{
69
+		foreach ($dirs as $dir) {
70
+			$this->common_dirs[] = realpath($dir);
71
+		}
72
+	}
73
+
74
+	/**
75
+	 * @throws Exception
76
+	 */
77
+	public function parse($file, array $dirs = [], array $defines = [])
78
+	{
79
+		$this->dirs = $this->common_dirs;
80
+		foreach ($dirs as $dir) {
81
+			$this->dirs[] = realpath($dir);
82
+		}
83
+
84
+		$this->parseFiles(array($file), $defines);
85
+
86
+		// Inherit classes
87
+		foreach ($this->Classes as $Class) {
88
+			$this->inherit($Class);
89
+		}
90
+
91
+		// Expand functions with used and seen functions/methods.
92
+		foreach ($this->Classes as $Class) {
93
+			foreach ($Class->Methods as $Method) {
94
+				$Method->Statements = $this->expand($Method->Statements, $Class);
95
+			}
96
+		}
97
+
98
+		return $this->extractStatements();
99
+	}
100
+
101
+	private function parseFiles(array $files, array $defines = [])
102
+	{
103
+		$this->files_queued = $files;
104
+
105
+		$index = 0;
106
+		while (($file = array_shift($this->files_queued)) !== null) {
107
+			$file = realpath($file);
108
+
109
+			// @todo Test if this works
110
+			if (in_array($file, $this->files_done)) {
111
+				continue;
112
+			}
113
+
114
+			$this->current_file = $file;
115
+			$this->files_done[] = $file;
116
+			++$index;
117
+
118
+			$this->Preprocessor->resetDefines();
119
+			$this->Preprocessor->addDefines($defines);
120
+			$source = $this->Preprocessor->preprocessFile($file);
121
+
122
+			$this->parseTokens($source);
123
+
124
+			if ($this->lastStatements !== null) {
125
+				$this->statements = array_merge($this->statements, $this->lastStatements);
126
+				$this->lastStatements = null;
127
+			}
128
+		}
129
+
130
+		$this->current_file = null;
131
+	}
132
+
133
+	private function parseTokens($source)
134
+	{
135
+		$mode = null;
136
+		$namespace = '';
137
+
138
+		$tokens = token_get_all($source);
139
+		$token = reset($tokens);
140
+		while ($token) {
141
+			switch ($token[0]) {
142
+				case T_NAMESPACE:
143
+					$mode = T_NAMESPACE;
144
+					break;
145
+
146
+				case T_NS_SEPARATOR:
147
+				case T_STRING:
148
+					if ($mode === T_NAMESPACE) {
149
+						$namespace .= $token[1];
150
+					}
151
+					break;
152
+
153
+				case ';':
154
+					$mode = null;
155
+					break;
156
+
157
+				case T_CLASS:
158
+				case T_INTERFACE:
159
+					$Class = new Entity\ParserClass($this, $tokens, $this->lastStatements);
160
+					$this->Classes[strtolower($Class->name)] = $Class;
161
+					$this->lastStatements = null;
162
+					break;
163
+
164
+				case T_FUNCTION:
165
+					$Function = new ParserFunction($this, $tokens, $this->lastStatements);
166
+					$this->Functions[strtolower($Function->name)] = $Function;
167
+					$this->lastStatements = null;
168
+					break;
169
+
170
+				case T_COMMENT:
171
+					if ($this->lastStatements !== null) {
172
+						$this->statements = array_merge($this->statements, $this->lastStatements);
173
+						$this->lastStatements = null;
174
+					}
175
+					$Statements = $this->tokenToStatements($token);
176
+					$this->queueClassesFromComments($Statements);
177
+					$this->statements = array_merge($this->statements, $Statements);
178
+					break;
179
+
180
+				case T_DOC_COMMENT:
181
+					if ($this->lastStatements !== null) {
182
+						$this->statements = array_merge($this->statements, $this->lastStatements);
183
+					}
184
+					$Statements = $this->tokenToStatements($token);
185
+					$this->queueClassesFromComments($Statements);
186
+					$this->lastStatements = $Statements;
187
+					break;
188
+			}
189
+
190
+			$token = next($tokens);
191
+		}
192
+	}
193
+
194
+	/**
195
+	 * Convert a T_*_COMMENT string to an array of Statements
196
+	 * @param array $token
197
+	 * @return Statement[]
198
+	 */
199
+	public function tokenToStatements($token)
200
+	{
201
+		list(, $comment, $commentLineNumber) = $token;
202
+		$commentLines = [];
203
+
204
+		$match = [];
205
+		if (preg_match('~^/\*\*?\s*(.*)\s*\*\/$~sm', $comment, $match) === 1) {
206
+			$lines = explode("\n", $match[0]);
207
+			foreach ($lines as $line) {
208
+				if ((preg_match('~^\s*\*?\s*(.*?)\s*$~', $line, $match) === 1)
209
+					&& !empty($match[1])) {
210
+					$commentLines[] = trim($match[1]);
211
+				}
212
+			}
213
+		} elseif (preg_match('~^//\s*(.*)$~', $comment, $match) === 1) {
214
+			$commentLines[] = trim($match[1]);
215
+		}
216
+		// to commands
217
+		$match = [];
218
+		$command = null;
219
+		$data = '';
220
+		$commandLineNumber = 0;
221
+		$statements = [];
222
+		foreach ($commentLines as $lineNumber => $line) {
223
+			// If new @-command, store any old and start new
224
+			if ($command !== null && chr(ord($line)) === '@') {
225
+				$statements[] = new Statement($command, $data, $this->current_file, $commentLineNumber + $commandLineNumber);
226
+				$command = null;
227
+				$data = '';
228
+			}
229
+
230
+			if (preg_match('~^@' . preg_quote(self::COMMENT_TAG, '~') . '\\\\([a-z][-a-z]*[?!]?)\\s*(.*)$~', $line, $match) === 1) {
231
+				list(, $command, $data) = $match;
232
+				$commandLineNumber = $lineNumber;
233
+			} elseif ($command !== null) {
234
+				if ($lineNumber < count($commentLines) - 1) {
235
+					$data .= ' ' . $line;
236
+				} else {
237
+					$data .= preg_replace('~\s*\**\/\s*$~', '', $line);
238
+				}
239
+			}
240
+		}
241
+
242
+		if ($command !== null) {
243
+			$statements[] = new Statement($command, $data, $this->current_file, $commentLineNumber + $commandLineNumber);
244
+		}
245
+
246
+		return $statements;
247
+	}
248
+
249
+	/**
250
+	 * Add to the queue any classes based on the commands.
251
+	 * @param Statement[] $Statements
252
+	 */
253
+	public function queueClassesFromComments(array $Statements)
254
+	{
255
+		foreach ($Statements as $Statement) {
256
+			if (in_array($Statement->getCommand(), array('uses', 'see'))) {
257
+				$match = [];
258
+				if ((preg_match('~^(\w+)(::|->)?(\w+)?(?:\(\))?$~', $Statement->getData(), $match) === 1)
259
+					&& !in_array($match[1], array('self', '$this'))) {
260
+					$this->queueClass($match[1]);
261
+				}
262
+			}
263
+		}
264
+	}
265
+
266
+	public function queueClass($classname)
267
+	{
268
+		foreach ($this->dirs as $dir) {
269
+			$paths = array(
270
+				$dir . DIRECTORY_SEPARATOR . $classname . '.php',
271
+				$dir . DIRECTORY_SEPARATOR . $classname . '.class.php',
272
+			);
273
+
274
+			foreach ($paths as $path) {
275
+				$realpath = realpath($path);
276
+				if (in_array($realpath, $this->files_done)) {
277
+					return;
278
+				}
279
+
280
+				if (is_file($realpath)) {
281
+					$this->files_queued[] = $realpath;
282
+					return;
283
+				}
284
+			}
285
+		}
286
+
287
+		// assume it's a class;
288
+	}
289
+
290
+	/**
291
+	 * Inherit the statements
292
+	 * @param ParserClass $Class
293
+	 */
294
+	private function inherit(Entity\ParserClass $Class)
295
+	{
296
+		$inherits = array_merge(array($Class->extends), $Class->implements);
297
+		while (($inherit = array_shift($inherits)) !== null) {
298
+			if (isset($this->Classes[strtolower($inherit)])) {
299
+				$inheritedClass = $this->Classes[strtolower($inherit)];
300
+				$this->inherit($inheritedClass);
301
+
302
+				foreach ($inheritedClass->Methods as $name => $Method) {
303
+					if (!isset($Class->Methods[$name])) {
304
+						$Class->Methods[$name] = $Method;
305
+					}
306
+				}
307
+			}
308
+		}
309
+	}
310
+
311
+	/**
312
+	 * Expands a set of comments with comments of methods referred to by rest\uses statements.
313
+	 *
314
+	 * @param Statement[] $Statements
315
+	 * @return Statement[]
316
+	 * @throws Exception
317
+	 * @throws Exception
318
+	 * @throws Exception
319
+	 */
320
+	private function expand(array $Statements, ?ParserClass $Self = null)
321
+	{
322
+		$output = [];
323
+
324
+		$match = [];
325
+		foreach ($Statements as $Statement) {
326
+			if (in_array($Statement->getCommand(), array('uses', 'see'))) {
327
+				if (preg_match('/^((?:\\w+)|\$this)(?:(::|->)(\\w+))?(?:\\(\\))?$/', strtolower($Statement->getData()), $match) === 1) {
328
+					if (count($match) >= 3) {
329
+						$Class = null;
330
+						if (in_array($match[1], array('$this', 'self', 'static'))) {
331
+							$Class = $Self;
332
+						} elseif (isset($this->Classes[$match[1]])) {
333
+							$Class = $this->Classes[$match[1]];
334
+						}
335
+
336
+						if ($Class) {
337
+							if (isset($Class->Methods[$match[3]])) {
338
+								$Method = $Class->Methods[$match[3]];
339
+								$Method->Statements = $this->expand($Method->Statements, $Class);
340
+								$output = array_merge($output, $Method->Statements);
341
+							} else {
342
+								throw new Exception("Method '{$match[3]}' for class '{$match[1]}' not found");
343
+							}
344
+						} else {
345
+							throw new Exception("Class '{$match[1]}' not found");
346
+						}
347
+					} elseif (isset($this->Functions[$match[1]])) {
348
+						$Function = $this->Functions[$match[1]];
349
+						$Function->Statements = $this->expand($Function->Statements);
350
+						$output = array_merge($output, $Function->Statements);
351
+					} else {
352
+						throw new Exception("Function '{$match[1]}' not found");
353
+					}
354
+				}
355
+			} else {
356
+				$output[] = $Statement;
357
+			}
358
+		}
359
+
360
+		return $output;
361
+	}
362
+
363
+	private function extractStatements()
364
+	{
365
+		// Core comments
366
+		$Statements = $this->statements;
367
+
368
+		// Functions
369
+		foreach ($this->Functions as $Function) {
370
+			if ($Function->hasCommand('method')) {
371
+				$Statements = array_merge($Statements, $Function->Statements);
372
+			}
373
+		}
374
+
375
+		// Classes
376
+		foreach ($this->Classes as $Class) {
377
+			$Statements = array_merge($Statements, $Class->Statements);
378
+			foreach ($Class->Methods as $Method) {
379
+				if ($Method->hasCommand('method')) {
380
+					$Statements = array_merge($Statements, $Method->Statements);
381
+				}
382
+			}
383
+		}
384
+
385
+		return $Statements;
386
+	}
387 387
 
388 388
 }
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 = [];
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 = [];
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/Entity/ParserClass.php 1 patch
Indentation   +115 added lines, -115 removed lines patch added patch discarded remove patch
@@ -15,120 +15,120 @@
 block discarded – undo
15 15
 class ParserClass extends AbstractEntity
16 16
 {
17 17
 
18
-    /**
19
-     * @var string
20
-     */
21
-    public $name = null;
22
-
23
-    /**
24
-     * @var ParserFunction[]
25
-     */
26
-    public $Methods = [];
27
-
28
-    /**
29
-     * @var string
30
-     */
31
-    public $extends = null;
32
-
33
-    /**
34
-     * @var string[]
35
-     */
36
-    public $implements = [];
37
-    private $lastStatements = null;
38
-
39
-    public function __construct(Parser $Parser, &$tokens, $Statements)
40
-    {
41
-        if ($Statements) {
42
-            $this->Statements = array_merge($this->Statements, $Statements);
43
-        }
44
-
45
-        $depth = 0;
46
-
47
-        $mode = T_CLASS;
48
-
49
-        $token = current($tokens);
50
-        while ($token) {
51
-            switch ($token[0]) {
52
-                case T_STRING:
53
-                    switch ($mode) {
54
-                        case T_CLASS:
55
-                            $this->name = $token[1];
56
-                            $mode = null;
57
-                            break;
58
-
59
-                        case T_EXTENDS:
60
-                            $Parser->queueClass($token[1]);
61
-                            $this->extends = $token[1];
62
-                            $mode = null;
63
-                            break;
64
-
65
-                        case T_IMPLEMENTS:
66
-                            $Parser->queueClass($token[1]);
67
-                            $this->implements[] = $token[1];
68
-                            break;
69
-                    }
70
-                    break;
71
-
72
-                case '{':
73
-                case T_CURLY_OPEN:
74
-                case T_DOLLAR_OPEN_CURLY_BRACES:
75
-                case T_STRING_VARNAME:
76
-                    $mode = null;
77
-                    ++$depth;
78
-                    break;
79
-
80
-                case '}':
81
-                    --$depth;
82
-                    if ($depth == 0) {
83
-                        if ($this->lastStatements) {
84
-                            $this->Statements = array_merge($this->Statements, $this->lastStatements);
85
-                            $this->lastStatements = null;
86
-                        }
87
-                        return;
88
-                    }
89
-                    break;
90
-
91
-                case T_FUNCTION:
92
-                    $Method = new ParserFunction($Parser, $tokens, $this->lastStatements);
93
-                    $this->Methods[strtolower($Method->name)] = $Method;
94
-                    $this->lastStatements = null;
95
-                    break;
96
-
97
-                case T_EXTENDS:
98
-                    $mode = T_EXTENDS;
99
-                    break;
100
-
101
-                case T_IMPLEMENTS:
102
-                    $mode = T_IMPLEMENTS;
103
-                    break;
104
-
105
-                case T_COMMENT:
106
-                    if ($this->lastStatements) {
107
-                        $this->Statements = array_merge($this->Statements, $this->lastStatements);
108
-                        $this->lastStatements = null;
109
-                    }
110
-                    $Statements = $Parser->tokenToStatements($token);
111
-                    $Parser->queueClassesFromComments($Statements);
112
-                    $this->Statements = array_merge($this->Statements, $Statements);
113
-                    break;
114
-
115
-                case T_DOC_COMMENT:
116
-                    if ($this->lastStatements) {
117
-                        $this->Statements = array_merge($this->Statements, $this->lastStatements);
118
-                    }
119
-                    $Statements = $Parser->tokenToStatements($token);
120
-                    $Parser->queueClassesFromComments($Statements);
121
-                    $this->lastStatements = $Statements;
122
-                    break;
123
-            }
124
-
125
-            $token = next($tokens);
126
-        }
127
-
128
-        if ($this->lastStatements) {
129
-            $this->Statements = array_merge($this->Statements, $this->lastStatements);
130
-            $this->lastStatements = null;
131
-        }
132
-    }
18
+	/**
19
+	 * @var string
20
+	 */
21
+	public $name = null;
22
+
23
+	/**
24
+	 * @var ParserFunction[]
25
+	 */
26
+	public $Methods = [];
27
+
28
+	/**
29
+	 * @var string
30
+	 */
31
+	public $extends = null;
32
+
33
+	/**
34
+	 * @var string[]
35
+	 */
36
+	public $implements = [];
37
+	private $lastStatements = null;
38
+
39
+	public function __construct(Parser $Parser, &$tokens, $Statements)
40
+	{
41
+		if ($Statements) {
42
+			$this->Statements = array_merge($this->Statements, $Statements);
43
+		}
44
+
45
+		$depth = 0;
46
+
47
+		$mode = T_CLASS;
48
+
49
+		$token = current($tokens);
50
+		while ($token) {
51
+			switch ($token[0]) {
52
+				case T_STRING:
53
+					switch ($mode) {
54
+						case T_CLASS:
55
+							$this->name = $token[1];
56
+							$mode = null;
57
+							break;
58
+
59
+						case T_EXTENDS:
60
+							$Parser->queueClass($token[1]);
61
+							$this->extends = $token[1];
62
+							$mode = null;
63
+							break;
64
+
65
+						case T_IMPLEMENTS:
66
+							$Parser->queueClass($token[1]);
67
+							$this->implements[] = $token[1];
68
+							break;
69
+					}
70
+					break;
71
+
72
+				case '{':
73
+				case T_CURLY_OPEN:
74
+				case T_DOLLAR_OPEN_CURLY_BRACES:
75
+				case T_STRING_VARNAME:
76
+					$mode = null;
77
+					++$depth;
78
+					break;
79
+
80
+				case '}':
81
+					--$depth;
82
+					if ($depth == 0) {
83
+						if ($this->lastStatements) {
84
+							$this->Statements = array_merge($this->Statements, $this->lastStatements);
85
+							$this->lastStatements = null;
86
+						}
87
+						return;
88
+					}
89
+					break;
90
+
91
+				case T_FUNCTION:
92
+					$Method = new ParserFunction($Parser, $tokens, $this->lastStatements);
93
+					$this->Methods[strtolower($Method->name)] = $Method;
94
+					$this->lastStatements = null;
95
+					break;
96
+
97
+				case T_EXTENDS:
98
+					$mode = T_EXTENDS;
99
+					break;
100
+
101
+				case T_IMPLEMENTS:
102
+					$mode = T_IMPLEMENTS;
103
+					break;
104
+
105
+				case T_COMMENT:
106
+					if ($this->lastStatements) {
107
+						$this->Statements = array_merge($this->Statements, $this->lastStatements);
108
+						$this->lastStatements = null;
109
+					}
110
+					$Statements = $Parser->tokenToStatements($token);
111
+					$Parser->queueClassesFromComments($Statements);
112
+					$this->Statements = array_merge($this->Statements, $Statements);
113
+					break;
114
+
115
+				case T_DOC_COMMENT:
116
+					if ($this->lastStatements) {
117
+						$this->Statements = array_merge($this->Statements, $this->lastStatements);
118
+					}
119
+					$Statements = $Parser->tokenToStatements($token);
120
+					$Parser->queueClassesFromComments($Statements);
121
+					$this->lastStatements = $Statements;
122
+					break;
123
+			}
124
+
125
+			$token = next($tokens);
126
+		}
127
+
128
+		if ($this->lastStatements) {
129
+			$this->Statements = array_merge($this->Statements, $this->lastStatements);
130
+			$this->lastStatements = null;
131
+		}
132
+	}
133 133
 
134 134
 }
Please login to merge, or discard this patch.