Skip to content

Commit 1a5d9a2

Browse files
committed
Add braces support to Property, Property-Read, Property-Write and Var
1 parent bb19583 commit 1a5d9a2

File tree

4 files changed

+50
-87
lines changed

4 files changed

+50
-87
lines changed

src/DocBlock/Tags/Property.php

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,16 @@
2929
/**
3030
* Reflection class for a {@}property tag in a Docblock.
3131
*/
32-
final class Property extends BaseTag implements Factory\StaticMethod
32+
final class Property extends TagWithType implements Factory\StaticMethod
3333
{
34-
/** @var string */
35-
protected $name = 'property';
36-
37-
/** @var Type|null */
38-
private $type;
39-
4034
/** @var string|null */
4135
protected $variableName = '';
4236

4337
public function __construct(?string $variableName, ?Type $type = null, ?Description $description = null)
4438
{
39+
Assert::string($variableName);
40+
41+
$this->name = 'property';
4542
$this->variableName = $variableName;
4643
$this->type = $type;
4744
$this->description = $description;
@@ -60,15 +57,17 @@ public static function create(
6057
Assert::notNull($typeResolver);
6158
Assert::notNull($descriptionFactory);
6259

63-
$parts = preg_split('/(\s+)/Su', $body, 3, PREG_SPLIT_DELIM_CAPTURE);
64-
Assert::isArray($parts);
65-
$type = null;
60+
list($firstPart, $body) = self::extractTypeFromBody($body);
61+
$type = null;
62+
$parts = preg_split('/(\s+)/Su', $body, 2, PREG_SPLIT_DELIM_CAPTURE);
6663
$variableName = '';
6764

6865
// if the first item that is encountered is not a variable; it is a type
69-
if (isset($parts[0]) && ($parts[0] !== '') && ($parts[0][0] !== '$')) {
70-
$type = $typeResolver->resolve(array_shift($parts), $context);
71-
array_shift($parts);
66+
if ($firstPart && (strlen($firstPart) > 0) && ($firstPart[0] !== '$')) {
67+
$type = $typeResolver->resolve($firstPart, $context);
68+
} else {
69+
// first part is not a type; we should prepend it to the parts array for further processing
70+
array_unshift($parts, $firstPart);
7271
}
7372

7473
// if the next item starts with a $ or ...$ it must be the variable name
@@ -94,14 +93,6 @@ public function getVariableName() : ?string
9493
return $this->variableName;
9594
}
9695

97-
/**
98-
* Returns the variable's type or null if unknown.
99-
*/
100-
public function getType() : ?Type
101-
{
102-
return $this->type;
103-
}
104-
10596
/**
10697
* Returns a string representation for this tag.
10798
*/

src/DocBlock/Tags/PropertyRead.php

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,16 @@
2929
/**
3030
* Reflection class for a {@}property-read tag in a Docblock.
3131
*/
32-
final class PropertyRead extends BaseTag implements Factory\StaticMethod
32+
final class PropertyRead extends TagWithType implements Factory\StaticMethod
3333
{
34-
/** @var string */
35-
protected $name = 'property-read';
36-
37-
/** @var Type|null */
38-
private $type;
39-
4034
/** @var string|null */
4135
protected $variableName = '';
4236

4337
public function __construct(?string $variableName, ?Type $type = null, ?Description $description = null)
4438
{
39+
Assert::string($variableName);
40+
41+
$this->name = 'property-read';
4542
$this->variableName = $variableName;
4643
$this->type = $type;
4744
$this->description = $description;
@@ -60,15 +57,17 @@ public static function create(
6057
Assert::notNull($typeResolver);
6158
Assert::notNull($descriptionFactory);
6259

63-
$parts = preg_split('/(\s+)/Su', $body, 3, PREG_SPLIT_DELIM_CAPTURE);
64-
Assert::isArray($parts);
65-
$type = null;
60+
list($firstPart, $body) = self::extractTypeFromBody($body);
61+
$type = null;
62+
$parts = preg_split('/(\s+)/Su', $body, 2, PREG_SPLIT_DELIM_CAPTURE);
6663
$variableName = '';
6764

6865
// if the first item that is encountered is not a variable; it is a type
69-
if (isset($parts[0]) && ($parts[0] !== '') && ($parts[0][0] !== '$')) {
70-
$type = $typeResolver->resolve(array_shift($parts), $context);
71-
array_shift($parts);
66+
if ($firstPart && (strlen($firstPart) > 0) && ($firstPart[0] !== '$')) {
67+
$type = $typeResolver->resolve($firstPart, $context);
68+
} else {
69+
// first part is not a type; we should prepend it to the parts array for further processing
70+
array_unshift($parts, $firstPart);
7271
}
7372

7473
// if the next item starts with a $ or ...$ it must be the variable name
@@ -94,14 +93,6 @@ public function getVariableName() : ?string
9493
return $this->variableName;
9594
}
9695

97-
/**
98-
* Returns the variable's type or null if unknown.
99-
*/
100-
public function getType() : ?Type
101-
{
102-
return $this->type;
103-
}
104-
10596
/**
10697
* Returns a string representation for this tag.
10798
*/

src/DocBlock/Tags/PropertyWrite.php

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,16 @@
3030
/**
3131
* Reflection class for a {@}property-write tag in a Docblock.
3232
*/
33-
final class PropertyWrite extends BaseTag implements Factory\StaticMethod
33+
final class PropertyWrite extends TagWithType implements Factory\StaticMethod
3434
{
3535
/** @var string */
36-
protected $name = 'property-write';
37-
38-
/** @var Type|null */
39-
private $type;
40-
41-
/** @var string|null */
4236
protected $variableName = '';
4337

4438
public function __construct(?string $variableName, ?Type $type = null, ?Description $description = null)
4539
{
40+
Assert::string($variableName);
41+
42+
$this->name = 'property-write';
4643
$this->variableName = $variableName;
4744
$this->type = $type;
4845
$this->description = $description;
@@ -61,15 +58,17 @@ public static function create(
6158
Assert::notNull($typeResolver);
6259
Assert::notNull($descriptionFactory);
6360

64-
$parts = preg_split('/(\s+)/Su', $body, 3, PREG_SPLIT_DELIM_CAPTURE);
65-
Assert::isArray($parts);
66-
$type = null;
61+
list($firstPart, $body) = self::extractTypeFromBody($body);
62+
$type = null;
63+
$parts = preg_split('/(\s+)/Su', $body, 2, PREG_SPLIT_DELIM_CAPTURE);
6764
$variableName = '';
6865

6966
// if the first item that is encountered is not a variable; it is a type
70-
if (isset($parts[0]) && ($parts[0] !== '') && ($parts[0][0] !== '$')) {
71-
$type = $typeResolver->resolve(array_shift($parts), $context);
72-
array_shift($parts);
67+
if ($firstPart && (strlen($firstPart) > 0) && ($firstPart[0] !== '$')) {
68+
$type = $typeResolver->resolve($firstPart, $context);
69+
} else {
70+
// first part is not a type; we should prepend it to the parts array for further processing
71+
array_unshift($parts, $firstPart);
7372
}
7473

7574
// if the next item starts with a $ or ...$ it must be the variable name
@@ -95,14 +94,6 @@ public function getVariableName() : ?string
9594
return $this->variableName;
9695
}
9796

98-
/**
99-
* Returns the variable's type or null if unknown.
100-
*/
101-
public function getType() : ?Type
102-
{
103-
return $this->type;
104-
}
105-
10697
/**
10798
* Returns a string representation for this tag.
10899
*/

src/DocBlock/Tags/Var_.php

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,19 @@
2929
/**
3030
* Reflection class for a {@}var tag in a Docblock.
3131
*/
32-
final class Var_ extends BaseTag implements Factory\StaticMethod
32+
final class Var_ extends TagWithType implements Factory\StaticMethod
3333
{
34-
/** @var string */
35-
protected $name = 'var';
36-
37-
/** @var Type|null */
38-
private $type;
39-
4034
/** @var string|null */
4135
protected $variableName = '';
4236

4337
public function __construct(?string $variableName, ?Type $type = null, ?Description $description = null)
4438
{
39+
Assert::string($variableName);
40+
41+
$this->name = 'var';
4542
$this->variableName = $variableName;
46-
$this->type = $type;
47-
$this->description = $description;
43+
$this->type = $type;
44+
$this->description = $description;
4845
}
4946

5047
/**
@@ -60,16 +57,17 @@ public static function create(
6057
Assert::notNull($typeResolver);
6158
Assert::notNull($descriptionFactory);
6259

63-
$parts = preg_split('/(\s+)/Su', $body, 3, PREG_SPLIT_DELIM_CAPTURE);
64-
Assert::isArray($parts);
65-
Assert::allString($parts);
66-
$type = null;
60+
list($firstPart, $body) = self::extractTypeFromBody($body);
61+
$parts = preg_split('/(\s+)/Su', $body, 2, PREG_SPLIT_DELIM_CAPTURE);
62+
$type = null;
6763
$variableName = '';
6864

6965
// if the first item that is encountered is not a variable; it is a type
70-
if (isset($parts[0]) && ($parts[0] !== '') && ($parts[0][0] !== '$')) {
71-
$type = $typeResolver->resolve(array_shift($parts), $context);
72-
array_shift($parts);
66+
if ($firstPart && (strlen($firstPart) > 0) && ($firstPart[0] !== '$')) {
67+
$type = $typeResolver->resolve($firstPart, $context);
68+
} else {
69+
// first part is not a type; we should prepend it to the parts array for further processing
70+
array_unshift($parts, $firstPart);
7371
}
7472

7573
// if the next item starts with a $ or ...$ it must be the variable name
@@ -95,14 +93,6 @@ public function getVariableName() : ?string
9593
return $this->variableName;
9694
}
9795

98-
/**
99-
* Returns the variable's type or null if unknown.
100-
*/
101-
public function getType() : ?Type
102-
{
103-
return $this->type;
104-
}
105-
10696
/**
10797
* Returns a string representation for this tag.
10898
*/

0 commit comments

Comments
 (0)