Skip to content

Commit de7bc57

Browse files
committed
Refactored method tag and added test
1 parent 3b060b5 commit de7bc57

File tree

2 files changed

+442
-111
lines changed

2 files changed

+442
-111
lines changed

src/DocBlock/Tags/Method.php

Lines changed: 112 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,77 @@
11
<?php
22
/**
3-
* phpDocumentor
3+
* This file is part of phpDocumentor.
44
*
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.
67
*
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]>
99
* @license http://www.opensource.org/licenses/mit-license.php MIT
1010
* @link http://phpdoc.org
1111
*/
1212

1313
namespace phpDocumentor\Reflection\DocBlock\Tags;
1414

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;
1622

1723
/**
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.
2325
*/
24-
class Method extends Return_
26+
final class Method extends BaseTag
2527
{
28+
protected $name = 'method';
2629

2730
/** @var string */
28-
protected $method_name = '';
31+
private $methodName = '';
2932

30-
/** @var string */
31-
protected $arguments = '';
33+
/** @var string[] */
34+
private $arguments = [];
3235

3336
/** @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();
4954
}
5055

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;
5261
}
5362

5463
/**
5564
* {@inheritdoc}
5665
*/
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+
6075
// 1. none or more whitespace
6176
// 2. optionally the keyword "static" followed by whitespace
6277
// 3. optionally a word with underscores followed by whitespace : as
@@ -66,10 +81,10 @@ public function setContent($content)
6681
// 5. then a word with underscores, followed by ( and any character
6782
// until a ) and whitespace : as method name with signature
6883
// 6. any remaining text : as description
69-
if (preg_match(
84+
if (!preg_match(
7085
'/^
7186
# Static keyword
72-
# Declates a static method ONLY if type is also present
87+
# Declares a static method ONLY if type is also present
7388
(?:
7489
(static)
7590
\s+
@@ -91,47 +106,36 @@ public function setContent($content)
91106
# Description
92107
(.*)
93108
$/sux',
94-
$this->description,
109+
$body,
95110
$matches
96111
)) {
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();
111127
} 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);
114132
}
115133
}
116-
$this->parsedDescription = null;
117-
}
118-
119-
return $this;
120-
}
121134

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+
}
132137

133-
$this->description = null;
134-
return $this;
138+
return new static($methodName, $arguments, $returnType, $static, $description);
135139
}
136140

137141
/**
@@ -141,69 +145,66 @@ public function setMethodName($method_name)
141145
*/
142146
public function getMethodName()
143147
{
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;
160149
}
161150

162151
/**
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-
*
168152
* @return string[]
169153
*/
170154
public function getArguments()
171155
{
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;
182157
}
183158

184159
/**
185160
* Checks whether the method tag describes a static method or not.
186161
*
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.
189163
*/
190164
public function isStatic()
191165
{
192166
return $this->isStatic;
193167
}
194168

195169
/**
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
201171
*/
202-
public function setIsStatic($isStatic)
172+
public function getReturnType()
203173
{
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+
}
205183

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;
208209
}
209210
}

0 commit comments

Comments
 (0)