Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properties declared in one statement captured default from first #302

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/phpDocumentor/Reflection/Php/Factory/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ protected function doCreate(
]
);

$default = null;
$iterator = new PropertyIterator($object);
if ($iterator->getDefault() !== null) {
$default = $this->valueConverter->prettyPrintExpr($iterator->getDefault());
}

foreach ($iterator as $stmt) {
$default = null;
if ($iterator->getDefault() !== null) {
$default = $this->valueConverter->prettyPrintExpr($iterator->getDefault());
}

$propertyContainer->addProperty(
new PropertyDescriptor(
$stmt->getFqsen(),
Expand Down
34 changes: 30 additions & 4 deletions tests/unit/phpDocumentor/Reflection/Php/Factory/PropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use stdClass;

use function current;
use function next;

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

public function testCreateMultipleInOneStatement(): void
{
$property1 = new PropertyProperty('property1', new String_('MyDefault1'));
$property1->setAttribute('fqsen', new Fqsen('\myClass::$property1'));
$property2 = new PropertyProperty('property2', new String_('MyDefault2'));
$property2->setAttribute('fqsen', new Fqsen('\myClass::$property2'));
$node = new PropertyNode(
ClassNode::MODIFIER_PRIVATE | ClassNode::MODIFIER_STATIC,
[$property1, $property2]
);

$class = $this->performCreate($node);
$properties = $class->getProperties();
$property1 = current($properties);
next($properties);
$property2 = current($properties);

$this->assertProperty($property1, 'private', 'property1', '\'MyDefault1\'');
$this->assertProperty($property2, 'private', 'property2', '\'MyDefault2\'');
}

private function buildPropertyMock(int $modifier): PropertyNode
{
$property = new PropertyProperty('property', new String_('MyDefault'));
Expand All @@ -113,12 +135,16 @@ private function buildPropertyMock(int $modifier): PropertyNode
return new PropertyNode($modifier | ClassNode::MODIFIER_STATIC, [$property]);
}

private function assertProperty(PropertyDescriptor $property, string $visibility): void
{
private function assertProperty(
PropertyDescriptor $property,
string $visibility,
string $name = 'property',
?string $default = '\'MyDefault\''
): void {
$this->assertInstanceOf(PropertyDescriptor::class, $property);
$this->assertEquals('\myClass::$property', (string) $property->getFqsen());
$this->assertEquals('\myClass::$' . $name, (string) $property->getFqsen());
$this->assertTrue($property->isStatic());
$this->assertEquals('\'MyDefault\'', $property->getDefault());
$this->assertEquals($default, $property->getDefault());
$this->assertEquals($visibility, (string) $property->getVisibility());
}

Expand Down