Skip to content

Commit cacac6b

Browse files
authored
Adding return type hints (#136)
* Adding return type hints * Fixed tests
1 parent 8d1c5dc commit cacac6b

File tree

5 files changed

+19
-12
lines changed

5 files changed

+19
-12
lines changed

src/Service/GitHubRequestHandler.php

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public function handle(Request $request)
4646
$this->logger->debug(sprintf('Handling from repository %s', $repositoryFullName));
4747

4848
$repository = $this->repositoryProvider->getRepository($repositoryFullName);
49-
5049
if (!$repository) {
5150
throw new PreconditionFailedHttpException(sprintf('Unsupported repository "%s".', $repositoryFullName));
5251
}

src/Service/RepositoryProvider.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@ public function __construct(array $repositories)
2828
}
2929
}
3030

31-
public function getRepository($repositoryName)
31+
public function getRepository(string $repositoryName): ?Repository
3232
{
3333
$repository = strtolower($repositoryName);
3434

3535
return $this->repositories[$repository] ?? null;
3636
}
3737

38-
public function getAllRepositories()
38+
/**
39+
* @return Repository[]
40+
*/
41+
public function getAllRepositories(): array
3942
{
4043
return array_values($this->repositories);
4144
}

src/Service/TaskHandler/CloseDraftHandler.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\Api\PullRequest\PullRequestApi;
99
use App\Entity\Task;
1010
use App\Service\RepositoryProvider;
11+
use Psr\Log\LoggerInterface;
1112

1213
/**
1314
* @author Tobias Nyholm <[email protected]>
@@ -17,19 +18,26 @@ class CloseDraftHandler implements TaskHandlerInterface
1718
private $issueApi;
1819
private $repositoryProvider;
1920
private $pullRequestApi;
21+
private $logger;
2022

21-
public function __construct(PullRequestApi $pullRequestApi, IssueApi $issueApi, RepositoryProvider $repositoryProvider)
23+
public function __construct(PullRequestApi $pullRequestApi, IssueApi $issueApi, RepositoryProvider $repositoryProvider, LoggerInterface $logger)
2224
{
2325
$this->issueApi = $issueApi;
2426
$this->repositoryProvider = $repositoryProvider;
2527
$this->pullRequestApi = $pullRequestApi;
28+
$this->logger = $logger;
2629
}
2730

2831
public function handle(Task $task): void
2932
{
3033
$repository = $this->repositoryProvider->getRepository($task->getRepositoryFullName());
31-
$pr = $this->pullRequestApi->show($repository, $task->getNumber());
34+
if (!$repository) {
35+
$this->logger->error(sprintf('RepositoryProvider returned nothing for "%s" ', $task->getRepositoryFullName()));
36+
37+
return;
38+
}
3239

40+
$pr = $this->pullRequestApi->show($repository, $task->getNumber());
3341
if ($pr['draft'] ?? false) {
3442
$this->issueApi->close($repository, $task->getNumber());
3543
}

tests/Controller/WebhookControllerTest.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ public function setup()
2121
$statusApi = self::$container->get(StatusApi::class);
2222

2323
// the labels need to be off this issue for one test to pass
24-
$statusApi->setIssueStatus(
25-
2,
26-
null,
27-
$repository->getRepository('carsonbot-playground/symfony')
28-
);
24+
$statusApi->setIssueStatus(2, null, $repository->getRepository('carsonbot-playground/symfony'));
2925
}
3026

3127
/**

tests/Service/TaskHandler/CloseDraftHandlerTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use App\Service\RepositoryProvider;
1111
use App\Service\TaskHandler\CloseDraftHandler;
1212
use PHPUnit\Framework\TestCase;
13+
use Psr\Log\NullLogger;
1314

1415
class CloseDraftHandlerTest extends TestCase
1516
{
@@ -29,7 +30,7 @@ public function testHandleStillInDraft()
2930

3031
$repoProvider = new RepositoryProvider(['carsonbot-playground/symfony' => []]);
3132

32-
$handler = new CloseDraftHandler($prApi, $issueApi, $repoProvider);
33+
$handler = new CloseDraftHandler($prApi, $issueApi, $repoProvider, new NullLogger());
3334
$handler->handle(new Task('carsonbot-playground/symfony', 4711, Task::ACTION_CLOSE_DRAFT, new \DateTimeImmutable()));
3435
}
3536

@@ -49,7 +50,7 @@ public function testHandleNotDraft()
4950

5051
$repoProvider = new RepositoryProvider(['carsonbot-playground/symfony' => []]);
5152

52-
$handler = new CloseDraftHandler($prApi, $issueApi, $repoProvider);
53+
$handler = new CloseDraftHandler($prApi, $issueApi, $repoProvider, new NullLogger());
5354
$handler->handle(new Task('carsonbot-playground/symfony', 4711, Task::ACTION_CLOSE_DRAFT, new \DateTimeImmutable()));
5455
}
5556
}

0 commit comments

Comments
 (0)