1
1
<?php
2
2
/**
3
- * phpDocumentor
3
+ * This file is part of phpDocumentor.
4
4
*
5
- * PHP Version 5.3
5
+ * For the full copyright and license information, please view the LICENSE
6
+ * file that was distributed with this source code.
6
7
*
7
- * @author Mike van Riel <[email protected] >
8
- * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
8
+ * @copyright 2010-2015 Mike van Riel<[email protected] >
9
9
* @license http://www.opensource.org/licenses/mit-license.php MIT
10
10
* @link http://phpdoc.org
11
11
*/
12
12
13
13
namespace phpDocumentor \Reflection \DocBlock \Tags ;
14
14
15
- use phpDocumentor \Reflection \DocBlock \Tag ;
15
+ use phpDocumentor \Reflection \DocBlock \Description ;
16
+ use phpDocumentor \Reflection \DocBlock \DescriptionFactory ;
17
+ use phpDocumentor \Reflection \Type ;
18
+ use phpDocumentor \Reflection \TypeResolver ;
19
+ use phpDocumentor \Reflection \Types \Context ;
20
+ use phpDocumentor \Reflection \Types \Void ;
21
+ use Webmozart \Assert \Assert ;
16
22
17
23
/**
18
- * Reflection class for a @method in a Docblock.
19
- *
20
- * @author Mike van Riel <[email protected] >
21
- * @license http://www.opensource.org/licenses/mit-license.php MIT
22
- * @link http://phpdoc.org
24
+ * Reflection class for an {@}method in a Docblock.
23
25
*/
24
- class Method extends Return_
26
+ final class Method extends BaseTag
25
27
{
28
+ protected $ name = 'method ' ;
26
29
27
30
/** @var string */
28
- protected $ method_name = '' ;
31
+ private $ methodName = '' ;
29
32
30
- /** @var string */
31
- protected $ arguments = '' ;
33
+ /** @var string[] */
34
+ private $ arguments = [] ;
32
35
33
36
/** @var bool */
34
- protected $ isStatic = false ;
35
-
36
- /**
37
- * {@inheritdoc}
38
- */
39
- public function getContent ()
40
- {
41
- if (null === $ this ->description ) {
42
- $ this ->description = '' ;
43
- if ($ this ->isStatic ) {
44
- $ this ->description .= 'static ' ;
45
- }
46
- $ this ->description .= $ this ->type .
47
- " {$ this ->method_name }( {$ this ->arguments }) " .
48
- $ this ->description ;
37
+ private $ isStatic = false ;
38
+
39
+ /** @var Type */
40
+ private $ returnType ;
41
+
42
+ public function __construct (
43
+ $ methodName ,
44
+ array $ arguments = [],
45
+ Type $ returnType = null ,
46
+ $ static = false ,
47
+ Description $ description = null
48
+ ) {
49
+ Assert::stringNotEmpty ($ methodName );
50
+ Assert::boolean ($ static );
51
+
52
+ if ($ returnType === null ) {
53
+ $ returnType = new Void ();
49
54
}
50
55
51
- return $ this ->description ;
56
+ $ this ->methodName = $ methodName ;
57
+ $ this ->arguments = $ this ->filterArguments ($ arguments );
58
+ $ this ->returnType = $ returnType ;
59
+ $ this ->isStatic = $ static ;
60
+ $ this ->description = $ description ;
52
61
}
53
62
54
63
/**
55
64
* {@inheritdoc}
56
65
*/
57
- public function setContent ($ content )
58
- {
59
- Tag::setContent ($ content );
66
+ public static function create (
67
+ $ body ,
68
+ TypeResolver $ typeResolver = null ,
69
+ DescriptionFactory $ descriptionFactory = null ,
70
+ Context $ context = null
71
+ ) {
72
+ Assert::stringNotEmpty ($ body );
73
+ Assert::allNotNull ([ $ typeResolver , $ descriptionFactory ]);
74
+
60
75
// 1. none or more whitespace
61
76
// 2. optionally the keyword "static" followed by whitespace
62
77
// 3. optionally a word with underscores followed by whitespace : as
@@ -66,10 +81,10 @@ public function setContent($content)
66
81
// 5. then a word with underscores, followed by ( and any character
67
82
// until a ) and whitespace : as method name with signature
68
83
// 6. any remaining text : as description
69
- if (preg_match (
84
+ if (! preg_match (
70
85
'/^
71
86
# Static keyword
72
- # Declates a static method ONLY if type is also present
87
+ # Declares a static method ONLY if type is also present
73
88
(?:
74
89
(static)
75
90
\s+
@@ -91,47 +106,36 @@ public function setContent($content)
91
106
# Description
92
107
(.*)
93
108
$/sux ' ,
94
- $ this -> description ,
109
+ $ body ,
95
110
$ matches
96
111
)) {
97
- list (
98
- ,
99
- $ static ,
100
- $ this ->type ,
101
- $ this ->method_name ,
102
- $ this ->arguments ,
103
- $ this ->description
104
- ) = $ matches ;
105
- if ($ static ) {
106
- if (!$ this ->type ) {
107
- $ this ->type = 'static ' ;
108
- } else {
109
- $ this ->isStatic = true ;
110
- }
112
+ return null ;
113
+ }
114
+
115
+ list (, $ static , $ returnType , $ methodName , $ arguments , $ description ) = $ matches ;
116
+
117
+ $ static = $ static === 'static ' ;
118
+ $ returnType = $ typeResolver ->resolve ($ returnType , $ context );
119
+ $ description = $ descriptionFactory ->create ($ description , $ context );
120
+
121
+ $ arguments = explode (', ' , $ arguments );
122
+ foreach ($ arguments as &$ argument ) {
123
+ $ argument = explode (' ' , trim ($ argument ));
124
+ if ($ argument [0 ][0 ] === '$ ' ) {
125
+ $ argumentName = substr ($ argument [0 ], 1 );
126
+ $ argumentType = new Void ();
111
127
} else {
112
- if (!$ this ->type ) {
113
- $ this ->type = 'void ' ;
128
+ $ argumentType = $ typeResolver ->resolve ($ argument [0 ], $ context );
129
+ $ argumentName = '' ;
130
+ if (isset ($ argument [1 ])) {
131
+ $ argumentName = substr ($ argument [1 ], 1 );
114
132
}
115
133
}
116
- $ this ->parsedDescription = null ;
117
- }
118
-
119
- return $ this ;
120
- }
121
134
122
- /**
123
- * Sets the name of this method.
124
- *
125
- * @param string $method_name The name of the method.
126
- *
127
- * @return $this
128
- */
129
- public function setMethodName ($ method_name )
130
- {
131
- $ this ->method_name = $ method_name ;
135
+ $ argument = [ 'name ' => $ argumentName , 'type ' => $ argumentType ];
136
+ }
132
137
133
- $ this ->description = null ;
134
- return $ this ;
138
+ return new static ($ methodName , $ arguments , $ returnType , $ static , $ description );
135
139
}
136
140
137
141
/**
@@ -141,69 +145,66 @@ public function setMethodName($method_name)
141
145
*/
142
146
public function getMethodName ()
143
147
{
144
- return $ this ->method_name ;
145
- }
146
-
147
- /**
148
- * Sets the arguments for this method.
149
- *
150
- * @param string $arguments A comma-separated arguments line.
151
- *
152
- * @return void
153
- */
154
- public function setArguments ($ arguments )
155
- {
156
- $ this ->arguments = $ arguments ;
157
-
158
- $ this ->description = null ;
159
- return $ this ;
148
+ return $ this ->methodName ;
160
149
}
161
150
162
151
/**
163
- * Returns an array containing each argument as array of type and name.
164
- *
165
- * Please note that the argument sub-array may only contain 1 element if no
166
- * type was specified.
167
- *
168
152
* @return string[]
169
153
*/
170
154
public function getArguments ()
171
155
{
172
- if (empty ($ this ->arguments )) {
173
- return array ();
174
- }
175
-
176
- $ arguments = explode (', ' , $ this ->arguments );
177
- foreach ($ arguments as $ key => $ value ) {
178
- $ arguments [$ key ] = explode (' ' , trim ($ value ));
179
- }
180
-
181
- return $ arguments ;
156
+ return $ this ->arguments ;
182
157
}
183
158
184
159
/**
185
160
* Checks whether the method tag describes a static method or not.
186
161
*
187
- * @return bool TRUE if the method declaration is for a static method, FALSE
188
- * otherwise.
162
+ * @return bool TRUE if the method declaration is for a static method, FALSE otherwise.
189
163
*/
190
164
public function isStatic ()
191
165
{
192
166
return $ this ->isStatic ;
193
167
}
194
168
195
169
/**
196
- * Sets a new value for whether the method is static or not.
197
- *
198
- * @param bool $isStatic The new value to set.
199
- *
200
- * @return $this
170
+ * @return Type
201
171
*/
202
- public function setIsStatic ( $ isStatic )
172
+ public function getReturnType ( )
203
173
{
204
- $ this ->isStatic = $ isStatic ;
174
+ return $ this ->returnType ;
175
+ }
176
+
177
+ public function __toString ()
178
+ {
179
+ $ arguments = [];
180
+ foreach ($ this ->arguments as $ argument ) {
181
+ $ arguments [] = $ argument ['type ' ] . ' $ ' . $ argument ['name ' ];
182
+ }
205
183
206
- $ this ->description = null ;
207
- return $ this ;
184
+ return ($ this ->isStatic () ? 'static ' : '' )
185
+ . (string )$ this ->returnType . ' '
186
+ . $ this ->methodName
187
+ . '( ' . implode (', ' , $ arguments ) . ') '
188
+ . ($ this ->description ? ' ' . $ this ->description ->render () : '' );
189
+ }
190
+
191
+ private function filterArguments ($ arguments )
192
+ {
193
+ foreach ($ arguments as &$ argument ) {
194
+ if (is_string ($ argument )) {
195
+ $ argument = [ 'name ' => $ argument ];
196
+ }
197
+ if (! isset ($ argument ['type ' ])) {
198
+ $ argument ['type ' ] = new Void ();
199
+ }
200
+ $ keys = array_keys ($ argument );
201
+ if ($ keys !== [ 'name ' , 'type ' ]) {
202
+ throw new \InvalidArgumentException (
203
+ 'Arguments can only have the "name" and "type" fields, found: ' . var_export ($ keys , true )
204
+ );
205
+ }
206
+ }
207
+
208
+ return $ arguments ;
208
209
}
209
210
}
0 commit comments