Skip to content

Commit

Permalink
chore: enable contract between documentation strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferror committed Jan 29, 2024
1 parent 4873000 commit d524a7f
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/Attribute/AbstractProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Ferror\AsyncapiDocBundle\Attribute;

abstract class AbstractProperty
abstract class AbstractProperty implements PropertyInterface
{
public function __construct(
public string $name,
Expand Down
13 changes: 12 additions & 1 deletion src/Attribute/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Ferror\AsyncapiDocBundle\Schema\V2\ChannelType;

#[Attribute(Attribute::TARGET_CLASS)]
class Message implements PropertyInterface
class Message
{
/**
* @param PropertyInterface[] $properties
Expand All @@ -35,4 +35,15 @@ public function addProperty(PropertyInterface $property): void
{
$this->properties[] = $property;
}

public function enrich(self $self): self
{
foreach ($this->properties as $property) {
foreach ($self->properties as $selfProperty) {
$property->enrich($selfProperty);

Check failure on line 43 in src/Attribute/Message.php

View workflow job for this annotation

GitHub Actions / CI - PHP 8.2, Dependencies lowest

Parameter #1 $property of method Ferror\AsyncapiDocBundle\Attribute\PropertyInterface::enrich() expects Ferror\AsyncapiDocBundle\Attribute\Property|Ferror\AsyncapiDocBundle\Attribute\PropertyArray|Ferror\AsyncapiDocBundle\Attribute\PropertyArrayObject|Ferror\AsyncapiDocBundle\Attribute\PropertyEnum|Ferror\AsyncapiDocBundle\Attribute\PropertyObject, Ferror\AsyncapiDocBundle\Attribute\PropertyInterface given.

Check failure on line 43 in src/Attribute/Message.php

View workflow job for this annotation

GitHub Actions / CI - PHP 8.2, Dependencies highest

Parameter #1 $property of method Ferror\AsyncapiDocBundle\Attribute\PropertyInterface::enrich() expects Ferror\AsyncapiDocBundle\Attribute\Property|Ferror\AsyncapiDocBundle\Attribute\PropertyArray|Ferror\AsyncapiDocBundle\Attribute\PropertyArrayObject|Ferror\AsyncapiDocBundle\Attribute\PropertyEnum|Ferror\AsyncapiDocBundle\Attribute\PropertyObject, Ferror\AsyncapiDocBundle\Attribute\PropertyInterface given.

Check failure on line 43 in src/Attribute/Message.php

View workflow job for this annotation

GitHub Actions / CI - PHP 8.2, Dependencies lowest

Parameter #1 $property of method Ferror\AsyncapiDocBundle\Attribute\PropertyInterface::enrich() expects Ferror\AsyncapiDocBundle\Attribute\Property|Ferror\AsyncapiDocBundle\Attribute\PropertyArray|Ferror\AsyncapiDocBundle\Attribute\PropertyArrayObject|Ferror\AsyncapiDocBundle\Attribute\PropertyEnum|Ferror\AsyncapiDocBundle\Attribute\PropertyObject, Ferror\AsyncapiDocBundle\Attribute\PropertyInterface given.

Check failure on line 43 in src/Attribute/Message.php

View workflow job for this annotation

GitHub Actions / CI - PHP 8.2, Dependencies highest

Parameter #1 $property of method Ferror\AsyncapiDocBundle\Attribute\PropertyInterface::enrich() expects Ferror\AsyncapiDocBundle\Attribute\Property|Ferror\AsyncapiDocBundle\Attribute\PropertyArray|Ferror\AsyncapiDocBundle\Attribute\PropertyArrayObject|Ferror\AsyncapiDocBundle\Attribute\PropertyEnum|Ferror\AsyncapiDocBundle\Attribute\PropertyObject, Ferror\AsyncapiDocBundle\Attribute\PropertyInterface given.
}
}

return $this;
}
}
25 changes: 21 additions & 4 deletions src/Attribute/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
use Ferror\AsyncapiDocBundle\Schema\PropertyType;

#[Attribute(Attribute::TARGET_PROPERTY)]
class Property extends AbstractProperty implements PropertyInterface
class Property extends AbstractProperty
{
public function __construct(
string $name,
string $description = '',
public readonly PropertyType $type = PropertyType::STRING,
public readonly ?Format $format = null,
public readonly ?string $example = null,
public PropertyType $type = PropertyType::STRING,
public ?Format $format = null,
public ?string $example = null,
bool $required = true,
) {
parent::__construct($name, $description, $required);
Expand All @@ -30,4 +30,21 @@ public function toArray(): array
'example' => $this->example,
]);
}

public function enrich(Property|PropertyArray|PropertyEnum|PropertyObject|PropertyArrayObject $property): void
{
if ($property->name === $this->name && $property::class === $this::class) {
if (empty($this->format)) {
$this->format = $property->format;
}

if ($this->type !== $property->type && $this->type === PropertyType::STRING) {
$this->type = $property->type;
}

if (empty($this->example)) {
$this->example = $property->example;
}
}
}
}
8 changes: 7 additions & 1 deletion src/Attribute/PropertyArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Ferror\AsyncapiDocBundle\Schema\PropertyType;

#[Attribute(Attribute::TARGET_PROPERTY)]
class PropertyArray extends AbstractProperty implements PropertyInterface
class PropertyArray extends AbstractProperty
{
public function __construct(
string $name,
Expand All @@ -31,4 +31,10 @@ public function toArray(): array
'itemsType' => $this->itemsType->value,
]);
}

public function enrich(Property|PropertyArray|PropertyEnum|PropertyObject|PropertyArrayObject $property): void
{
if ($property->name === $this->name && $property::class === $this::class) {
}
}
}
8 changes: 7 additions & 1 deletion src/Attribute/PropertyArrayObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Ferror\AsyncapiDocBundle\Schema\Format;

#[Attribute(Attribute::TARGET_PROPERTY)]
class PropertyArrayObject extends AbstractProperty implements PropertyInterface
class PropertyArrayObject extends AbstractProperty
{
public function __construct(
string $name,
Expand All @@ -29,4 +29,10 @@ public function toArray(): array
'example' => $this->example,
]);
}

public function enrich(Property|PropertyArray|PropertyEnum|PropertyObject|PropertyArrayObject $property): void
{
if ($property->name === $this->name && $property::class === $this::class) {
}
}
}
20 changes: 19 additions & 1 deletion src/Attribute/PropertyEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
use Attribute;
use Ferror\AsyncapiDocBundle\PropertyTypeTranslator;
use Ferror\AsyncapiDocBundle\Schema\Format;
use Ferror\AsyncapiDocBundle\Schema\PropertyType;
use InvalidArgumentException;
use ReflectionEnum;
use ReflectionEnumBackedCase;
use ReflectionException;

#[Attribute(Attribute::TARGET_PROPERTY)]
class PropertyEnum extends AbstractProperty implements PropertyInterface
class PropertyEnum extends AbstractProperty
{
public function __construct(
string $name,
Expand All @@ -25,6 +27,9 @@ public function __construct(
parent::__construct($name, $description, $required);
}

/**
* @throws ReflectionException
*/
public function toArray(): array
{
$enum = new ReflectionEnum($this->enum);
Expand All @@ -43,4 +48,17 @@ public function toArray(): array
'example' => $this->example,
]);
}

public function enrich(Property|PropertyArray|PropertyEnum|PropertyObject|PropertyArrayObject $property): void
{
if ($property->name === $this->name && $property::class === $this::class) {
if (empty($this->format)) {
$this->format = $property->format;
}

if (empty($this->example)) {
$this->example = $property->example;
}
}
}
}
2 changes: 2 additions & 0 deletions src/Attribute/PropertyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
interface PropertyInterface
{
public function toArray(): array;

public function enrich(Property|PropertyArray|PropertyEnum|PropertyObject|PropertyArrayObject $property): void;
}
8 changes: 7 additions & 1 deletion src/Attribute/PropertyObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Attribute;

#[Attribute(Attribute::TARGET_PROPERTY)]
class PropertyObject extends AbstractProperty implements PropertyInterface
class PropertyObject extends AbstractProperty
{
public function __construct(
string $name,
Expand All @@ -26,4 +26,10 @@ public function toArray(): array
'items' => array_map(static fn(PropertyInterface $property) => $property->toArray(), $this->items),
]);
}

public function enrich(Property|PropertyArray|PropertyEnum|PropertyObject|PropertyArrayObject $property): void
{
if ($property->name === $this->name && $property::class === $this::class) {
}
}
}
14 changes: 7 additions & 7 deletions src/DocumentationEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@

namespace Ferror\AsyncapiDocBundle;

use Ferror\AsyncapiDocBundle\DocumentationStrategy\PrioritisedDocumentationStrategy;

use Ferror\AsyncapiDocBundle\Attribute\Message;
use Ferror\AsyncapiDocBundle\DocumentationStrategy\PrioritisedDocumentationStrategyInterface;

final readonly class DocumentationEditor
{
/**
* @param PrioritisedDocumentationStrategy[] $documentationStrategies
* @param PrioritisedDocumentationStrategyInterface[] $documentationStrategies
*/
public function __construct(
private array $documentationStrategies,
) {
}

public function document(string $class): array
public function document(string $class): Message
{
$result = [];
$strategies = [];

foreach ($this->documentationStrategies as $documentationStrategy) {
$strategies[$documentationStrategy->priority] = $documentationStrategy->strategy;
$strategies[$documentationStrategy::getDefaultPriority()] = $documentationStrategy;
}

foreach ($strategies as $documentationStrategy) {

Check failure on line 30 in src/DocumentationEditor.php

View workflow job for this annotation

GitHub Actions / CI - PHP 8.2, Dependencies lowest

Method Ferror\AsyncapiDocBundle\DocumentationEditor::document() should return Ferror\AsyncapiDocBundle\Attribute\Message but return statement is missing.

Check failure on line 30 in src/DocumentationEditor.php

View workflow job for this annotation

GitHub Actions / CI - PHP 8.2, Dependencies highest

Method Ferror\AsyncapiDocBundle\DocumentationEditor::document() should return Ferror\AsyncapiDocBundle\Attribute\Message but return statement is missing.

Check failure on line 30 in src/DocumentationEditor.php

View workflow job for this annotation

GitHub Actions / CI - PHP 8.2, Dependencies lowest

Method Ferror\AsyncapiDocBundle\DocumentationEditor::document() should return Ferror\AsyncapiDocBundle\Attribute\Message but return statement is missing.

Check failure on line 30 in src/DocumentationEditor.php

View workflow job for this annotation

GitHub Actions / CI - PHP 8.2, Dependencies highest

Method Ferror\AsyncapiDocBundle\DocumentationEditor::document() should return Ferror\AsyncapiDocBundle\Attribute\Message but return statement is missing.
$result = array_merge($result, $documentationStrategy->document($class)->toArray());
return $documentationStrategy->document($class);
}

return $result;
}
}
8 changes: 4 additions & 4 deletions src/Schema/V2/MessageRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ public function render(array $document): array
foreach ($document['properties'] as $property) {
$properties[$property['name']]['type'] = PropertyTypeTranslator::translate($property['type']);

if (isset($property['description'])) {
if (!empty($property['description'])) {
$properties[$property['name']]['description'] = $property['description'];
}

if (isset($property['format'])) {
if (!empty($property['format'])) {
$properties[$property['name']]['format'] = $property['format'];
}

if (isset($property['example'])) {
if (!empty($property['example'])) {
$properties[$property['name']]['example'] = $property['example'];
}

if (isset($property['required'])) {
if (isset($property['required']) && $property['required']) {
$required[] = $property['name'];
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Schema/V2/SchemaRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
namespace Ferror\AsyncapiDocBundle\Schema\V2;

use Ferror\AsyncapiDocBundle\ClassFinder\ClassFinderInterface;
use Ferror\AsyncapiDocBundle\DocumentationStrategy\DocumentationStrategyInterface;
use Ferror\AsyncapiDocBundle\DocumentationEditor;
use Ferror\AsyncapiDocBundle\SchemaRendererInterface;

final readonly class SchemaRenderer implements SchemaRendererInterface
{
public function __construct(
private ClassFinderInterface $classFinder,
private DocumentationStrategyInterface $documentationStrategy,
private DocumentationEditor $documentationEditor,
private ChannelRenderer $channelRenderer,
private MessageRenderer $messageRenderer,
private InfoRenderer $infoRenderer,
Expand All @@ -29,7 +29,7 @@ public function generate(): array
$messages = [];

foreach ($classes as $class) {
$document = $this->documentationStrategy->document($class);
$document = $this->documentationEditor->document($class);
$document = $document->toArray();
$channel = $this->channelRenderer->render($document);
$message = $this->messageRenderer->render($document);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/DocumentationEditorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function testDocument(): void
new AttributeDocumentationStrategy(),
]);

$actual = $documentationEditor->document(PaymentExecuted::class);
$actual = $documentationEditor->document(PaymentExecuted::class)->toArray();

$expected = [
'name' => "PaymentExecuted",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Ferror\AsyncapiDocBundle\Tests\Unit\Symfony\Controller;

use Ferror\AsyncapiDocBundle\ClassFinder\ManualClassFinder;
use Ferror\AsyncapiDocBundle\DocumentationEditor;
use Ferror\AsyncapiDocBundle\DocumentationStrategy\AttributeDocumentationStrategy;
use Ferror\AsyncapiDocBundle\DocumentationStrategy\PropertyExtractor;
use Ferror\AsyncapiDocBundle\Generator\JsonGenerator;
Expand All @@ -30,7 +31,9 @@ public function test(): void
PaymentExecuted::class,
ProductCreated::class,
]),
new AttributeDocumentationStrategy(new PropertyExtractor()),
new DocumentationEditor([
new AttributeDocumentationStrategy(new PropertyExtractor())
]),
new ChannelRenderer(),
new MessageRenderer(),
new InfoRenderer('Service Example API', 'This service is in charge of processing user signups', '1.2.3'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Ferror\AsyncapiDocBundle\Tests\Unit\Symfony\Controller;

use Ferror\AsyncapiDocBundle\ClassFinder\ManualClassFinder;
use Ferror\AsyncapiDocBundle\DocumentationEditor;
use Ferror\AsyncapiDocBundle\DocumentationStrategy\AttributeDocumentationStrategy;
use Ferror\AsyncapiDocBundle\DocumentationStrategy\PropertyExtractor;
use Ferror\AsyncapiDocBundle\Generator\YamlGenerator;
Expand All @@ -30,7 +31,9 @@ public function test(): void
PaymentExecuted::class,
ProductCreated::class,
]),
new AttributeDocumentationStrategy(new PropertyExtractor()),
new DocumentationEditor([
new AttributeDocumentationStrategy(new PropertyExtractor())
]),
new ChannelRenderer(),
new MessageRenderer(),
new InfoRenderer('Service Example API', 'This service is in charge of processing user signups', '1.2.3'),
Expand Down

0 comments on commit d524a7f

Please sign in to comment.