Skip to content

Commit 63c9de4

Browse files
committed
Added support for static method declarations at MethodTag and according unit tests.
Fixed MethodTag::getContent() to actually return the content as opposed to $this;
1 parent 76619d4 commit 63c9de4

File tree

2 files changed

+88
-21
lines changed

2 files changed

+88
-21
lines changed

src/phpDocumentor/Reflection/DocBlock/Tag/MethodTag.php

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,26 @@ class MethodTag extends ReturnTag
2929

3030
/** @var string */
3131
protected $arguments = '';
32+
33+
/** @var bool */
34+
protected $isStatic = false;
3235

3336
/**
3437
* {@inheritdoc}
3538
*/
3639
public function getContent()
3740
{
3841
if (null === $this->content) {
39-
$this->content = $this->type .
42+
$this->content = '';
43+
if ($this->isStatic) {
44+
$this->content .= 'static ';
45+
}
46+
$this->content .= $this->type .
4047
" {$this->method_name}({$this->arguments}) " .
4148
$this->description;
4249
}
4350

44-
return $this;
51+
return $this->content;
4552
}
4653

4754
/**
@@ -51,15 +58,22 @@ public function setContent($content)
5158
{
5259
Tag::setContent($content);
5360
// 1. none or more whitespace
54-
// 2. optionally a word with underscores followed by whitespace : as
61+
// 2. optionally the keyword "static" followed by whitespace
62+
// 3. optionally a word with underscores followed by whitespace : as
5563
// type for the return value
56-
// 3. then optionally a word with underscores followed by () and
64+
// 4. then optionally a word with underscores followed by () and
5765
// whitespace : as method name as used by phpDocumentor
58-
// 4. then a word with underscores, followed by ( and any character
66+
// 5. then a word with underscores, followed by ( and any character
5967
// until a ) and whitespace : as method name with signature
60-
// 5. any remaining text : as description
68+
// 6. any remaining text : as description
6169
if (preg_match(
6270
'/^
71+
# Static keyword
72+
# Declates a static method ONLY if type is also present
73+
(?:
74+
(static)
75+
\s+
76+
)?
6377
# Return type
6478
(?:
6579
([\w\|_\\\\]+)
@@ -82,13 +96,22 @@ public function setContent($content)
8296
)) {
8397
list(
8498
,
99+
$static,
85100
$this->type,
86101
$this->method_name,
87102
$this->arguments,
88103
$this->description
89104
) = $matches;
90-
if (!$this->type) {
91-
$this->type = 'void';
105+
if ($static) {
106+
if (!$this->type) {
107+
$this->type = 'static';
108+
} else {
109+
$this->isStatic = true;
110+
}
111+
} else {
112+
if (!$this->type) {
113+
$this->type = 'void';
114+
}
92115
}
93116
$this->parsedDescription = null;
94117
} else {
@@ -160,4 +183,30 @@ public function getArguments()
160183

161184
return $arguments;
162185
}
186+
187+
/**
188+
* Checks whether the method tag describes a static method or not.
189+
*
190+
* @return bool TRUE if the method declaration is for a static method, FALSE
191+
* otherwise.
192+
*/
193+
public function isStatic()
194+
{
195+
return $this->isStatic;
196+
}
197+
198+
/**
199+
* Sets a new value for whether the method is static or not.
200+
*
201+
* @param bool $isStatic The new value to set.
202+
*
203+
* @return $this
204+
*/
205+
public function setIsStatic($isStatic)
206+
{
207+
$this->isStatic = $isStatic;
208+
209+
$this->content = null;
210+
return $this;
211+
}
163212
}

tests/phpDocumentor/Reflection/DocBlock/Tag/MethodTagTest.php

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function testConstruct(
4444
$valid,
4545
$expected_name,
4646
$expected_return,
47+
$expected_isStatic,
4748
$paramCount,
4849
$description
4950
) {
@@ -64,6 +65,7 @@ public function testConstruct(
6465
$this->assertEquals($expected_name, $tag->getMethodName());
6566
$this->assertEquals($expected_return, $tag->getType());
6667
$this->assertEquals($description, $tag->getDescription());
68+
$this->assertEquals($expected_isStatic, $tag->isStatic());
6769
$this->assertCount($paramCount, $tag->getArguments());
6870
}
6971

@@ -72,56 +74,72 @@ public function getTestSignatures()
7274
return array(
7375
array(
7476
'foo',
75-
false, 'foo', '', 0, ''
77+
false, 'foo', '', false, 0, ''
7678
),
7779
array(
7880
'foo()',
79-
true, 'foo', 'void', 0, ''
81+
true, 'foo', 'void', false, 0, ''
8082
),
8183
array(
8284
'foo() description',
83-
true, 'foo', 'void', 0, 'description'
85+
true, 'foo', 'void', false, 0, 'description'
8486
),
8587
array(
8688
'int foo()',
87-
true, 'foo', 'int', 0, ''
89+
true, 'foo', 'int', false, 0, ''
8890
),
8991
array(
9092
'int foo() description',
91-
true, 'foo', 'int', 0, 'description'
93+
true, 'foo', 'int', false, 0, 'description'
9294
),
9395
array(
9496
'int foo($a, $b)',
95-
true, 'foo', 'int', 2, ''
97+
true, 'foo', 'int', false, 2, ''
9698
),
9799
array(
98100
'int foo() foo(int $a, int $b)',
99-
true, 'foo', 'int', 2, ''
101+
true, 'foo', 'int', false, 2, ''
100102
),
101103
array(
102104
'int foo(int $a, int $b)',
103-
true, 'foo', 'int', 2, ''
105+
true, 'foo', 'int', false, 2, ''
104106
),
105107
array(
106108
'null|int foo(int $a, int $b)',
107-
true, 'foo', 'null|int', 2, ''
109+
true, 'foo', 'null|int', false, 2, ''
108110
),
109111
array(
110112
'int foo(null|int $a, int $b)',
111-
true, 'foo', 'int', 2, ''
113+
true, 'foo', 'int', false, 2, ''
112114
),
113115
array(
114116
'\Exception foo() foo(Exception $a, Exception $b)',
115-
true, 'foo', '\Exception', 2, ''
117+
true, 'foo', '\Exception', false, 2, ''
116118
),
117119
array(
118120
'int foo() foo(Exception $a, Exception $b) description',
119-
true, 'foo', 'int', 2, 'description'
121+
true, 'foo', 'int', false, 2, 'description'
120122
),
121123
array(
122124
'int foo() foo(\Exception $a, \Exception $b) description',
123-
true, 'foo', 'int', 2, 'description'
125+
true, 'foo', 'int', false, 2, 'description'
124126
),
127+
array(
128+
'void()',
129+
true, 'void', 'void', false, 0, ''
130+
),
131+
array(
132+
'static foo()',
133+
true, 'foo', 'static', false, 0, ''
134+
),
135+
array(
136+
'static void foo()',
137+
true, 'foo', 'void', true, 0, ''
138+
),
139+
array(
140+
'static static foo()',
141+
true, 'foo', 'static', true, 0, ''
142+
)
125143
);
126144
}
127145
}

0 commit comments

Comments
 (0)