Skip to content
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
2 changes: 2 additions & 0 deletions src/phpDocumentor/Reflection/Php/Factory/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use phpDocumentor\Reflection\Location;
use phpDocumentor\Reflection\Php\Class_;
use phpDocumentor\Reflection\Php\Factory\Reducer\Reducer;
use phpDocumentor\Reflection\Php\Interface_;
use phpDocumentor\Reflection\Php\Property as PropertyDescriptor;
use phpDocumentor\Reflection\Php\StrategyContainer;
use phpDocumentor\Reflection\Php\Trait_;
Expand Down Expand Up @@ -69,6 +70,7 @@ protected function doCreate(
[
Class_::class,
Trait_::class,
Interface_::class,
],
);

Expand Down
21 changes: 21 additions & 0 deletions src/phpDocumentor/Reflection/Php/Interface_.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ final class Interface_ implements Element, MetaDataContainerInterface, Attribute
/** @var Method[] */
private array $methods = [];

/** @var Property[] */
private array $properties = [];

private readonly Location $location;

private readonly Location $endLocation;
Expand Down Expand Up @@ -95,6 +98,24 @@ public function addMethod(Method $method): void
$this->methods[(string) $method->getFqsen()] = $method;
}

/**
* Returns the properties of this interface.
*
* @return Property[]
*/
public function getProperties(): array
{
return $this->properties;
}

/**
* Add a property to this interface.
*/
public function addProperty(Property $property): void
{
$this->properties[(string) $property->getFqsen()] = $property;
}

/**
* Returns the Fqsen of the element.
*/
Expand Down
44 changes: 44 additions & 0 deletions tests/integration/InterfacePropertyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace integration;

use EliasHaeussler\PHPUnitAttributes\Attribute\RequiresPackage;
use phpDocumentor\Reflection\File\LocalFile;
use phpDocumentor\Reflection\Php\ProjectFactory;
use phpDocumentor\Reflection\Php\Visibility;
use phpDocumentor\Reflection\Types\Integer;
use phpDocumentor\Reflection\Types\String_;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;

#[RequiresPackage('nikic/php-parser', '>= 5.2')]
#[CoversNothing]
final class InterfacePropertyTest extends TestCase
{
public function testInterfacePropertiesAreParsed(): void
{
$file = __DIR__ . '/data/PHP84/InterfaceProperties.php';
$projectFactory = ProjectFactory::createInstance();
$project = $projectFactory->create('My project', [new LocalFile($file)]);

$interfaces = $project->getFiles()[$file]->getInterfaces();

$hasId = $interfaces['\PHP84\HasId'];
$properties = $hasId->getProperties();
$this->assertCount(1, $properties);
$idProperty = $properties['\PHP84\HasId::$id'];
$this->assertEquals(new Integer(), $idProperty->getType());
$this->assertEquals(new Visibility(Visibility::PUBLIC_), $idProperty->getVisibility());
$this->assertCount(1, $idProperty->getHooks());
$this->assertEquals('get', $idProperty->getHooks()[0]->getName());

$hasName = $interfaces['\PHP84\HasName'];
$properties = $hasName->getProperties();
$this->assertCount(1, $properties);
$nameProperty = $properties['\PHP84\HasName::$name'];
$this->assertEquals(new String_(), $nameProperty->getType());
$this->assertCount(2, $nameProperty->getHooks());
}
}
15 changes: 15 additions & 0 deletions tests/integration/data/PHP84/InterfaceProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace PHP84;

interface HasId
{
public int $id { get; }
}

interface HasName
{
public string $name { get; set; }
}
12 changes: 12 additions & 0 deletions tests/unit/phpDocumentor/Reflection/Php/Interface_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#[UsesClass('\phpDocumentor\Reflection\Php\Method')]
#[UsesClass('\phpDocumentor\Reflection\Php\Constant')]
#[UsesClass('\phpDocumentor\Reflection\Php\Visibility')]
#[UsesClass('\phpDocumentor\Reflection\Php\Property')]
final class Interface_Test extends TestCase
{
use MetadataContainerTestHelper;
Expand Down Expand Up @@ -96,6 +97,17 @@ public function testSettingAndGettingMethods(): void
$this->assertEquals(['\MySpace\MyInterface::myMethod()' => $method], $this->fixture->getMethods());
}

public function testSettingAndGettingProperties(): void
{
$this->assertEquals([], $this->fixture->getProperties());

$property = new Property(new Fqsen('\MySpace\MyInterface::$myProperty'));

$this->fixture->addProperty($property);

$this->assertEquals(['\MySpace\MyInterface::$myProperty' => $property], $this->fixture->getProperties());
}

public function testReturningTheParentsOfThisInterface(): void
{
$this->assertSame($this->exampleParents, $this->fixture->getParents());
Expand Down
Loading