Skip to content

Commit dbbc0fc

Browse files
authored
Merge pull request #302 from phpDocumentor/fix/3334
Properties declared in one statement captured default from first
2 parents 8ccd850 + 5f4dc05 commit dbbc0fc

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

src/phpDocumentor/Reflection/Php/Factory/Property.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ protected function doCreate(
7272
]
7373
);
7474

75-
$default = null;
7675
$iterator = new PropertyIterator($object);
77-
if ($iterator->getDefault() !== null) {
78-
$default = $this->valueConverter->prettyPrintExpr($iterator->getDefault());
79-
}
80-
8176
foreach ($iterator as $stmt) {
77+
$default = null;
78+
if ($iterator->getDefault() !== null) {
79+
$default = $this->valueConverter->prettyPrintExpr($iterator->getDefault());
80+
}
81+
8282
$propertyContainer->addProperty(
8383
new PropertyDescriptor(
8484
$stmt->getFqsen(),

tests/unit/phpDocumentor/Reflection/Php/Factory/PropertyTest.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use stdClass;
3131

3232
use function current;
33+
use function next;
3334

3435
/**
3536
* @uses \phpDocumentor\Reflection\Php\Factory\PropertyIterator
@@ -105,6 +106,27 @@ public function testCreateWithDocBlock(): void
105106
$this->assertSame($docBlock, $property->getDocBlock());
106107
}
107108

109+
public function testCreateMultipleInOneStatement(): void
110+
{
111+
$property1 = new PropertyProperty('property1', new String_('MyDefault1'));
112+
$property1->setAttribute('fqsen', new Fqsen('\myClass::$property1'));
113+
$property2 = new PropertyProperty('property2', new String_('MyDefault2'));
114+
$property2->setAttribute('fqsen', new Fqsen('\myClass::$property2'));
115+
$node = new PropertyNode(
116+
ClassNode::MODIFIER_PRIVATE | ClassNode::MODIFIER_STATIC,
117+
[$property1, $property2]
118+
);
119+
120+
$class = $this->performCreate($node);
121+
$properties = $class->getProperties();
122+
$property1 = current($properties);
123+
next($properties);
124+
$property2 = current($properties);
125+
126+
$this->assertProperty($property1, 'private', 'property1', '\'MyDefault1\'');
127+
$this->assertProperty($property2, 'private', 'property2', '\'MyDefault2\'');
128+
}
129+
108130
private function buildPropertyMock(int $modifier): PropertyNode
109131
{
110132
$property = new PropertyProperty('property', new String_('MyDefault'));
@@ -113,12 +135,16 @@ private function buildPropertyMock(int $modifier): PropertyNode
113135
return new PropertyNode($modifier | ClassNode::MODIFIER_STATIC, [$property]);
114136
}
115137

116-
private function assertProperty(PropertyDescriptor $property, string $visibility): void
117-
{
138+
private function assertProperty(
139+
PropertyDescriptor $property,
140+
string $visibility,
141+
string $name = 'property',
142+
?string $default = '\'MyDefault\''
143+
): void {
118144
$this->assertInstanceOf(PropertyDescriptor::class, $property);
119-
$this->assertEquals('\myClass::$property', (string) $property->getFqsen());
145+
$this->assertEquals('\myClass::$' . $name, (string) $property->getFqsen());
120146
$this->assertTrue($property->isStatic());
121-
$this->assertEquals('\'MyDefault\'', $property->getDefault());
147+
$this->assertEquals($default, $property->getDefault());
122148
$this->assertEquals($visibility, (string) $property->getVisibility());
123149
}
124150

0 commit comments

Comments
 (0)