Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 31 additions & 0 deletions src/ArrayRepresentationClassProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Sabberworm\CSS;

/**
* Provides a reusable implementation of `getArrayRepresentation()` which will populate the `class` element with the
* short name of the actual class.
* It is expected that this will be `use`d by (possibly-abstract) base classes, with extended classes calling on to the
* parent implementation then further populating the array.
* If a base class using this trait needs to further populate the array, the trait method can be `use`d `as`.
*
* @internal
*
* @phpstan-require-implements Renderable
*/
trait ArrayRepresentationClassProvider
{
/**
* @return array<string, bool|int|float|string|list<array<string, mixed>>>
*
* @internal
*/
public function getArrayRepresentation(): array

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd strongly prefer the trait to not have an implementation of getArrayRepresentation as this can lead to classes with an incomplete implementation if they implement the trait. (Also, I'd like to avoid parent:: calls.)

Instead, I suggest having a private method getShortClassName(): non-empty-string in the trait. Then we can also build the array from scratch in getArrayRepresentation in the implementing classes, allowing for more concise code.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, I suggest having a private method getShortClassName(): non-empty-string in the trait.

Changed as suggested. Also renamed the trait accordingly.

(Also, I'd like to avoid parent:: calls.)
Then we can also build the array from scratch in getArrayRepresentation in the implementing classes, allowing for more concise code.

In cases where a subclass includes additional properties and the parent also has some properties, it would be more concise not to duplicate filling in the parent's properties in the subclass's implementation.

{
$reflect = new \ReflectionClass($this);

return ['class' => $reflect->getShortName()];
Comment thread
oliverklee marked this conversation as resolved.
Outdated
}
}
27 changes: 27 additions & 0 deletions tests/Unit/ArrayRepresentationClassProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Sabberworm\CSS\Tests\Unit;

use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\Tests\Unit\Fixtures\ConcreteArrayRepresentationClassProvider;

/**
* @covers \Sabberworm\CSS\ArrayRepresentationClassProvider
*/
final class ArrayRepresentationClassProviderTest extends TestCase
{
/**
* @test
*/
public function getArrayRepresentationIncludesClassName(): void
{
$subject = new ConcreteArrayRepresentationClassProvider();

$result = $subject->getArrayRepresentation();

self::assertArrayHasKey('class', $result);
self::assertSame('ConcreteArrayRepresentationClassProvider', $result['class']);
}
}
22 changes: 22 additions & 0 deletions tests/Unit/Fixtures/ConcreteArrayRepresentationClassProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Sabberworm\CSS\Tests\Unit\Fixtures;

use Sabberworm\CSS\ArrayRepresentationClassProvider;
use Sabberworm\CSS\OutputFormat;
use Sabberworm\CSS\Renderable;

final class ConcreteArrayRepresentationClassProvider implements Renderable
{
use ArrayRepresentationClassProvider;

/**
* @return never
*/
public function render(OutputFormat $outputFormat): string
{
throw new \BadMethodCallException('Nothing to see here :/', 1767308395);
}
}