Added support for static method declarations at MethodTag and according unit tests.

Fixed MethodTag::getContent() to actually return the content as opposed to $this;
This commit is contained in:
Vasil Rangelov
2013-05-16 22:17:15 +03:00
parent 76619d4a16
commit 63c9de4e8b
2 changed files with 88 additions and 21 deletions
@@ -29,6 +29,9 @@ class MethodTag extends ReturnTag
/** @var string */
protected $arguments = '';
/** @var bool */
protected $isStatic = false;
/**
* {@inheritdoc}
@@ -36,12 +39,16 @@ class MethodTag extends ReturnTag
public function getContent()
{
if (null === $this->content) {
$this->content = $this->type .
$this->content = '';
if ($this->isStatic) {
$this->content .= 'static ';
}
$this->content .= $this->type .
" {$this->method_name}({$this->arguments}) " .
$this->description;
}
return $this;
return $this->content;
}
/**
@@ -51,15 +58,22 @@ class MethodTag extends ReturnTag
{
Tag::setContent($content);
// 1. none or more whitespace
// 2. optionally a word with underscores followed by whitespace : as
// 2. optionally the keyword "static" followed by whitespace
// 3. optionally a word with underscores followed by whitespace : as
// type for the return value
// 3. then optionally a word with underscores followed by () and
// 4. then optionally a word with underscores followed by () and
// whitespace : as method name as used by phpDocumentor
// 4. then a word with underscores, followed by ( and any character
// 5. then a word with underscores, followed by ( and any character
// until a ) and whitespace : as method name with signature
// 5. any remaining text : as description
// 6. any remaining text : as description
if (preg_match(
'/^
# Static keyword
# Declates a static method ONLY if type is also present
(?:
(static)
\s+
)?
# Return type
(?:
([\w\|_\\\\]+)
@@ -82,13 +96,22 @@ class MethodTag extends ReturnTag
)) {
list(
,
$static,
$this->type,
$this->method_name,
$this->arguments,
$this->description
) = $matches;
if (!$this->type) {
$this->type = 'void';
if ($static) {
if (!$this->type) {
$this->type = 'static';
} else {
$this->isStatic = true;
}
} else {
if (!$this->type) {
$this->type = 'void';
}
}
$this->parsedDescription = null;
} else {
@@ -160,4 +183,30 @@ class MethodTag extends ReturnTag
return $arguments;
}
/**
* Checks whether the method tag describes a static method or not.
*
* @return bool TRUE if the method declaration is for a static method, FALSE
* otherwise.
*/
public function isStatic()
{
return $this->isStatic;
}
/**
* Sets a new value for whether the method is static or not.
*
* @param bool $isStatic The new value to set.
*
* @return $this
*/
public function setIsStatic($isStatic)
{
$this->isStatic = $isStatic;
$this->content = null;
return $this;
}
}
@@ -44,6 +44,7 @@ class MethodTagTest extends \PHPUnit_Framework_TestCase
$valid,
$expected_name,
$expected_return,
$expected_isStatic,
$paramCount,
$description
) {
@@ -64,6 +65,7 @@ class MethodTagTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expected_name, $tag->getMethodName());
$this->assertEquals($expected_return, $tag->getType());
$this->assertEquals($description, $tag->getDescription());
$this->assertEquals($expected_isStatic, $tag->isStatic());
$this->assertCount($paramCount, $tag->getArguments());
}
@@ -72,56 +74,72 @@ class MethodTagTest extends \PHPUnit_Framework_TestCase
return array(
array(
'foo',
false, 'foo', '', 0, ''
false, 'foo', '', false, 0, ''
),
array(
'foo()',
true, 'foo', 'void', 0, ''
true, 'foo', 'void', false, 0, ''
),
array(
'foo() description',
true, 'foo', 'void', 0, 'description'
true, 'foo', 'void', false, 0, 'description'
),
array(
'int foo()',
true, 'foo', 'int', 0, ''
true, 'foo', 'int', false, 0, ''
),
array(
'int foo() description',
true, 'foo', 'int', 0, 'description'
true, 'foo', 'int', false, 0, 'description'
),
array(
'int foo($a, $b)',
true, 'foo', 'int', 2, ''
true, 'foo', 'int', false, 2, ''
),
array(
'int foo() foo(int $a, int $b)',
true, 'foo', 'int', 2, ''
true, 'foo', 'int', false, 2, ''
),
array(
'int foo(int $a, int $b)',
true, 'foo', 'int', 2, ''
true, 'foo', 'int', false, 2, ''
),
array(
'null|int foo(int $a, int $b)',
true, 'foo', 'null|int', 2, ''
true, 'foo', 'null|int', false, 2, ''
),
array(
'int foo(null|int $a, int $b)',
true, 'foo', 'int', 2, ''
true, 'foo', 'int', false, 2, ''
),
array(
'\Exception foo() foo(Exception $a, Exception $b)',
true, 'foo', '\Exception', 2, ''
true, 'foo', '\Exception', false, 2, ''
),
array(
'int foo() foo(Exception $a, Exception $b) description',
true, 'foo', 'int', 2, 'description'
true, 'foo', 'int', false, 2, 'description'
),
array(
'int foo() foo(\Exception $a, \Exception $b) description',
true, 'foo', 'int', 2, 'description'
true, 'foo', 'int', false, 2, 'description'
),
array(
'void()',
true, 'void', 'void', false, 0, ''
),
array(
'static foo()',
true, 'foo', 'static', false, 0, ''
),
array(
'static void foo()',
true, 'foo', 'void', true, 0, ''
),
array(
'static static foo()',
true, 'foo', 'static', true, 0, ''
)
);
}
}