1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This file is part of phpDocumentor. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @link http://phpdoc.org |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace phpDocumentor\Reflection\DocBlock\Tags; |
15
|
|
|
|
16
|
|
|
use phpDocumentor\Reflection\DocBlock\Description; |
17
|
|
|
use phpDocumentor\Reflection\DocBlock\DescriptionFactory; |
18
|
|
|
use phpDocumentor\Reflection\Type; |
19
|
|
|
use phpDocumentor\Reflection\TypeResolver; |
20
|
|
|
use phpDocumentor\Reflection\Types\Context as TypeContext; |
21
|
|
|
use Webmozart\Assert\Assert; |
22
|
|
|
use function preg_split; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Reflection class for a {@}return tag in a Docblock. |
26
|
|
|
*/ |
27
|
|
|
final class Return_ extends BaseTag implements Factory\StaticMethod |
28
|
|
|
{ |
29
|
|
|
/** @var string */ |
30
|
|
|
protected $name = 'return'; |
31
|
|
|
|
32
|
3 |
|
/** @var Type */ |
33
|
|
|
private $type; |
34
|
3 |
|
|
35
|
3 |
|
public function __construct(Type $type, ?Description $description = null) |
36
|
3 |
|
{ |
37
|
|
|
$this->type = $type; |
38
|
|
|
$this->description = $description; |
39
|
|
|
} |
40
|
|
|
|
41
|
5 |
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public static function create( |
45
|
|
|
string $body, |
46
|
|
|
?TypeResolver $typeResolver = null, |
47
|
5 |
|
?DescriptionFactory $descriptionFactory = null, |
48
|
4 |
|
?TypeContext $context = null |
49
|
|
|
) : self { |
50
|
1 |
|
Assert::notNull($typeResolver); |
51
|
|
|
Assert::notNull($descriptionFactory); |
52
|
1 |
|
|
53
|
1 |
|
list($type, $description) = self::splitBodyIntoTypeAndTheRest($body); |
54
|
|
|
|
55
|
1 |
|
$type = $typeResolver->resolve($type, $context); |
56
|
|
|
$description = $descriptionFactory->create($description, $context); |
57
|
|
|
|
58
|
|
|
return new static($type, $description); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns the type section of the variable. |
63
|
1 |
|
*/ |
64
|
|
|
public function getType() : Type |
65
|
1 |
|
{ |
66
|
|
|
return $this->type; |
67
|
|
|
} |
68
|
1 |
|
|
69
|
|
|
public function __toString() : string |
70
|
1 |
|
{ |
71
|
|
|
return $this->type . ' ' . (string) $this->description; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private static function splitBodyIntoTypeAndTheRest(string $body) : array |
75
|
|
|
{ |
76
|
|
|
$type = ''; |
77
|
|
|
$nestingLevel = 0; |
78
|
|
|
for ($i = 0; $i < strlen($body); $i++) { |
79
|
|
|
$character = $body[$i]; |
80
|
|
|
|
81
|
|
|
if (trim($character) === '' && $nestingLevel === 0) { |
82
|
|
|
break; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$type .= $character; |
86
|
|
|
if (in_array($character, ['<', '(', '[', '{'])) { |
87
|
|
|
$nestingLevel++; |
88
|
|
|
} |
89
|
|
|
if (in_array($character, ['>', ')', ']', '}'])) { |
90
|
|
|
$nestingLevel--; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$description = trim(substr($body, strlen($type))); |
95
|
|
|
|
96
|
|
|
return [$type, $description]; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|