Completed
Push — master ( 2f1531...a6cc58 )
by Martijn
17s
created
SwaggerGen/Swagger/Type/Property.php 1 patch
Indentation   +67 added lines, -67 removed lines patch added patch discarded remove patch
@@ -16,82 +16,82 @@
 block discarded – undo
16 16
 class Property extends AbstractObject
17 17
 {
18 18
 
19
-    /**
20
-     * Description of this property
21
-     * @var string
22
-     */
23
-    private $description;
19
+	/**
20
+	 * Description of this property
21
+	 * @var string
22
+	 */
23
+	private $description;
24 24
 
25
-    /**
26
-     * Whether property is read only
27
-     * @var bool
28
-     */
29
-    private $readOnly;
25
+	/**
26
+	 * Whether property is read only
27
+	 * @var bool
28
+	 */
29
+	private $readOnly;
30 30
 
31
-    /**
32
-     * Type definition of this property
33
-     * @var AbstractType
34
-     */
35
-    private $Type;
31
+	/**
32
+	 * Type definition of this property
33
+	 * @var AbstractType
34
+	 */
35
+	private $Type;
36 36
 
37
-    /**
38
-     * Create a new property
39
-     * @param AbstractObject $parent
40
-     * @param string $definition Either a built-in type or a definition name
41
-     * @param string $description description of the property
42
-     * @param bool $readOnly Whether the property is read only
43
-     * @throws Exception
44
-     */
45
-    public function __construct(AbstractObject $parent, $definition, $description = null, $readOnly = null)
46
-    {
47
-        parent::__construct($parent);
48
-        $this->Type = AbstractType::typeFactory($this, $definition, "Not a property: '%s'");
49
-        $this->description = $description;
50
-        $this->readOnly = $readOnly;
51
-    }
37
+	/**
38
+	 * Create a new property
39
+	 * @param AbstractObject $parent
40
+	 * @param string $definition Either a built-in type or a definition name
41
+	 * @param string $description description of the property
42
+	 * @param bool $readOnly Whether the property is read only
43
+	 * @throws Exception
44
+	 */
45
+	public function __construct(AbstractObject $parent, $definition, $description = null, $readOnly = null)
46
+	{
47
+		parent::__construct($parent);
48
+		$this->Type = AbstractType::typeFactory($this, $definition, "Not a property: '%s'");
49
+		$this->description = $description;
50
+		$this->readOnly = $readOnly;
51
+	}
52 52
 
53
-    /**
54
-     * @param string $command The comment command
55
-     * @param string $data Any data added after the command
56
-     * @return self|boolean
57
-     * @throws Exception
58
-     */
59
-    public function handleCommand($command, $data = null)
60
-    {
61
-        // Pass through to Type
62
-        if ($this->Type && $this->Type->handleCommand($command, $data)) {
63
-            return $this;
64
-        }
53
+	/**
54
+	 * @param string $command The comment command
55
+	 * @param string $data Any data added after the command
56
+	 * @return self|boolean
57
+	 * @throws Exception
58
+	 */
59
+	public function handleCommand($command, $data = null)
60
+	{
61
+		// Pass through to Type
62
+		if ($this->Type && $this->Type->handleCommand($command, $data)) {
63
+			return $this;
64
+		}
65 65
 
66
-        return parent::handleCommand($command, $data);
67
-    }
66
+		return parent::handleCommand($command, $data);
67
+	}
68 68
 
69
-    public function toArray()
70
-    {
71
-        // Reference + readonly/description result in allOf construct
72
-        // as it's semantically the same and that's what swagger tools
73
-        // like swagger-ui can understand
74
-        $requiresWrap =
75
-            $this->Type instanceof ReferenceObjectType
76
-            && (!empty($this->description) || !is_null($this->readOnly));
69
+	public function toArray()
70
+	{
71
+		// Reference + readonly/description result in allOf construct
72
+		// as it's semantically the same and that's what swagger tools
73
+		// like swagger-ui can understand
74
+		$requiresWrap =
75
+			$this->Type instanceof ReferenceObjectType
76
+			&& (!empty($this->description) || !is_null($this->readOnly));
77 77
 
78
-        $valueType = $this->Type->toArray();
78
+		$valueType = $this->Type->toArray();
79 79
 
80
-        if ($requiresWrap) {
81
-            $valueType = array(
82
-                'allOf' => array($valueType),
83
-            );
84
-        }
80
+		if ($requiresWrap) {
81
+			$valueType = array(
82
+				'allOf' => array($valueType),
83
+			);
84
+		}
85 85
 
86
-        return self::arrayFilterNull(array_merge($valueType, array(
87
-            'description' => empty($this->description) ? null : $this->description,
88
-            'readOnly' => $this->readOnly
89
-        ), parent::toArray()));
90
-    }
86
+		return self::arrayFilterNull(array_merge($valueType, array(
87
+			'description' => empty($this->description) ? null : $this->description,
88
+			'readOnly' => $this->readOnly
89
+		), parent::toArray()));
90
+	}
91 91
 
92
-    public function __toString()
93
-    {
94
-        return __CLASS__;
95
-    }
92
+	public function __toString()
93
+	{
94
+		return __CLASS__;
95
+	}
96 96
 
97 97
 }
Please login to merge, or discard this patch.
SwaggerGen/Swagger/Type/NumberType.php 1 patch
Indentation   +149 added lines, -149 removed lines patch added patch discarded remove patch
@@ -15,154 +15,154 @@
 block discarded – undo
15 15
 class NumberType extends AbstractType
16 16
 {
17 17
 
18
-    const REGEX_RANGE = '(?:([[<])(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?,(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?([\\]>]))?';
19
-    const REGEX_DEFAULT = '(?:=(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)))?';
20
-
21
-    private static $formats = array(
22
-        'float' => 'float',
23
-        'double' => 'double',
24
-    );
25
-    private $format;
26
-    private $default = null;
27
-    private $maximum = null;
28
-    private $exclusiveMaximum = null;
29
-    private $minimum = null;
30
-    private $exclusiveMinimum = null;
31
-    private $enum = array();
32
-    private $multipleOf = null;
33
-
34
-    /**
35
-     * @throws Exception
36
-     */
37
-    protected function parseDefinition($definition)
38
-    {
39
-        $match = array();
40
-        if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) {
41
-            throw new Exception("Unparseable number definition: '{$definition}'");
42
-        }
43
-
44
-        $this->parseFormat($definition, $match);
45
-        $this->parseRange($definition, $match);
46
-        $this->parseDefault($definition, $match);
47
-    }
48
-
49
-    /**
50
-     * @param string[] $match
51
-     * @throws Exception
52
-     */
53
-    private function parseFormat($definition, $match)
54
-    {
55
-        if (!isset(self::$formats[strtolower($match[1])])) {
56
-            throw new Exception("Not a number: '{$definition}'");
57
-        }
58
-        $this->format = self::$formats[strtolower($match[1])];
59
-    }
60
-
61
-    /**
62
-     * @param string[] $match
63
-     * @throws Exception
64
-     */
65
-    private function parseRange($definition, $match)
66
-    {
67
-        if (!empty($match[2])) {
68
-            if ($match[3] === '' && $match[4] === '') {
69
-                throw new Exception("Empty number range: '{$definition}'");
70
-            }
71
-
72
-            $this->exclusiveMinimum = $match[2] == '<';
73
-            $this->minimum = $match[3] === '' ? null : doubleval($match[3]);
74
-            $this->maximum = $match[4] === '' ? null : doubleval($match[4]);
75
-            $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null;
76
-            if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) {
77
-                self::swap($this->minimum, $this->maximum);
78
-                self::swap($this->exclusiveMinimum, $this->exclusiveMaximum);
79
-            }
80
-        }
81
-    }
82
-
83
-    /**
84
-     * @param string[] $match
85
-     * @throws Exception
86
-     */
87
-    private function parseDefault($definition, $match)
88
-    {
89
-        $this->default = isset($match[6]) && $match[6] !== '' ? $this->validateDefault($match[6]) : null;
90
-    }
91
-
92
-    /**
93
-     * @param string $command The comment command
94
-     * @param string $data Any data added after the command
95
-     * @return AbstractType|boolean
96
-     * @throws Exception
97
-     * @throws Exception
98
-     * @throws Exception
99
-     */
100
-    public function handleCommand($command, $data = null)
101
-    {
102
-        switch (strtolower($command)) {
103
-            case 'default':
104
-                $this->default = $this->validateDefault($data);
105
-                return $this;
106
-
107
-            case 'enum':
108
-                $words = self::wordSplit($data);
109
-                foreach ($words as &$word) {
110
-                    $word = $this->validateDefault($word);
111
-                }
112
-                $this->enum = array_merge($this->enum, $words);
113
-                return $this;
114
-
115
-            case 'step':
116
-                if (($step = doubleval($data)) > 0) {
117
-                    $this->multipleOf = $step;
118
-                }
119
-                return $this;
120
-        }
121
-
122
-        return parent::handleCommand($command, $data);
123
-    }
124
-
125
-    public function toArray()
126
-    {
127
-        return self::arrayFilterNull(array_merge(array(
128
-            'type' => 'number',
129
-            'format' => $this->format,
130
-            'default' => $this->default,
131
-            'minimum' => $this->minimum,
132
-            'exclusiveMinimum' => ($this->exclusiveMinimum && !is_null($this->minimum)) ? true : null,
133
-            'maximum' => $this->maximum,
134
-            'exclusiveMaximum' => ($this->exclusiveMaximum && !is_null($this->maximum)) ? true : null,
135
-            'enum' => $this->enum,
136
-            'multipleOf' => $this->multipleOf,
137
-        ), parent::toArray()));
138
-    }
139
-
140
-    public function __toString()
141
-    {
142
-        return __CLASS__;
143
-    }
144
-
145
-    /**
146
-     * @throws Exception
147
-     */
148
-    private function validateDefault($value)
149
-    {
150
-        if (preg_match('~^-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)$~', $value) !== 1) {
151
-            throw new Exception("Invalid number default: '{$value}'");
152
-        }
153
-
154
-        if ($this->maximum) {
155
-            if (($value > $this->maximum) || ($this->exclusiveMaximum && $value == $this->maximum)) {
156
-                throw new Exception("Default number beyond maximum: '{$value}'");
157
-            }
158
-        }
159
-        if ($this->minimum) {
160
-            if (($value < $this->minimum) || ($this->exclusiveMinimum && $value == $this->minimum)) {
161
-                throw new Exception("Default number beyond minimum: '{$value}'");
162
-            }
163
-        }
164
-
165
-        return doubleval($value);
166
-    }
18
+	const REGEX_RANGE = '(?:([[<])(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?,(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?([\\]>]))?';
19
+	const REGEX_DEFAULT = '(?:=(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)))?';
20
+
21
+	private static $formats = array(
22
+		'float' => 'float',
23
+		'double' => 'double',
24
+	);
25
+	private $format;
26
+	private $default = null;
27
+	private $maximum = null;
28
+	private $exclusiveMaximum = null;
29
+	private $minimum = null;
30
+	private $exclusiveMinimum = null;
31
+	private $enum = array();
32
+	private $multipleOf = null;
33
+
34
+	/**
35
+	 * @throws Exception
36
+	 */
37
+	protected function parseDefinition($definition)
38
+	{
39
+		$match = array();
40
+		if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) {
41
+			throw new Exception("Unparseable number definition: '{$definition}'");
42
+		}
43
+
44
+		$this->parseFormat($definition, $match);
45
+		$this->parseRange($definition, $match);
46
+		$this->parseDefault($definition, $match);
47
+	}
48
+
49
+	/**
50
+	 * @param string[] $match
51
+	 * @throws Exception
52
+	 */
53
+	private function parseFormat($definition, $match)
54
+	{
55
+		if (!isset(self::$formats[strtolower($match[1])])) {
56
+			throw new Exception("Not a number: '{$definition}'");
57
+		}
58
+		$this->format = self::$formats[strtolower($match[1])];
59
+	}
60
+
61
+	/**
62
+	 * @param string[] $match
63
+	 * @throws Exception
64
+	 */
65
+	private function parseRange($definition, $match)
66
+	{
67
+		if (!empty($match[2])) {
68
+			if ($match[3] === '' && $match[4] === '') {
69
+				throw new Exception("Empty number range: '{$definition}'");
70
+			}
71
+
72
+			$this->exclusiveMinimum = $match[2] == '<';
73
+			$this->minimum = $match[3] === '' ? null : doubleval($match[3]);
74
+			$this->maximum = $match[4] === '' ? null : doubleval($match[4]);
75
+			$this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null;
76
+			if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) {
77
+				self::swap($this->minimum, $this->maximum);
78
+				self::swap($this->exclusiveMinimum, $this->exclusiveMaximum);
79
+			}
80
+		}
81
+	}
82
+
83
+	/**
84
+	 * @param string[] $match
85
+	 * @throws Exception
86
+	 */
87
+	private function parseDefault($definition, $match)
88
+	{
89
+		$this->default = isset($match[6]) && $match[6] !== '' ? $this->validateDefault($match[6]) : null;
90
+	}
91
+
92
+	/**
93
+	 * @param string $command The comment command
94
+	 * @param string $data Any data added after the command
95
+	 * @return AbstractType|boolean
96
+	 * @throws Exception
97
+	 * @throws Exception
98
+	 * @throws Exception
99
+	 */
100
+	public function handleCommand($command, $data = null)
101
+	{
102
+		switch (strtolower($command)) {
103
+			case 'default':
104
+				$this->default = $this->validateDefault($data);
105
+				return $this;
106
+
107
+			case 'enum':
108
+				$words = self::wordSplit($data);
109
+				foreach ($words as &$word) {
110
+					$word = $this->validateDefault($word);
111
+				}
112
+				$this->enum = array_merge($this->enum, $words);
113
+				return $this;
114
+
115
+			case 'step':
116
+				if (($step = doubleval($data)) > 0) {
117
+					$this->multipleOf = $step;
118
+				}
119
+				return $this;
120
+		}
121
+
122
+		return parent::handleCommand($command, $data);
123
+	}
124
+
125
+	public function toArray()
126
+	{
127
+		return self::arrayFilterNull(array_merge(array(
128
+			'type' => 'number',
129
+			'format' => $this->format,
130
+			'default' => $this->default,
131
+			'minimum' => $this->minimum,
132
+			'exclusiveMinimum' => ($this->exclusiveMinimum && !is_null($this->minimum)) ? true : null,
133
+			'maximum' => $this->maximum,
134
+			'exclusiveMaximum' => ($this->exclusiveMaximum && !is_null($this->maximum)) ? true : null,
135
+			'enum' => $this->enum,
136
+			'multipleOf' => $this->multipleOf,
137
+		), parent::toArray()));
138
+	}
139
+
140
+	public function __toString()
141
+	{
142
+		return __CLASS__;
143
+	}
144
+
145
+	/**
146
+	 * @throws Exception
147
+	 */
148
+	private function validateDefault($value)
149
+	{
150
+		if (preg_match('~^-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)$~', $value) !== 1) {
151
+			throw new Exception("Invalid number default: '{$value}'");
152
+		}
153
+
154
+		if ($this->maximum) {
155
+			if (($value > $this->maximum) || ($this->exclusiveMaximum && $value == $this->maximum)) {
156
+				throw new Exception("Default number beyond maximum: '{$value}'");
157
+			}
158
+		}
159
+		if ($this->minimum) {
160
+			if (($value < $this->minimum) || ($this->exclusiveMinimum && $value == $this->minimum)) {
161
+				throw new Exception("Default number beyond minimum: '{$value}'");
162
+			}
163
+		}
164
+
165
+		return doubleval($value);
166
+	}
167 167
 
168 168
 }
Please login to merge, or discard this patch.
SwaggerGen/Swagger/Info.php 2 patches
Indentation   +101 added lines, -101 removed lines patch added patch discarded remove patch
@@ -14,106 +14,106 @@
 block discarded – undo
14 14
 class Info extends AbstractObject
15 15
 {
16 16
 
17
-    /**
18
-     * @var string
19
-     */
20
-    private $title = 'undefined';
21
-
22
-    /**
23
-     * @var string
24
-     */
25
-    private $description;
26
-
27
-    /**
28
-     * @var string
29
-     */
30
-    private $termsofservice;
31
-
32
-    /**
33
-     * @var Contact
34
-     */
35
-    private $contact;
36
-
37
-    /**
38
-     * @var License
39
-     */
40
-    private $license;
41
-
42
-    /**
43
-     * @var string|integer|float
44
-     */
45
-    private $version = 0;
46
-
47
-    /**
48
-     * @param string $command
49
-     * @param string $data
50
-     * @return AbstractObject|boolean
51
-     */
52
-    public function handleCommand($command, $data = null)
53
-    {
54
-        switch (strtolower($command)) {
55
-            case 'title':
56
-            case 'description':
57
-            case 'termsofservice':
58
-            case 'version':
59
-                $this->$command = $data;
60
-                return $this;
61
-
62
-            case 'terms': // alias
63
-            case 'tos': // alias
64
-                $this->termsofservice = $data;
65
-                return $this;
66
-
67
-            case 'contact':
68
-                $name = array();
69
-                $url = null;
70
-                $email = null;
71
-                foreach (self::wordSplit($data) as $word) {
72
-                    if (filter_var($word, FILTER_VALIDATE_URL)) {
73
-                        $url = $word;
74
-                    } elseif (filter_var($word, FILTER_VALIDATE_EMAIL)) {
75
-                        $email = $word;
76
-                    } else {
77
-                        $name[] = $word;
78
-                    }
79
-                }
80
-                $name = join(' ', array_filter($name));
81
-                $this->contact = new Contact($this, $name, $url, $email);
82
-                return $this->contact;
83
-
84
-            case 'license':
85
-                $name = array();
86
-                $url = null;
87
-                foreach (self::wordSplit($data) as $word) {
88
-                    if (filter_var($word, FILTER_VALIDATE_URL)) {
89
-                        $url = $word;
90
-                    } else {
91
-                        $name[] = $word;
92
-                    }
93
-                }
94
-                $name = join(' ', array_filter($name));
95
-                $this->license = new License($this, $name, $url);
96
-                return $this->license;
97
-        }
98
-
99
-        return parent::handleCommand($command, $data);
100
-    }
101
-
102
-    public function toArray()
103
-    {
104
-        return self::arrayFilterNull(array_merge(array(
105
-            'title' => $this->title,
106
-            'description' => $this->description,
107
-            'termsOfService' => $this->termsofservice,
108
-            'contact' => $this->contact ? $this->contact->toArray() : null,
109
-            'license' => $this->license ? $this->license->toArray() : null,
110
-            'version' => (string)$this->version,
111
-        ), parent::toArray()));
112
-    }
113
-
114
-    public function __toString()
115
-    {
116
-        return __CLASS__ . ' \'' . $this->title . '\'';
117
-    }
17
+	/**
18
+	 * @var string
19
+	 */
20
+	private $title = 'undefined';
21
+
22
+	/**
23
+	 * @var string
24
+	 */
25
+	private $description;
26
+
27
+	/**
28
+	 * @var string
29
+	 */
30
+	private $termsofservice;
31
+
32
+	/**
33
+	 * @var Contact
34
+	 */
35
+	private $contact;
36
+
37
+	/**
38
+	 * @var License
39
+	 */
40
+	private $license;
41
+
42
+	/**
43
+	 * @var string|integer|float
44
+	 */
45
+	private $version = 0;
46
+
47
+	/**
48
+	 * @param string $command
49
+	 * @param string $data
50
+	 * @return AbstractObject|boolean
51
+	 */
52
+	public function handleCommand($command, $data = null)
53
+	{
54
+		switch (strtolower($command)) {
55
+			case 'title':
56
+			case 'description':
57
+			case 'termsofservice':
58
+			case 'version':
59
+				$this->$command = $data;
60
+				return $this;
61
+
62
+			case 'terms': // alias
63
+			case 'tos': // alias
64
+				$this->termsofservice = $data;
65
+				return $this;
66
+
67
+			case 'contact':
68
+				$name = array();
69
+				$url = null;
70
+				$email = null;
71
+				foreach (self::wordSplit($data) as $word) {
72
+					if (filter_var($word, FILTER_VALIDATE_URL)) {
73
+						$url = $word;
74
+					} elseif (filter_var($word, FILTER_VALIDATE_EMAIL)) {
75
+						$email = $word;
76
+					} else {
77
+						$name[] = $word;
78
+					}
79
+				}
80
+				$name = join(' ', array_filter($name));
81
+				$this->contact = new Contact($this, $name, $url, $email);
82
+				return $this->contact;
83
+
84
+			case 'license':
85
+				$name = array();
86
+				$url = null;
87
+				foreach (self::wordSplit($data) as $word) {
88
+					if (filter_var($word, FILTER_VALIDATE_URL)) {
89
+						$url = $word;
90
+					} else {
91
+						$name[] = $word;
92
+					}
93
+				}
94
+				$name = join(' ', array_filter($name));
95
+				$this->license = new License($this, $name, $url);
96
+				return $this->license;
97
+		}
98
+
99
+		return parent::handleCommand($command, $data);
100
+	}
101
+
102
+	public function toArray()
103
+	{
104
+		return self::arrayFilterNull(array_merge(array(
105
+			'title' => $this->title,
106
+			'description' => $this->description,
107
+			'termsOfService' => $this->termsofservice,
108
+			'contact' => $this->contact ? $this->contact->toArray() : null,
109
+			'license' => $this->license ? $this->license->toArray() : null,
110
+			'version' => (string)$this->version,
111
+		), parent::toArray()));
112
+	}
113
+
114
+	public function __toString()
115
+	{
116
+		return __CLASS__ . ' \'' . $this->title . '\'';
117
+	}
118 118
 
119 119
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -107,7 +107,7 @@
 block discarded – undo
107 107
             'termsOfService' => $this->termsofservice,
108 108
             'contact' => $this->contact ? $this->contact->toArray() : null,
109 109
             'license' => $this->license ? $this->license->toArray() : null,
110
-            'version' => (string)$this->version,
110
+            'version' => (string) $this->version,
111 111
         ), parent::toArray()));
112 112
     }
113 113
 
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 = array();
17
-    private $stack = array();
18
-
19
-    public function __construct()
20
-    {
21
-        $this->resetDefines();
22
-    }
23
-
24
-    public function resetDefines()
25
-    {
26
-        $this->defines = array();
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
-    protected function getState()
45
-    {
46
-        return empty($this->stack) || end($this->stack);
47
-    }
48
-
49
-    /**
50
-     * Get the first word from a string and remove it from the string.
51
-     *
52
-     * @param string $data
53
-     * @return boolean|string
54
-     */
55
-    private static function wordShift(&$data)
56
-    {
57
-        if (preg_match('~^(\S+)\s*(.*)$~', $data, $matches) === 1) {
58
-            $data = $matches[2];
59
-            return $matches[1];
60
-        }
61
-        return false;
62
-    }
63
-
64
-    protected function handle($command, $expression)
65
-    {
66
-        switch (strtolower($command)) {
67
-            case 'if':
68
-                $name = self::wordShift($expression);
69
-                $state = $this->getState();
70
-                if (empty($expression)) {
71
-                    $this->stack[] = $state && !empty($this->defines[$name]);
72
-                } else {
73
-                    $this->stack[] = $state && isset($this->defines[$name]) && $this->defines[$name] == $expression;
74
-                }
75
-                break;
76
-
77
-            case 'ifdef':
78
-                $this->stack[] = $this->getState() && isset($this->defines[$expression]);
79
-                break;
80
-
81
-            case 'ifndef':
82
-                $this->stack[] = $this->getState() && !isset($this->defines[$expression]);
83
-                break;
84
-
85
-            case 'else':
86
-                $state = $this->getState();
87
-                array_pop($this->stack);
88
-                $this->stack[] = !$state;
89
-                break;
90
-
91
-            case 'elif':
92
-                $name = self::wordShift($expression);
93
-                $state = $this->getState();
94
-                array_pop($this->stack);
95
-                if (empty($expression)) {
96
-                    $this->stack[] = !$state && !empty($this->defines[$name]);
97
-                } else {
98
-                    $this->stack[] = !$state && isset($this->defines[$name]) && $this->defines[$name] == $expression;
99
-                }
100
-                break;
101
-
102
-            case 'define':
103
-                $name = self::wordShift($expression);
104
-                $this->defines[$name] = $expression;
105
-                break;
106
-
107
-            case 'undef':
108
-                unset($this->defines[$expression]);
109
-                break;
110
-
111
-            case 'endif':
112
-                array_pop($this->stack);
113
-                break;
114
-
115
-            default:
116
-                return false;
117
-        }
118
-
119
-        return true;
120
-    }
121
-
122
-    public function preprocess($content)
123
-    {
124
-        $this->stack = array();
125
-
126
-        return $this->parseContent($content);
127
-    }
128
-
129
-    public function preprocessFile($filename)
130
-    {
131
-        return $this->preprocess(file_get_contents($filename));
132
-    }
133
-
134
-    abstract protected function parseContent($content);
16
+	private $defines = array();
17
+	private $stack = array();
18
+
19
+	public function __construct()
20
+	{
21
+		$this->resetDefines();
22
+	}
23
+
24
+	public function resetDefines()
25
+	{
26
+		$this->defines = array();
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
+	protected function getState()
45
+	{
46
+		return empty($this->stack) || end($this->stack);
47
+	}
48
+
49
+	/**
50
+	 * Get the first word from a string and remove it from the string.
51
+	 *
52
+	 * @param string $data
53
+	 * @return boolean|string
54
+	 */
55
+	private static function wordShift(&$data)
56
+	{
57
+		if (preg_match('~^(\S+)\s*(.*)$~', $data, $matches) === 1) {
58
+			$data = $matches[2];
59
+			return $matches[1];
60
+		}
61
+		return false;
62
+	}
63
+
64
+	protected function handle($command, $expression)
65
+	{
66
+		switch (strtolower($command)) {
67
+			case 'if':
68
+				$name = self::wordShift($expression);
69
+				$state = $this->getState();
70
+				if (empty($expression)) {
71
+					$this->stack[] = $state && !empty($this->defines[$name]);
72
+				} else {
73
+					$this->stack[] = $state && isset($this->defines[$name]) && $this->defines[$name] == $expression;
74
+				}
75
+				break;
76
+
77
+			case 'ifdef':
78
+				$this->stack[] = $this->getState() && isset($this->defines[$expression]);
79
+				break;
80
+
81
+			case 'ifndef':
82
+				$this->stack[] = $this->getState() && !isset($this->defines[$expression]);
83
+				break;
84
+
85
+			case 'else':
86
+				$state = $this->getState();
87
+				array_pop($this->stack);
88
+				$this->stack[] = !$state;
89
+				break;
90
+
91
+			case 'elif':
92
+				$name = self::wordShift($expression);
93
+				$state = $this->getState();
94
+				array_pop($this->stack);
95
+				if (empty($expression)) {
96
+					$this->stack[] = !$state && !empty($this->defines[$name]);
97
+				} else {
98
+					$this->stack[] = !$state && isset($this->defines[$name]) && $this->defines[$name] == $expression;
99
+				}
100
+				break;
101
+
102
+			case 'define':
103
+				$name = self::wordShift($expression);
104
+				$this->defines[$name] = $expression;
105
+				break;
106
+
107
+			case 'undef':
108
+				unset($this->defines[$expression]);
109
+				break;
110
+
111
+			case 'endif':
112
+				array_pop($this->stack);
113
+				break;
114
+
115
+			default:
116
+				return false;
117
+		}
118
+
119
+		return true;
120
+	}
121
+
122
+	public function preprocess($content)
123
+	{
124
+		$this->stack = array();
125
+
126
+		return $this->parseContent($content);
127
+	}
128
+
129
+	public function preprocessFile($filename)
130
+	{
131
+		return $this->preprocess(file_get_contents($filename));
132
+	}
133
+
134
+	abstract protected function parseContent($content);
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
-    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/Parser.php 1 patch
Indentation   +366 added lines, -366 removed lines patch added patch discarded remove patch
@@ -20,376 +20,376 @@
 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
-        $comment = $token[1];
139
-        $commentLineNumber = $token[2];
140
-        $commentLines = [];
141
-
142
-        $match = [];
143
-        if (preg_match('~^/\*\*?\s*(.*)\s*\*\/$~sm', $comment, $match) === 1) {
144
-            $lines = preg_split('~\n~', $match[0]);
145
-            foreach ($lines as $line) {
146
-                if (preg_match('~^\s*\*?\s*(.*?)\s*$~', $line, $match) === 1) {
147
-                    if (!empty($match[1])) {
148
-                        $commentLines[] = trim($match[1]);
149
-                    }
150
-                }
151
-            }
152
-        } elseif (preg_match('~^//\s*(.*)$~', $comment, $match) === 1) {
153
-            $commentLines[] = trim($match[1]);
154
-        }
155
-        // to commands
156
-        $match = [];
157
-        $command = null;
158
-        $data = '';
159
-        $commandLineNumber = 0;
160
-        $statements = [];
161
-        foreach ($commentLines as $lineNumber => $line) {
162
-            // If new @-command, store any old and start new
163
-            if ($command !== null && chr(ord($line)) === '@') {
164
-                $statements[] = new Statement($command, $data, $this->current_file, $commentLineNumber + $commandLineNumber);
165
-                $command = null;
166
-                $data = '';
167
-            }
168
-
169
-            if (preg_match('~^@' . preg_quote(self::COMMENT_TAG) . '\\\\([a-z][-a-z]*[?!]?)\\s*(.*)$~', $line, $match) === 1) {
170
-                $command = $match[1];
171
-                $data = $match[2];
172
-                $commandLineNumber = $lineNumber;
173
-            } elseif ($command !== null) {
174
-                if ($lineNumber < count($commentLines) - 1) {
175
-                    $data .= ' ' . $line;
176
-                } else {
177
-                    $data .= preg_replace('~\s*\**\/\s*$~', '', $line);
178
-                }
179
-            }
180
-        }
181
-
182
-        if ($command !== null) {
183
-            $statements[] = new Statement($command, $data, $this->current_file, $commentLineNumber + $commandLineNumber);
184
-        }
185
-
186
-        return $statements;
187
-    }
188
-
189
-    public function queueClass($classname)
190
-    {
191
-        foreach ($this->dirs as $dir) {
192
-            $paths = array(
193
-                $dir . DIRECTORY_SEPARATOR . $classname . '.php',
194
-                $dir . DIRECTORY_SEPARATOR . $classname . '.class.php',
195
-            );
196
-
197
-            foreach ($paths as $path) {
198
-                $realpath = realpath($path);
199
-                if (in_array($realpath, $this->files_done)) {
200
-                    return;
201
-                } elseif (is_file($realpath)) {
202
-                    $this->files_queued[] = $realpath;
203
-                    return;
204
-                }
205
-            }
206
-        }
207
-
208
-        // assume it's a class;
209
-    }
210
-
211
-    /**
212
-     * Add to the queue any classes based on the commands.
213
-     * @param Statement[] $Statements
214
-     */
215
-    public function queueClassesFromComments(array $Statements)
216
-    {
217
-        foreach ($Statements as $Statement) {
218
-            if (in_array($Statement->getCommand(), array('uses', 'see'))) {
219
-                $match = [];
220
-                if (preg_match('~^(\w+)(::|->)?(\w+)?(?:\(\))?$~', $Statement->getData(), $match) === 1) {
221
-                    if (!in_array($match[1], array('self', '$this'))) {
222
-                        $this->queueClass($match[1]);
223
-                    }
224
-                }
225
-            }
226
-        }
227
-    }
228
-
229
-    private function parseTokens($source)
230
-    {
231
-        $mode = null;
232
-        $namespace = '';
233
-
234
-        $tokens = token_get_all($source);
235
-        $token = reset($tokens);
236
-        while ($token) {
237
-            switch ($token[0]) {
238
-                case T_NAMESPACE:
239
-                    $mode = T_NAMESPACE;
240
-                    break;
241
-
242
-                case T_NS_SEPARATOR:
243
-                case T_STRING:
244
-                    if ($mode === T_NAMESPACE) {
245
-                        $namespace .= $token[1];
246
-                    }
247
-                    break;
248
-
249
-                case ';':
250
-                    $mode = null;
251
-                    break;
252
-
253
-                case T_CLASS:
254
-                case T_INTERFACE:
255
-                    $Class = new Entity\ParserClass($this, $tokens, $this->lastStatements);
256
-                    $this->Classes[strtolower($Class->name)] = $Class;
257
-                    $this->lastStatements = null;
258
-                    break;
259
-
260
-                case T_FUNCTION:
261
-                    $Function = new Entity\ParserFunction($this, $tokens, $this->lastStatements);
262
-                    $this->Functions[strtolower($Function->name)] = $Function;
263
-                    $this->lastStatements = null;
264
-                    break;
265
-
266
-                case T_COMMENT:
267
-                    if ($this->lastStatements !== null) {
268
-                        $this->statements = array_merge($this->statements, $this->lastStatements);
269
-                        $this->lastStatements = null;
270
-                    }
271
-                    $Statements = $this->tokenToStatements($token);
272
-                    $this->queueClassesFromComments($Statements);
273
-                    $this->statements = array_merge($this->statements, $Statements);
274
-                    break;
275
-
276
-                case T_DOC_COMMENT:
277
-                    if ($this->lastStatements !== null) {
278
-                        $this->statements = array_merge($this->statements, $this->lastStatements);
279
-                    }
280
-                    $Statements = $this->tokenToStatements($token);
281
-                    $this->queueClassesFromComments($Statements);
282
-                    $this->lastStatements = $Statements;
283
-                    break;
284
-            }
285
-
286
-            $token = next($tokens);
287
-        }
288
-    }
289
-
290
-    private function parseFiles(array $files, array $defines = [])
291
-    {
292
-        $this->files_queued = $files;
293
-
294
-        $index = 0;
295
-        while (($file = array_shift($this->files_queued)) !== null) {
296
-            $file = realpath($file);
297
-
298
-            // @todo Test if this works
299
-            if (in_array($file, $this->files_done)) {
300
-                continue;
301
-            }
302
-
303
-            $this->current_file = $file;
304
-            $this->files_done[] = $file;
305
-            ++$index;
306
-
307
-            $this->Preprocessor->resetDefines();
308
-            $this->Preprocessor->addDefines($defines);
309
-            $source = $this->Preprocessor->preprocessFile($file);
310
-
311
-            $this->parseTokens($source);
312
-
313
-            if ($this->lastStatements !== null) {
314
-                $this->statements = array_merge($this->statements, $this->lastStatements);
315
-                $this->lastStatements = null;
316
-            }
317
-        }
318
-
319
-        $this->current_file = null;
320
-    }
321
-
322
-    /**
323
-     * Inherit the statements
324
-     * @param ParserClass $Class
325
-     */
326
-    private function inherit(Entity\ParserClass $Class)
327
-    {
328
-        $inherits = array_merge(array($Class->extends), $Class->implements);
329
-        while (($inherit = array_shift($inherits)) !== null) {
330
-            if (isset($this->Classes[strtolower($inherit)])) {
331
-                $inheritedClass = $this->Classes[strtolower($inherit)];
332
-                $this->inherit($inheritedClass);
333
-
334
-                foreach ($inheritedClass->Methods as $name => $Method) {
335
-                    if (!isset($Class->Methods[$name])) {
336
-                        $Class->Methods[$name] = $Method;
337
-                    }
338
-                }
339
-            }
340
-        }
341
-    }
342
-
343
-    /**
344
-     * Expands a set of comments with comments of methods referred to by rest\uses statements.
345
-     *
346
-     * @param Statement[] $Statements
347
-     * @return Statement[]
348
-     * @throws Exception
349
-     * @throws Exception
350
-     * @throws Exception
351
-     */
352
-    private function expand(array $Statements, ParserClass $Self = null)
353
-    {
354
-        $output = [];
355
-
356
-        $match = [];
357
-        foreach ($Statements as $Statement) {
358
-            if (in_array($Statement->getCommand(), array('uses', 'see'))) {
359
-                if (preg_match('/^((?:\\w+)|\$this)(?:(::|->)(\\w+))?(?:\\(\\))?$/', strtolower($Statement->getData()), $match) === 1) {
360
-                    if (count($match) >= 3) {
361
-                        $Class = null;
362
-                        if (in_array($match[1], array('$this', 'self', 'static'))) {
363
-                            $Class = $Self;
364
-                        } elseif (isset($this->Classes[$match[1]])) {
365
-                            $Class = $this->Classes[$match[1]];
366
-                        }
367
-
368
-                        if ($Class) {
369
-                            if (isset($Class->Methods[$match[3]])) {
370
-                                $Method = $Class->Methods[$match[3]];
371
-                                $Method->Statements = $this->expand($Method->Statements, $Class);
372
-                                $output = array_merge($output, $Method->Statements);
373
-                            } else {
374
-                                throw new Exception("Method '{$match[3]}' for class '{$match[1]}' not found");
375
-                            }
376
-                        } else {
377
-                            throw new Exception("Class '{$match[1]}' not found");
378
-                        }
379
-                    } elseif (isset($this->Functions[$match[1]])) {
380
-                        $Function = $this->Functions[$match[1]];
381
-                        $Function->Statements = $this->expand($Function->Statements);
382
-                        $output = array_merge($output, $Function->Statements);
383
-                    } else {
384
-                        throw new Exception("Function '{$match[1]}' not found");
385
-                    }
386
-                }
387
-            } else {
388
-                $output[] = $Statement;
389
-            }
390
-        }
391
-
392
-        return $output;
393
-    }
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
+		$comment = $token[1];
139
+		$commentLineNumber = $token[2];
140
+		$commentLines = [];
141
+
142
+		$match = [];
143
+		if (preg_match('~^/\*\*?\s*(.*)\s*\*\/$~sm', $comment, $match) === 1) {
144
+			$lines = preg_split('~\n~', $match[0]);
145
+			foreach ($lines as $line) {
146
+				if (preg_match('~^\s*\*?\s*(.*?)\s*$~', $line, $match) === 1) {
147
+					if (!empty($match[1])) {
148
+						$commentLines[] = trim($match[1]);
149
+					}
150
+				}
151
+			}
152
+		} elseif (preg_match('~^//\s*(.*)$~', $comment, $match) === 1) {
153
+			$commentLines[] = trim($match[1]);
154
+		}
155
+		// to commands
156
+		$match = [];
157
+		$command = null;
158
+		$data = '';
159
+		$commandLineNumber = 0;
160
+		$statements = [];
161
+		foreach ($commentLines as $lineNumber => $line) {
162
+			// If new @-command, store any old and start new
163
+			if ($command !== null && chr(ord($line)) === '@') {
164
+				$statements[] = new Statement($command, $data, $this->current_file, $commentLineNumber + $commandLineNumber);
165
+				$command = null;
166
+				$data = '';
167
+			}
168
+
169
+			if (preg_match('~^@' . preg_quote(self::COMMENT_TAG) . '\\\\([a-z][-a-z]*[?!]?)\\s*(.*)$~', $line, $match) === 1) {
170
+				$command = $match[1];
171
+				$data = $match[2];
172
+				$commandLineNumber = $lineNumber;
173
+			} elseif ($command !== null) {
174
+				if ($lineNumber < count($commentLines) - 1) {
175
+					$data .= ' ' . $line;
176
+				} else {
177
+					$data .= preg_replace('~\s*\**\/\s*$~', '', $line);
178
+				}
179
+			}
180
+		}
181
+
182
+		if ($command !== null) {
183
+			$statements[] = new Statement($command, $data, $this->current_file, $commentLineNumber + $commandLineNumber);
184
+		}
185
+
186
+		return $statements;
187
+	}
188
+
189
+	public function queueClass($classname)
190
+	{
191
+		foreach ($this->dirs as $dir) {
192
+			$paths = array(
193
+				$dir . DIRECTORY_SEPARATOR . $classname . '.php',
194
+				$dir . DIRECTORY_SEPARATOR . $classname . '.class.php',
195
+			);
196
+
197
+			foreach ($paths as $path) {
198
+				$realpath = realpath($path);
199
+				if (in_array($realpath, $this->files_done)) {
200
+					return;
201
+				} elseif (is_file($realpath)) {
202
+					$this->files_queued[] = $realpath;
203
+					return;
204
+				}
205
+			}
206
+		}
207
+
208
+		// assume it's a class;
209
+	}
210
+
211
+	/**
212
+	 * Add to the queue any classes based on the commands.
213
+	 * @param Statement[] $Statements
214
+	 */
215
+	public function queueClassesFromComments(array $Statements)
216
+	{
217
+		foreach ($Statements as $Statement) {
218
+			if (in_array($Statement->getCommand(), array('uses', 'see'))) {
219
+				$match = [];
220
+				if (preg_match('~^(\w+)(::|->)?(\w+)?(?:\(\))?$~', $Statement->getData(), $match) === 1) {
221
+					if (!in_array($match[1], array('self', '$this'))) {
222
+						$this->queueClass($match[1]);
223
+					}
224
+				}
225
+			}
226
+		}
227
+	}
228
+
229
+	private function parseTokens($source)
230
+	{
231
+		$mode = null;
232
+		$namespace = '';
233
+
234
+		$tokens = token_get_all($source);
235
+		$token = reset($tokens);
236
+		while ($token) {
237
+			switch ($token[0]) {
238
+				case T_NAMESPACE:
239
+					$mode = T_NAMESPACE;
240
+					break;
241
+
242
+				case T_NS_SEPARATOR:
243
+				case T_STRING:
244
+					if ($mode === T_NAMESPACE) {
245
+						$namespace .= $token[1];
246
+					}
247
+					break;
248
+
249
+				case ';':
250
+					$mode = null;
251
+					break;
252
+
253
+				case T_CLASS:
254
+				case T_INTERFACE:
255
+					$Class = new Entity\ParserClass($this, $tokens, $this->lastStatements);
256
+					$this->Classes[strtolower($Class->name)] = $Class;
257
+					$this->lastStatements = null;
258
+					break;
259
+
260
+				case T_FUNCTION:
261
+					$Function = new Entity\ParserFunction($this, $tokens, $this->lastStatements);
262
+					$this->Functions[strtolower($Function->name)] = $Function;
263
+					$this->lastStatements = null;
264
+					break;
265
+
266
+				case T_COMMENT:
267
+					if ($this->lastStatements !== null) {
268
+						$this->statements = array_merge($this->statements, $this->lastStatements);
269
+						$this->lastStatements = null;
270
+					}
271
+					$Statements = $this->tokenToStatements($token);
272
+					$this->queueClassesFromComments($Statements);
273
+					$this->statements = array_merge($this->statements, $Statements);
274
+					break;
275
+
276
+				case T_DOC_COMMENT:
277
+					if ($this->lastStatements !== null) {
278
+						$this->statements = array_merge($this->statements, $this->lastStatements);
279
+					}
280
+					$Statements = $this->tokenToStatements($token);
281
+					$this->queueClassesFromComments($Statements);
282
+					$this->lastStatements = $Statements;
283
+					break;
284
+			}
285
+
286
+			$token = next($tokens);
287
+		}
288
+	}
289
+
290
+	private function parseFiles(array $files, array $defines = [])
291
+	{
292
+		$this->files_queued = $files;
293
+
294
+		$index = 0;
295
+		while (($file = array_shift($this->files_queued)) !== null) {
296
+			$file = realpath($file);
297
+
298
+			// @todo Test if this works
299
+			if (in_array($file, $this->files_done)) {
300
+				continue;
301
+			}
302
+
303
+			$this->current_file = $file;
304
+			$this->files_done[] = $file;
305
+			++$index;
306
+
307
+			$this->Preprocessor->resetDefines();
308
+			$this->Preprocessor->addDefines($defines);
309
+			$source = $this->Preprocessor->preprocessFile($file);
310
+
311
+			$this->parseTokens($source);
312
+
313
+			if ($this->lastStatements !== null) {
314
+				$this->statements = array_merge($this->statements, $this->lastStatements);
315
+				$this->lastStatements = null;
316
+			}
317
+		}
318
+
319
+		$this->current_file = null;
320
+	}
321
+
322
+	/**
323
+	 * Inherit the statements
324
+	 * @param ParserClass $Class
325
+	 */
326
+	private function inherit(Entity\ParserClass $Class)
327
+	{
328
+		$inherits = array_merge(array($Class->extends), $Class->implements);
329
+		while (($inherit = array_shift($inherits)) !== null) {
330
+			if (isset($this->Classes[strtolower($inherit)])) {
331
+				$inheritedClass = $this->Classes[strtolower($inherit)];
332
+				$this->inherit($inheritedClass);
333
+
334
+				foreach ($inheritedClass->Methods as $name => $Method) {
335
+					if (!isset($Class->Methods[$name])) {
336
+						$Class->Methods[$name] = $Method;
337
+					}
338
+				}
339
+			}
340
+		}
341
+	}
342
+
343
+	/**
344
+	 * Expands a set of comments with comments of methods referred to by rest\uses statements.
345
+	 *
346
+	 * @param Statement[] $Statements
347
+	 * @return Statement[]
348
+	 * @throws Exception
349
+	 * @throws Exception
350
+	 * @throws Exception
351
+	 */
352
+	private function expand(array $Statements, ParserClass $Self = null)
353
+	{
354
+		$output = [];
355
+
356
+		$match = [];
357
+		foreach ($Statements as $Statement) {
358
+			if (in_array($Statement->getCommand(), array('uses', 'see'))) {
359
+				if (preg_match('/^((?:\\w+)|\$this)(?:(::|->)(\\w+))?(?:\\(\\))?$/', strtolower($Statement->getData()), $match) === 1) {
360
+					if (count($match) >= 3) {
361
+						$Class = null;
362
+						if (in_array($match[1], array('$this', 'self', 'static'))) {
363
+							$Class = $Self;
364
+						} elseif (isset($this->Classes[$match[1]])) {
365
+							$Class = $this->Classes[$match[1]];
366
+						}
367
+
368
+						if ($Class) {
369
+							if (isset($Class->Methods[$match[3]])) {
370
+								$Method = $Class->Methods[$match[3]];
371
+								$Method->Statements = $this->expand($Method->Statements, $Class);
372
+								$output = array_merge($output, $Method->Statements);
373
+							} else {
374
+								throw new Exception("Method '{$match[3]}' for class '{$match[1]}' not found");
375
+							}
376
+						} else {
377
+							throw new Exception("Class '{$match[1]}' not found");
378
+						}
379
+					} elseif (isset($this->Functions[$match[1]])) {
380
+						$Function = $this->Functions[$match[1]];
381
+						$Function->Statements = $this->expand($Function->Statements);
382
+						$output = array_merge($output, $Function->Statements);
383
+					} else {
384
+						throw new Exception("Function '{$match[1]}' not found");
385
+					}
386
+				}
387
+			} else {
388
+				$output[] = $Statement;
389
+			}
390
+		}
391
+
392
+		return $output;
393
+	}
394 394
 
395 395
 }
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 = array();
27
-
28
-    /**
29
-     * @var string
30
-     */
31
-    public $extends = null;
32
-
33
-    /**
34
-     * @var string[]
35
-     */
36
-    public $implements = array();
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 = array();
27
+
28
+	/**
29
+	 * @var string
30
+	 */
31
+	public $extends = null;
32
+
33
+	/**
34
+	 * @var string[]
35
+	 */
36
+	public $implements = array();
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.
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)
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()
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)
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()
40
+	{
41
+		return $this->Statements;
42
+	}
43 43
 
44 44
 }
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()
87
-    {
88
-        // inherit
89
-        return $this->Statements;
90
-    }
86
+	public function getStatements()
87
+	{
88
+		// inherit
89
+		return $this->Statements;
90
+	}
91 91
 
92 92
 }
Please login to merge, or discard this patch.