Skip to content

Commit

Permalink
Represent inline config as object to hide responsibility for array-ac…
Browse files Browse the repository at this point in the history
…cess to properties inside it

+ add access to raw data to wide compatibility (permit access to not described fields).
  • Loading branch information
Aeliot-Tm committed Jul 1, 2024
1 parent 7b2ffcc commit 3e22215
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 22 deletions.
4 changes: 3 additions & 1 deletion src/ApplicationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Aeliot\TodoRegistrar\Service\FileProcessor;
use Aeliot\TodoRegistrar\Service\InlineConfig\ArrayFromJsonLexerBuilder;
use Aeliot\TodoRegistrar\Service\InlineConfig\ExtrasReader;
use Aeliot\TodoRegistrar\Service\InlineConfig\InlineConfigFactory;
use Aeliot\TodoRegistrar\Service\Registrar\RegistrarFactoryInterface;
use Aeliot\TodoRegistrar\Service\Registrar\RegistrarFactoryRegistry;
use Aeliot\TodoRegistrar\Service\Registrar\RegistrarInterface;
Expand All @@ -35,12 +36,13 @@ public function create(Config $config): Application
private function createCommentRegistrar(RegistrarInterface $registrar, Config $config): CommentRegistrar
{
$inlineConfigReader = $config->getInlineConfigReader() ?? new ExtrasReader(new ArrayFromJsonLexerBuilder());
$inlineConfigFactory = $config->getInlineConfigFactory() ?? new InlineConfigFactory();

return new CommentRegistrar(
new Detector(),
new Extractor(new TagDetector($config->getTags())),
$registrar,
new TodoFactory($inlineConfigReader),
new TodoFactory($inlineConfigFactory, $inlineConfigReader),
);
}

Expand Down
31 changes: 21 additions & 10 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
class Config
{
private Finder $finder;
private ?InlineConfigFactoryInterface $InlineConfigFactory = null;
private ?InlineConfigReaderInterface $inlineConfigReader = null;
/**
* @var array<string,mixed>
Expand All @@ -34,6 +35,26 @@ public function setFinder(Finder $finder): self
return $this;
}

public function getInlineConfigFactory(): ?InlineConfigFactoryInterface
{
return $this->InlineConfigFactory;
}

public function setInlineConfigFactory(?InlineConfigFactoryInterface $InlineConfigFactory): void
{
$this->InlineConfigFactory = $InlineConfigFactory;
}

public function getInlineConfigReader(): ?InlineConfigReaderInterface
{
return $this->inlineConfigReader;
}

public function setInlineConfigReader(?InlineConfigReaderInterface $inlineConfigReader): void
{
$this->inlineConfigReader = $inlineConfigReader;
}

/**
* @return array<string,mixed>
*/
Expand Down Expand Up @@ -75,14 +96,4 @@ public function setTags(array $tags): self

return $this;
}

public function getInlineConfigReader(): ?InlineConfigReaderInterface
{
return $this->inlineConfigReader;
}

public function setInlineConfigReader(?InlineConfigReaderInterface $inlineConfigReader): void
{
$this->inlineConfigReader = $inlineConfigReader;
}
}
50 changes: 50 additions & 0 deletions src/Dto/InlineConfig/InlineConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Aeliot\TodoRegistrar\Dto\InlineConfig;

use Aeliot\TodoRegistrar\InlineConfigInterface;

class InlineConfig implements InlineConfigInterface
{
/**
* @param array<array-key,mixed> $data
*/
public function __construct(
private array $data,
) {
}

/**
* @param int|string $offset
*/
public function offsetExists(mixed $offset): bool
{
return array_key_exists($offset, $this->data);
}

/**
* @param int|string $offset
*/
public function offsetGet(mixed $offset): mixed
{
return $this->data[$offset];
}

/**
* @param int|string $offset
*/
public function offsetSet(mixed $offset, mixed $value): void
{
throw new \BadMethodCallException('Setting value is not allowed.');
}

/**
* @param int|string $offset
*/
public function offsetUnset(mixed $offset): void
{
throw new \BadMethodCallException('Unsetting value is not allowed.');
}
}
9 changes: 4 additions & 5 deletions src/Dto/Registrar/Todo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@

namespace Aeliot\TodoRegistrar\Dto\Registrar;

use Aeliot\TodoRegistrar\InlineConfigInterface;

class Todo
{
/**
* @param array<array-key,mixed> $inlineConfig
*/
public function __construct(
private string $tag,
private string $summary,
private string $description,
private ?string $assignee,
private array $inlineConfig,
private InlineConfigInterface $inlineConfig,
) {
}

Expand All @@ -28,7 +27,7 @@ public function getDescription(): string
return $this->description;
}

public function getInlineConfig(): array
public function getInlineConfig(): InlineConfigInterface
{
return $this->inlineConfig;
}
Expand Down
13 changes: 13 additions & 0 deletions src/InlineConfigFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Aeliot\TodoRegistrar;

interface InlineConfigFactoryInterface
{
/**
* @param array<array-key,mixed> $input
*/
public function getInlineConfig(array $input): InlineConfigInterface;
}
9 changes: 9 additions & 0 deletions src/InlineConfigInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Aeliot\TodoRegistrar;

interface InlineConfigInterface extends \ArrayAccess
{
}
17 changes: 17 additions & 0 deletions src/Service/InlineConfig/InlineConfigFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Aeliot\TodoRegistrar\Service\InlineConfig;

use Aeliot\TodoRegistrar\Dto\InlineConfig\InlineConfig;
use Aeliot\TodoRegistrar\InlineConfigFactoryInterface;
use Aeliot\TodoRegistrar\InlineConfigInterface;

final class InlineConfigFactory implements InlineConfigFactoryInterface
{
public function getInlineConfig(array $input): InlineConfigInterface
{
return new InlineConfig($input);
}
}
14 changes: 12 additions & 2 deletions src/Service/TodoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@

use Aeliot\TodoRegistrar\Dto\Comment\CommentPart;
use Aeliot\TodoRegistrar\Dto\Registrar\Todo;
use Aeliot\TodoRegistrar\InlineConfigFactoryInterface;
use Aeliot\TodoRegistrar\InlineConfigInterface;
use Aeliot\TodoRegistrar\InlineConfigReaderInterface;

class TodoFactory
{
public function __construct(private InlineConfigReaderInterface $inlineConfigReader)
public function __construct(
private InlineConfigFactoryInterface $inlineConfigFactory,
private InlineConfigReaderInterface $inlineConfigReader,
)
{
}

Expand All @@ -23,7 +28,12 @@ public function create(CommentPart $commentPart): Todo
$commentPart->getFirstLine(),
$commentPart->getContent(),
$commentPart->getTagMetadata()?->getAssignee(),
$this->inlineConfigReader->getInlineConfig($description),
$this->getInlineConfig($description),
);
}

private function getInlineConfig(string $description): InlineConfigInterface
{
return $this->inlineConfigFactory->getInlineConfig($this->inlineConfigReader->getInlineConfig($description));
}
}
9 changes: 5 additions & 4 deletions tests/Unit/Service/CommentRegistrarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Aeliot\TodoRegistrar\Service\CommentRegistrar;
use Aeliot\TodoRegistrar\Service\InlineConfig\ArrayFromJsonLexerBuilder;
use Aeliot\TodoRegistrar\Service\InlineConfig\ExtrasReader;
use Aeliot\TodoRegistrar\Service\InlineConfig\InlineConfigFactory;
use Aeliot\TodoRegistrar\Service\Registrar\RegistrarInterface;
use Aeliot\TodoRegistrar\Service\TodoFactory;
use PHPUnit\Framework\Attributes\CoversClass;
Expand All @@ -35,7 +36,7 @@ public function testDontRegisterTwice(): void
$commentParts = $this->createCommentParts($tokens[2]->text);
$commentExtractor = $this->mockCommentExtractor($commentParts);

$todoFactory = new TodoFactory(new ExtrasReader(new ArrayFromJsonLexerBuilder()));
$todoFactory = new TodoFactory(new InlineConfigFactory(), new ExtrasReader(new ArrayFromJsonLexerBuilder()));
$todo = $todoFactory->create($commentParts->getTodos()[0]);

$registrar = $this->mockRegistrar($todo, true);
Expand All @@ -59,7 +60,7 @@ public function testDontRegisterTwiceWithIssueKey(): void
$commentParts = $this->createCommentParts($tokens[2]->text, 'X-001');
$commentExtractor = $this->mockCommentExtractor($commentParts);

$todoFactory = new TodoFactory(new ExtrasReader(new ArrayFromJsonLexerBuilder()));
$todoFactory = new TodoFactory(new InlineConfigFactory(), new ExtrasReader(new ArrayFromJsonLexerBuilder()));

$registrar = $this->createMock(RegistrarInterface::class);
$registrar
Expand All @@ -81,7 +82,7 @@ public function testRegisterNewTodos(): void
$commentExtractor = $this->mockCommentExtractor($commentParts);

$token = $commentParts->getTodos()[0];
$todoFactory = new TodoFactory(new ExtrasReader(new ArrayFromJsonLexerBuilder()));
$todoFactory = new TodoFactory(new InlineConfigFactory(), new ExtrasReader(new ArrayFromJsonLexerBuilder()));
$todo = $todoFactory->create($token);

$registrar = $this->mockRegistrar($todo, false);
Expand Down Expand Up @@ -160,4 +161,4 @@ private function mockRegistrar(Todo $todo, bool $isRegistered): RegistrarInterfa

return $registrar;
}
}
}

0 comments on commit 3e22215

Please sign in to comment.