Skip to content

Commit

Permalink
Move responsibility for key injection out of Registrar into CommentRe…
Browse files Browse the repository at this point in the history
…gistrar
  • Loading branch information
Aeliot-Tm committed Jun 12, 2024
1 parent 0c49453 commit 6b67d48
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 25 deletions.
2 changes: 2 additions & 0 deletions 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\Registrar\RegistrarFactory;
use Aeliot\TodoRegistrar\Service\Registrar\RegistrarInterface;
use Aeliot\TodoRegistrar\Service\TodoFactory;

class ApplicationFactory
{
Expand All @@ -33,6 +34,7 @@ private function createCommentRegistrar(RegistrarInterface $registrar): CommentR
new Detector(),
new Extractor(),
$registrar,
new TodoFactory(),
);
}

Expand Down
18 changes: 18 additions & 0 deletions src/Dto/Comment/CommentPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ public function addLine(string $line): void
$this->lines[] = $line;
}

public function getDescription(): string
{
if (!$this->lines) {
throw new NoLineException('Cannot get description till added one line');
}

$prefixLength = (int) $this->tagMetadata?->getPrefixLength();
$lines = array_map(static fn(string $line): string => substr($line, $prefixLength), $this->lines);

return implode('', $lines);
}

public function getFirstLine(): string
{
if (!$this->lines) {
Expand All @@ -44,9 +56,15 @@ public function getLines(): array

public function getPrefixLength(): ?int
{
// THINK: throw exception when there is no prefix
return $this->tagMetadata?->getPrefixLength();
}

public function getSummary(): string
{
return trim(substr($this->getFirstLine(), $this->getPrefixLength()));
}

public function getTag(): ?string
{
return $this->tagMetadata?->getTag();
Expand Down
36 changes: 36 additions & 0 deletions src/Dto/Registrar/Todo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Aeliot\TodoRegistrar\Dto\Registrar;

class Todo
{
public function __construct(
private string $tag,
private string $summary,
private string $description,
private ?string $assignee,
) {
}

public function getAssignee(): ?string
{
return $this->assignee;
}

public function getDescription(): string
{
return $this->description;
}

public function getSummary(): string
{
return $this->summary;
}

public function getTag(): string
{
return $this->tag;
}
}
7 changes: 5 additions & 2 deletions src/Service/CommentRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function __construct(
private CommentDetector $commentDetector,
private CommentExtractor $commentExtractor,
private RegistrarInterface $registrar,
private TodoFactory $todoFactory,
) {
}

Expand All @@ -39,10 +40,12 @@ private function registerTodos(array $tokens): bool
foreach ($tokens as $token) {
$commentParts = $this->commentExtractor->extract($token->text);
foreach ($commentParts->getTodos() as $commentPart) {
if ($this->registrar->isRegistered($commentPart)) {
$todo = $this->todoFactory->create($commentPart);
if ($this->registrar->isRegistered($todo)) {
continue;
}
$this->registrar->register($commentPart);
$key = $this->registrar->register($todo);
$commentPart->injectKey($key);
$hasNewTodo = true;
}

Expand Down
26 changes: 12 additions & 14 deletions src/Service/Registrar/JIRA/JiraRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Aeliot\TodoRegistrar\Service\Registrar\JIRA;

use Aeliot\TodoRegistrar\Dto\Comment\CommentPart;
use Aeliot\TodoRegistrar\Dto\Registrar\Todo;
use Aeliot\TodoRegistrar\Service\Registrar\RegistrarInterface;
use JiraRestApi\Issue\IssueField;
use JiraRestApi\Issue\IssueService;
Expand All @@ -17,38 +17,36 @@ public function __construct(
) {
}

public function isRegistered(CommentPart $commentPart): bool
public function isRegistered(Todo $todo): bool
{
$lineWithoutPrefix = substr($commentPart->getFirstLine(), $commentPart->getPrefixLength());

return preg_match('/^\\s*\\b[A-Z]+-\\d+\\b/i', $lineWithoutPrefix);
return preg_match('/^\\s*\\b[A-Z]+-\\d+\\b/i', $todo->getSummary());
}

public function register(CommentPart $commentPart): void
public function register(Todo $todo): string
{
$issueField = $this->createIssueField($commentPart);
$issue = $this->issueService->create($issueField);
$commentPart->injectKey($issue->key);
$issueField = $this->createIssueField($todo);

return $this->issueService->create($issueField)->key;
}

private function createIssueField(CommentPart $commentPart): IssueField
private function createIssueField(Todo $todo): IssueField
{
$issueField = new IssueField();
$issueField
->setProjectKey($this->issueConfig->getProjectKey())
->setIssueTypeAsString($this->issueConfig->getIssueType())
->setSummary($commentPart->getFirstLine())
->setDescription($commentPart->getContent())
->setSummary($todo->getSummary())
->setDescription($todo->getDescription())
->addComponentsAsArray($this->issueConfig->getComponents());

$assignee = $commentPart->getTagMetadata()?->getAssignee();
$assignee = $todo->getAssignee();
if ($assignee) {
$issueField->setAssigneeNameAsString($assignee);
}

$labels = $this->issueConfig->getLabels();
if ($this->issueConfig->addTagToLabels()) {
$labels[] = strtolower(sprintf('%s%s', $this->issueConfig->getTagPrefix(), $commentPart->getTag()));
$labels[] = strtolower(sprintf('%s%s', $this->issueConfig->getTagPrefix(), $todo->getTag()));
}

foreach ($labels as $label) {
Expand Down
6 changes: 3 additions & 3 deletions src/Service/Registrar/RegistrarInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Aeliot\TodoRegistrar\Service\Registrar;

use Aeliot\TodoRegistrar\Dto\Comment\CommentPart;
use Aeliot\TodoRegistrar\Dto\Registrar\Todo;

interface RegistrarInterface
{
public function isRegistered(CommentPart $commentPart): bool;
public function isRegistered(Todo $todo): bool;

public function register(CommentPart $commentPart): void;
public function register(Todo $todo): string;
}
21 changes: 21 additions & 0 deletions src/Service/TodoFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Aeliot\TodoRegistrar\Service;

use Aeliot\TodoRegistrar\Dto\Comment\CommentPart;
use Aeliot\TodoRegistrar\Dto\Registrar\Todo;

class TodoFactory
{
public function create(CommentPart $commentPart): Todo
{
return new Todo(
$commentPart->getTag(),
$commentPart->getFirstLine(),
$commentPart->getContent(),
$commentPart->getTagMetadata()?->getAssignee(),
);
}
}
20 changes: 14 additions & 6 deletions tests/Unit/Service/CommentRegistrarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

use Aeliot\TodoRegistrar\Dto\Comment\CommentPart;
use Aeliot\TodoRegistrar\Dto\Comment\CommentParts;
use Aeliot\TodoRegistrar\Dto\Registrar\Todo;
use Aeliot\TodoRegistrar\Dto\Tag\TagMetadata;
use Aeliot\TodoRegistrar\Service\Comment\Detector as CommentDetector;
use Aeliot\TodoRegistrar\Service\Comment\Extractor as CommentExtractor;
use Aeliot\TodoRegistrar\Service\CommentRegistrar;
use Aeliot\TodoRegistrar\Service\Registrar\RegistrarInterface;
use Aeliot\TodoRegistrar\Service\TodoFactory;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand All @@ -25,15 +27,16 @@ public function testDontRegisterTwice(): void
$commentParts = $this->createCommentParts();
$commentExtractor = $this->mockCommentExtractor($commentParts);

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

$registrar = $this->mockRegistrar($todo, true);
$registrar
->expects($this->never())
->method('register')
->with($todo);

$commentRegistrar = new CommentRegistrar($commentDetector, $commentExtractor, $registrar);
$commentRegistrar = new CommentRegistrar($commentDetector, $commentExtractor, $registrar, $todoFactory);
$commentRegistrar->register($tokens);
}

Expand All @@ -44,16 +47,21 @@ public function testRegisterNewTodos(): void
$commentParts = $this->createCommentParts();
$commentExtractor = $this->mockCommentExtractor($commentParts);

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

$registrar = $this->mockRegistrar($todo, false);
$registrar
->expects($this->once())
->method('register')
->with($todo);
->with($todo)
->willReturn('X-001');

$commentRegistrar = new CommentRegistrar($commentDetector, $commentExtractor, $registrar);
$commentRegistrar = new CommentRegistrar($commentDetector, $commentExtractor, $registrar, $todoFactory);
$commentRegistrar->register($tokens);

self::assertSame('// TODO X-001 single line comment', $tokens[2]->text);
}

public function createCommentParts(): CommentParts
Expand Down Expand Up @@ -108,7 +116,7 @@ private function mockCommentExtractor(CommentParts $commentParts): CommentExtrac
return $commentExtractor;
}

public function mockRegistrar(CommentPart $todo, bool $isRegistered): RegistrarInterface&MockObject
public function mockRegistrar(Todo $todo, bool $isRegistered): RegistrarInterface&MockObject
{
$registrar = $this->createMock(RegistrarInterface::class);
$registrar
Expand Down

0 comments on commit 6b67d48

Please sign in to comment.