Skip to content

Commit 7d19c87

Browse files
committed
Add braces support to Property, Property-Read, Property-Write and Var
1 parent e495766 commit 7d19c87

File tree

4 files changed

+53
-101
lines changed

4 files changed

+53
-101
lines changed

src/DocBlock/Tags/Property.php

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,21 @@
2222
/**
2323
* Reflection class for a {@}property tag in a Docblock.
2424
*/
25-
class Property extends BaseTag implements Factory\StaticMethod
25+
class Property extends TagWithType implements Factory\StaticMethod
2626
{
27-
/** @var string */
28-
protected $name = 'property';
29-
30-
/** @var Type */
31-
private $type;
32-
3327
/** @var string */
3428
protected $variableName = '';
3529

3630
/**
37-
* @param string $variableName
38-
* @param Type $type
31+
* @param string $variableName
32+
* @param Type $type
3933
* @param Description $description
4034
*/
4135
public function __construct($variableName, Type $type = null, Description $description = null)
4236
{
4337
Assert::string($variableName);
4438

39+
$this->name = 'property';
4540
$this->variableName = $variableName;
4641
$this->type = $type;
4742
$this->description = $description;
@@ -59,14 +54,17 @@ public static function create(
5954
Assert::stringNotEmpty($body);
6055
Assert::allNotNull([$typeResolver, $descriptionFactory]);
6156

62-
$parts = preg_split('/(\s+)/Su', $body, 3, PREG_SPLIT_DELIM_CAPTURE);
57+
list($firstPart, $body) = self::extractTypeFromBody($body);
6358
$type = null;
59+
$parts = preg_split('/(\s+)/Su', $body, 2, PREG_SPLIT_DELIM_CAPTURE);
6460
$variableName = '';
6561

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

7270
// if the next item starts with a $ or ...$ it must be the variable name
@@ -94,16 +92,6 @@ public function getVariableName()
9492
return $this->variableName;
9593
}
9694

97-
/**
98-
* Returns the variable's type or null if unknown.
99-
*
100-
* @return Type|null
101-
*/
102-
public function getType()
103-
{
104-
return $this->type;
105-
}
106-
10795
/**
10896
* Returns a string representation for this tag.
10997
*
@@ -112,7 +100,7 @@ public function getType()
112100
public function __toString()
113101
{
114102
return ($this->type ? $this->type . ' ' : '')
115-
. '$' . $this->variableName
116-
. ($this->description ? ' ' . $this->description : '');
103+
. '$' . $this->variableName
104+
. ($this->description ? ' ' . $this->description : '');
117105
}
118106
}

src/DocBlock/Tags/PropertyRead.php

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,21 @@
2222
/**
2323
* Reflection class for a {@}property-read tag in a Docblock.
2424
*/
25-
class PropertyRead extends BaseTag implements Factory\StaticMethod
25+
class PropertyRead extends TagWithType implements Factory\StaticMethod
2626
{
27-
/** @var string */
28-
protected $name = 'property-read';
29-
30-
/** @var Type */
31-
private $type;
32-
3327
/** @var string */
3428
protected $variableName = '';
3529

3630
/**
37-
* @param string $variableName
38-
* @param Type $type
31+
* @param string $variableName
32+
* @param Type $type
3933
* @param Description $description
4034
*/
4135
public function __construct($variableName, Type $type = null, Description $description = null)
4236
{
4337
Assert::string($variableName);
4438

39+
$this->name = 'property-read';
4540
$this->variableName = $variableName;
4641
$this->type = $type;
4742
$this->description = $description;
@@ -59,14 +54,17 @@ public static function create(
5954
Assert::stringNotEmpty($body);
6055
Assert::allNotNull([$typeResolver, $descriptionFactory]);
6156

62-
$parts = preg_split('/(\s+)/Su', $body, 3, PREG_SPLIT_DELIM_CAPTURE);
57+
list($firstPart, $body) = self::extractTypeFromBody($body);
6358
$type = null;
59+
$parts = preg_split('/(\s+)/Su', $body, 2, PREG_SPLIT_DELIM_CAPTURE);
6460
$variableName = '';
6561

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

7270
// if the next item starts with a $ or ...$ it must be the variable name
@@ -94,16 +92,6 @@ public function getVariableName()
9492
return $this->variableName;
9593
}
9694

97-
/**
98-
* Returns the variable's type or null if unknown.
99-
*
100-
* @return Type|null
101-
*/
102-
public function getType()
103-
{
104-
return $this->type;
105-
}
106-
10795
/**
10896
* Returns a string representation for this tag.
10997
*
@@ -112,7 +100,7 @@ public function getType()
112100
public function __toString()
113101
{
114102
return ($this->type ? $this->type . ' ' : '')
115-
. '$' . $this->variableName
116-
. ($this->description ? ' ' . $this->description : '');
103+
. '$' . $this->variableName
104+
. ($this->description ? ' ' . $this->description : '');
117105
}
118106
}

src/DocBlock/Tags/PropertyWrite.php

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,21 @@
2222
/**
2323
* Reflection class for a {@}property-write tag in a Docblock.
2424
*/
25-
class PropertyWrite extends BaseTag implements Factory\StaticMethod
25+
class PropertyWrite extends TagWithType implements Factory\StaticMethod
2626
{
27-
/** @var string */
28-
protected $name = 'property-write';
29-
30-
/** @var Type */
31-
private $type;
32-
3327
/** @var string */
3428
protected $variableName = '';
3529

3630
/**
37-
* @param string $variableName
38-
* @param Type $type
31+
* @param string $variableName
32+
* @param Type $type
3933
* @param Description $description
4034
*/
4135
public function __construct($variableName, Type $type = null, Description $description = null)
4236
{
4337
Assert::string($variableName);
4438

39+
$this->name = 'property-write';
4540
$this->variableName = $variableName;
4641
$this->type = $type;
4742
$this->description = $description;
@@ -59,14 +54,17 @@ public static function create(
5954
Assert::stringNotEmpty($body);
6055
Assert::allNotNull([$typeResolver, $descriptionFactory]);
6156

62-
$parts = preg_split('/(\s+)/Su', $body, 3, PREG_SPLIT_DELIM_CAPTURE);
57+
list($firstPart, $body) = self::extractTypeFromBody($body);
6358
$type = null;
59+
$parts = preg_split('/(\s+)/Su', $body, 2, PREG_SPLIT_DELIM_CAPTURE);
6460
$variableName = '';
6561

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

7270
// if the next item starts with a $ or ...$ it must be the variable name
@@ -94,16 +92,6 @@ public function getVariableName()
9492
return $this->variableName;
9593
}
9694

97-
/**
98-
* Returns the variable's type or null if unknown.
99-
*
100-
* @return Type|null
101-
*/
102-
public function getType()
103-
{
104-
return $this->type;
105-
}
106-
10795
/**
10896
* Returns a string representation for this tag.
10997
*
@@ -112,7 +100,7 @@ public function getType()
112100
public function __toString()
113101
{
114102
return ($this->type ? $this->type . ' ' : '')
115-
. '$' . $this->variableName
116-
. ($this->description ? ' ' . $this->description : '');
103+
. '$' . $this->variableName
104+
. ($this->description ? ' ' . $this->description : '');
117105
}
118106
}

src/DocBlock/Tags/Var_.php

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,24 @@
2222
/**
2323
* Reflection class for a {@}var tag in a Docblock.
2424
*/
25-
class Var_ extends BaseTag implements Factory\StaticMethod
25+
class Var_ extends TagWithType implements Factory\StaticMethod
2626
{
27-
/** @var string */
28-
protected $name = 'var';
29-
30-
/** @var Type */
31-
private $type;
32-
3327
/** @var string */
3428
protected $variableName = '';
3529

3630
/**
37-
* @param string $variableName
38-
* @param Type $type
31+
* @param string $variableName
32+
* @param Type $type
3933
* @param Description $description
4034
*/
4135
public function __construct($variableName, Type $type = null, Description $description = null)
4236
{
4337
Assert::string($variableName);
4438

39+
$this->name = 'var';
4540
$this->variableName = $variableName;
46-
$this->type = $type;
47-
$this->description = $description;
41+
$this->type = $type;
42+
$this->description = $description;
4843
}
4944

5045
/**
@@ -59,14 +54,17 @@ public static function create(
5954
Assert::stringNotEmpty($body);
6055
Assert::allNotNull([$typeResolver, $descriptionFactory]);
6156

62-
$parts = preg_split('/(\s+)/Su', $body, 3, PREG_SPLIT_DELIM_CAPTURE);
63-
$type = null;
57+
list($firstPart, $body) = self::extractTypeFromBody($body);
58+
$parts = preg_split('/(\s+)/Su', $body, 2, PREG_SPLIT_DELIM_CAPTURE);
59+
$type = null;
6460
$variableName = '';
6561

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

7270
// if the next item starts with a $ or ...$ it must be the variable name
@@ -94,16 +92,6 @@ public function getVariableName()
9492
return $this->variableName;
9593
}
9694

97-
/**
98-
* Returns the variable's type or null if unknown.
99-
*
100-
* @return Type|null
101-
*/
102-
public function getType()
103-
{
104-
return $this->type;
105-
}
106-
10795
/**
10896
* Returns a string representation for this tag.
10997
*

0 commit comments

Comments
 (0)