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 phpDocumentor\Reflection\Utils; |
22
|
|
|
use Webmozart\Assert\Assert; |
23
|
|
|
use function array_shift; |
24
|
|
|
use function array_unshift; |
25
|
|
|
use function implode; |
26
|
|
|
use function strpos; |
27
|
|
|
use function substr; |
28
|
|
|
use const PREG_SPLIT_DELIM_CAPTURE; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Reflection class for a {@}property-write tag in a Docblock. |
32
|
|
|
*/ |
33
|
|
|
final class PropertyWrite extends TagWithType implements Factory\StaticMethod |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
/** @var string */ |
36
|
|
|
protected $variableName; |
37
|
|
|
|
38
|
|
|
public function __construct(?string $variableName, ?Type $type = null, ?Description $description = null) |
39
|
|
|
{ |
40
|
|
|
Assert::string($variableName); |
41
|
5 |
|
|
42
|
|
|
$this->name = 'property-write'; |
43
|
5 |
|
$this->variableName = $variableName; |
44
|
|
|
$this->type = $type; |
45
|
4 |
|
$this->description = $description; |
46
|
4 |
|
} |
47
|
4 |
|
|
48
|
4 |
|
public static function create( |
49
|
|
|
string $body, |
50
|
|
|
?TypeResolver $typeResolver = null, |
51
|
|
|
?DescriptionFactory $descriptionFactory = null, |
52
|
|
|
?TypeContext $context = null |
53
|
5 |
|
) : self { |
54
|
|
|
Assert::stringNotEmpty($body); |
55
|
|
|
Assert::notNull($typeResolver); |
56
|
|
|
Assert::notNull($descriptionFactory); |
57
|
|
|
|
58
|
|
|
[$firstPart, $body] = self::extractTypeFromBody($body); |
|
|
|
|
59
|
5 |
|
$type = null; |
60
|
3 |
|
$parts = Utils::pregSplit('/(\s+)/Su', $body, 2, PREG_SPLIT_DELIM_CAPTURE); |
61
|
|
|
$variableName = ''; |
62
|
1 |
|
|
63
|
1 |
|
// if the first item that is encountered is not a variable; it is a type |
64
|
1 |
|
if ($firstPart && $firstPart[0] !== '$') { |
65
|
|
|
$type = $typeResolver->resolve($firstPart, $context); |
66
|
|
|
} else { |
67
|
1 |
|
// first part is not a type; we should prepend it to the parts array for further processing |
68
|
1 |
|
array_unshift($parts, $firstPart); |
69
|
1 |
|
} |
70
|
|
|
|
71
|
|
|
// if the next item starts with a $ it must be the variable name |
72
|
|
|
if (isset($parts[0]) && strpos($parts[0], '$') === 0) { |
73
|
1 |
|
$variableName = array_shift($parts); |
74
|
1 |
|
if ($type) { |
75
|
1 |
|
array_shift($parts); |
76
|
|
|
} |
77
|
1 |
|
|
78
|
1 |
|
Assert::notNull($variableName); |
79
|
|
|
|
80
|
|
|
$variableName = substr($variableName, 1); |
81
|
|
|
} |
82
|
1 |
|
|
83
|
|
|
$description = $descriptionFactory->create(implode('', $parts), $context); |
84
|
1 |
|
|
85
|
|
|
return new static($variableName, $type, $description); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the variable's name. |
90
|
|
|
*/ |
91
|
|
|
public function getVariableName() : ?string |
92
|
1 |
|
{ |
93
|
|
|
return $this->variableName; |
94
|
1 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns a string representation for this tag. |
98
|
|
|
*/ |
99
|
|
|
public function __toString() : string |
100
|
|
|
{ |
101
|
|
|
if ($this->description) { |
102
|
1 |
|
$description = $this->description->render(); |
103
|
|
|
} else { |
104
|
1 |
|
$description = ''; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($this->variableName) { |
108
|
|
|
$variableName = '$' . $this->variableName; |
109
|
|
|
} else { |
110
|
|
|
$variableName = ''; |
111
|
|
|
} |
112
|
1 |
|
|
113
|
|
|
$type = (string) $this->type; |
114
|
1 |
|
|
115
|
1 |
|
return $type |
116
|
1 |
|
. ($variableName !== '' ? ($type !== '' ? ' ' : '') . $variableName : '') |
117
|
|
|
. ($description !== '' ? ($type !== '' || $variableName !== '' ? ' ' : '') . $description : ''); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.