From 83a0d034d6d06dac827436db96473b8d4705d8df Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Mon, 16 Sep 2024 18:02:48 +0200 Subject: [PATCH] Enhancement: Executed rector PHP 8.2 set --- public/config.php | 7 +--- public/index.php | 4 +- src/Api/Issue/GithubIssueApi.php | 13 +----- src/Api/Label/GithubLabelApi.php | 28 +------------ src/Api/Milestone/GithubMilestoneApi.php | 14 +------ src/Api/PullRequest/GithubPullRequestApi.php | 7 +--- src/Api/Status/GitHubStatusApi.php | 13 +----- src/Api/Workflow/GithubWorkflowApi.php | 7 +--- src/Command/ListTaskCommand.php | 4 +- src/Command/PingStaleIssuesCommand.php | 13 +----- src/Command/RunTaskCommand.php | 8 +--- src/Entity/Task.php | 42 +++++-------------- src/Event/GitHubEvent.php | 7 +--- src/Model/Repository.php | 31 ++++---------- src/Service/GitHubRequestHandler.php | 13 ++---- src/Service/LabelNameExtractor.php | 23 +++------- src/Service/RepositoryProvider.php | 2 +- src/Service/StaleIssueCommentGenerator.php | 14 +++---- src/Service/SymfonyVersionProvider.php | 16 +------ src/Service/TaskHandler/CloseDraftHandler.php | 11 +---- .../TaskHandler/CloseStaleIssuesHandler.php | 11 +---- .../InformAboutClosingStaleIssuesHandler.php | 13 +----- src/Service/TaskRunner.php | 19 ++++----- src/Service/TaskScheduler.php | 5 +-- .../AllowEditFromMaintainerSubscriber.php | 5 +-- .../ApproveCiForNonContributors.php | 5 +-- .../AutoLabelFromContentSubscriber.php | 16 +++---- .../AutoUpdateTitleWithLabelSubscriber.php | 13 ++---- src/Subscriber/BugLabelNewIssueSubscriber.php | 10 +---- src/Subscriber/CloseDraftPRSubscriber.php | 7 +--- .../MilestoneMergedPRSubscriber.php | 5 +-- src/Subscriber/MilestoneNewPRSubscriber.php | 11 +---- .../MismatchBranchDescriptionSubscriber.php | 9 +--- src/Subscriber/NeedsReviewNewPRSubscriber.php | 5 +-- .../RemoveStalledLabelOnCommentSubscriber.php | 7 +--- .../RewriteUnwantedPhrasesSubscriber.php | 7 +--- .../StatusChangeByCommentSubscriber.php | 5 +-- .../StatusChangeByReviewSubscriber.php | 22 +++------- .../StatusChangeOnPushSubscriber.php | 5 +-- .../UnsupportedBranchSubscriber.php | 9 +--- ...eWhenLabeledWaitingCodeMergeSubscriber.php | 5 +-- .../WelcomeFirstTimeContributorSubscriber.php | 7 +--- tests/Api/Label/GithubLabelApiTest.php | 4 +- tests/Controller/WebhookControllerTest.php | 4 +- 44 files changed, 97 insertions(+), 389 deletions(-) diff --git a/public/config.php b/public/config.php index 1b4ff9f3..a31b89c6 100644 --- a/public/config.php +++ b/public/config.php @@ -4,15 +4,12 @@ exit("This script cannot be run from the CLI. Run it from a browser.\n"); } -if (!in_array(@$_SERVER['REMOTE_ADDR'], array( - '127.0.0.1', - '::1', -))) { +if (!in_array(@$_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1'])) { header('HTTP/1.0 403 Forbidden'); exit('This script is only accessible from localhost.'); } -require_once dirname(__FILE__).'/../app/SymfonyRequirements.php'; +require_once __DIR__.'/../app/SymfonyRequirements.php'; $symfonyRequirements = new SymfonyRequirements(); diff --git a/public/index.php b/public/index.php index 9982c218..cf18ce02 100644 --- a/public/index.php +++ b/public/index.php @@ -4,6 +4,4 @@ require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; -return function (array $context) { - return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']); -}; +return fn(array $context) => new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']); diff --git a/src/Api/Issue/GithubIssueApi.php b/src/Api/Issue/GithubIssueApi.php index 7b6ba46d..b8ac1596 100644 --- a/src/Api/Issue/GithubIssueApi.php +++ b/src/Api/Issue/GithubIssueApi.php @@ -10,19 +10,8 @@ class GithubIssueApi implements IssueApi { - private $resultPager; - private $issueCommentApi; - private $issueApi; - private $searchApi; - private $botUsername; - - public function __construct(ResultPager $resultPager, Comments $issueCommentApi, Issue $issueApi, Search $searchApi, string $botUsername) + public function __construct(private readonly ResultPager $resultPager, private readonly Comments $issueCommentApi, private readonly Issue $issueApi, private readonly Search $searchApi, private readonly string $botUsername) { - $this->resultPager = $resultPager; - $this->issueCommentApi = $issueCommentApi; - $this->issueApi = $issueApi; - $this->searchApi = $searchApi; - $this->botUsername = $botUsername; } public function open(Repository $repository, string $title, string $body, array $labels) diff --git a/src/Api/Label/GithubLabelApi.php b/src/Api/Label/GithubLabelApi.php index 350bc9b7..72443e1a 100644 --- a/src/Api/Label/GithubLabelApi.php +++ b/src/Api/Label/GithubLabelApi.php @@ -22,32 +22,8 @@ class GithubLabelApi implements LabelApi */ private $labelCache = []; - /** - * @var Labels - */ - private $labelsApi; - - /** - * @var ResultPager - */ - private $resultPager; - - /** - * @var CacheInterface - */ - private $cache; - - /** - * @var LoggerInterface - */ - private $logger; - - public function __construct(Labels $labelsApi, ResultPager $resultPager, CacheInterface $cache, LoggerInterface $logger) + public function __construct(private readonly Labels $labelsApi, private readonly ResultPager $resultPager, private readonly CacheInterface $cache, private readonly LoggerInterface $logger) { - $this->labelsApi = $labelsApi; - $this->resultPager = $resultPager; - $this->cache = $cache; - $this->logger = $logger; } public function getIssueLabels($issueNumber, Repository $repository): array @@ -146,7 +122,7 @@ public function getComponentLabelsForRepository(Repository $repository): array $item->expiresAfter(86400); $componentLabels = []; foreach ($labels as $label) { - if ('dddddd' === strtolower($label['color'])) { + if ('dddddd' === strtolower((string) $label['color'])) { $componentLabels[] = $label['name']; } } diff --git a/src/Api/Milestone/GithubMilestoneApi.php b/src/Api/Milestone/GithubMilestoneApi.php index bfdcf9bf..12fb564c 100644 --- a/src/Api/Milestone/GithubMilestoneApi.php +++ b/src/Api/Milestone/GithubMilestoneApi.php @@ -11,25 +11,13 @@ */ class GithubMilestoneApi implements MilestoneApi { - /** - * @var Milestones - */ - private $milestonesApi; - - /** - * @var Issue - */ - private $issuesApi; - /** * @var string[][] */ private $cache = []; - public function __construct(Milestones $milestonesApi, Issue $issuesApi) + public function __construct(private readonly Milestones $milestonesApi, private readonly Issue $issuesApi) { - $this->milestonesApi = $milestonesApi; - $this->issuesApi = $issuesApi; } private function getMilestones(Repository $repository): array diff --git a/src/Api/PullRequest/GithubPullRequestApi.php b/src/Api/PullRequest/GithubPullRequestApi.php index 3c3e4e3f..f91750ac 100644 --- a/src/Api/PullRequest/GithubPullRequestApi.php +++ b/src/Api/PullRequest/GithubPullRequestApi.php @@ -11,13 +11,8 @@ */ class GithubPullRequestApi implements PullRequestApi { - private $pullRequest; - private $search; - - public function __construct(PullRequest $pullRequest, Search $search) + public function __construct(private readonly PullRequest $pullRequest, private readonly Search $search) { - $this->pullRequest = $pullRequest; - $this->search = $search; } public function show(Repository $repository, $number): array diff --git a/src/Api/Status/GitHubStatusApi.php b/src/Api/Status/GitHubStatusApi.php index c0e7cd02..058681f2 100644 --- a/src/Api/Status/GitHubStatusApi.php +++ b/src/Api/Status/GitHubStatusApi.php @@ -17,20 +17,9 @@ class GitHubStatusApi implements StatusApi private $labelToStatus = []; - /** - * @var LabelApi - */ - private $labelsApi; - /** - * @var LoggerInterface - */ - private $logger; - - public function __construct(LabelApi $labelsApi, LoggerInterface $logger) + public function __construct(private readonly LabelApi $labelsApi, private readonly LoggerInterface $logger) { - $this->labelsApi = $labelsApi; $this->labelToStatus = array_flip(self::$statusToLabel); - $this->logger = $logger; } /** diff --git a/src/Api/Workflow/GithubWorkflowApi.php b/src/Api/Workflow/GithubWorkflowApi.php index 66d7ed65..1a9c871b 100644 --- a/src/Api/Workflow/GithubWorkflowApi.php +++ b/src/Api/Workflow/GithubWorkflowApi.php @@ -11,13 +11,8 @@ */ class GithubWorkflowApi implements WorkflowApi { - private ResultPager $resultPager; - private WorkflowRuns $workflowApi; - - public function __construct(ResultPager $resultPager, WorkflowRuns $workflowApi) + public function __construct(private readonly ResultPager $resultPager, private readonly WorkflowRuns $workflowApi) { - $this->resultPager = $resultPager; - $this->workflowApi = $workflowApi; } public function approveWorkflowsForPullRequest(Repository $repository, string $headRepository, string $headBranch): void diff --git a/src/Command/ListTaskCommand.php b/src/Command/ListTaskCommand.php index 09db1c3f..7e4e539e 100644 --- a/src/Command/ListTaskCommand.php +++ b/src/Command/ListTaskCommand.php @@ -18,12 +18,10 @@ class ListTaskCommand extends Command { protected static $defaultName = 'app:task:list'; - private $repository; - public function __construct(TaskRepository $repository) + public function __construct(private readonly TaskRepository $repository) { parent::__construct(); - $this->repository = $repository; } protected function configure() diff --git a/src/Command/PingStaleIssuesCommand.php b/src/Command/PingStaleIssuesCommand.php index 8dbe7a3d..57c72329 100644 --- a/src/Command/PingStaleIssuesCommand.php +++ b/src/Command/PingStaleIssuesCommand.php @@ -27,20 +27,9 @@ class PingStaleIssuesCommand extends Command protected static $defaultName = 'app:issue:ping-stale'; - private $repositoryProvider; - private $issueApi; - private $scheduler; - private $commentGenerator; - private $labelApi; - - public function __construct(RepositoryProvider $repositoryProvider, IssueApi $issueApi, TaskScheduler $scheduler, StaleIssueCommentGenerator $commentGenerator, LabelApi $labelApi) + public function __construct(private readonly RepositoryProvider $repositoryProvider, private readonly IssueApi $issueApi, private readonly TaskScheduler $scheduler, private readonly StaleIssueCommentGenerator $commentGenerator, private readonly LabelApi $labelApi) { parent::__construct(); - $this->repositoryProvider = $repositoryProvider; - $this->issueApi = $issueApi; - $this->scheduler = $scheduler; - $this->commentGenerator = $commentGenerator; - $this->labelApi = $labelApi; } protected function configure() diff --git a/src/Command/RunTaskCommand.php b/src/Command/RunTaskCommand.php index 75f7294d..015dd879 100644 --- a/src/Command/RunTaskCommand.php +++ b/src/Command/RunTaskCommand.php @@ -15,16 +15,10 @@ class RunTaskCommand extends Command { protected static $defaultName = 'app:task:run'; - private $repository; - private $taskRunner; - private $logger; - public function __construct(TaskRepository $repository, TaskRunner $taskRunner, LoggerInterface $logger) + public function __construct(private readonly TaskRepository $repository, private readonly TaskRunner $taskRunner, private readonly LoggerInterface $logger) { parent::__construct(); - $this->repository = $repository; - $this->taskRunner = $taskRunner; - $this->logger = $logger; } protected function configure() diff --git a/src/Entity/Task.php b/src/Entity/Task.php index 7e5942bc..1319767b 100644 --- a/src/Entity/Task.php +++ b/src/Entity/Task.php @@ -32,27 +32,6 @@ class Task */ private $id; - /** - * @var string - * - * @ORM\Column(type="string") - */ - private $repositoryFullName; - - /** - * @var int - * - * @ORM\Column(type="integer") - */ - private $number; - - /** - * @var int - * - * @ORM\Column(type="integer") - */ - private $action; - /** * @var \DateTimeImmutable * @@ -67,19 +46,20 @@ class Task */ private $updatedAt; - /** - * @var \DateTimeImmutable - * + public function __construct(/** + * @ORM\Column(type="string") + */ + private string $repositoryFullName, /** + * @ORM\Column(type="integer") + */ + private int $number, /** + * @ORM\Column(type="integer") + */ + private int $action, /** * @ORM\Column(type="datetime_immutable") */ - private $verifyAfter; - - public function __construct(string $repositoryFullName, int $number, int $action, \DateTimeImmutable $verifyAfter) + private \DateTimeImmutable $verifyAfter) { - $this->repositoryFullName = $repositoryFullName; - $this->number = $number; - $this->action = $action; - $this->verifyAfter = $verifyAfter; $this->createdAt = new \DateTimeImmutable(); } diff --git a/src/Event/GitHubEvent.php b/src/Event/GitHubEvent.php index 43e32801..1c861653 100644 --- a/src/Event/GitHubEvent.php +++ b/src/Event/GitHubEvent.php @@ -12,13 +12,8 @@ class GitHubEvent extends Event { protected $responseData = []; - private $data; - private $repository; - - public function __construct(array $data, Repository $repository) + public function __construct(private readonly array $data, private readonly Repository $repository) { - $this->data = $data; - $this->repository = $repository; } public function getData() diff --git a/src/Model/Repository.php b/src/Model/Repository.php index 56d1d275..e3717c38 100644 --- a/src/Model/Repository.php +++ b/src/Model/Repository.php @@ -9,28 +9,15 @@ */ class Repository { - /** - * @var string - */ - private $vendor; - - /** - * @var string - */ - private $name; - - /** - * The webhook secret used by GitHub. - * - * @var string|null - */ - private $secret; - - public function __construct(string $vendor, string $name, string $secret = null) + public function __construct( + private readonly string $vendor, + private readonly string $name, + /** + * The webhook secret used by GitHub. + */ + private readonly ?string $secret = null + ) { - $this->vendor = $vendor; - $this->name = $name; - $this->secret = $secret; } public function getVendor(): string @@ -54,7 +41,7 @@ public function getNeedsReviewUrl(): string 'https://github.com/%s/%s/labels/%s', $this->getVendor(), $this->getName(), - rawurlencode(GitHubStatusApi::getNeedsReviewLabel()) + rawurlencode((string) GitHubStatusApi::getNeedsReviewLabel()) ); } diff --git a/src/Service/GitHubRequestHandler.php b/src/Service/GitHubRequestHandler.php index 40adde74..8cd8667a 100644 --- a/src/Service/GitHubRequestHandler.php +++ b/src/Service/GitHubRequestHandler.php @@ -17,15 +17,8 @@ */ class GitHubRequestHandler { - private $dispatcher; - private $repositoryProvider; - private $logger; - - public function __construct(EventDispatcher $dispatcher, RepositoryProvider $repositoryProvider, LoggerInterface $logger) + public function __construct(private readonly EventDispatcher $dispatcher, private readonly RepositoryProvider $repositoryProvider, private readonly LoggerInterface $logger) { - $this->dispatcher = $dispatcher; - $this->repositoryProvider = $repositoryProvider; - $this->logger = $logger; } /** @@ -38,7 +31,7 @@ public function handle(Request $request) throw new BadRequestHttpException('Invalid JSON body!'); } - $repositoryFullName = isset($data['repository']['full_name']) ? $data['repository']['full_name'] : null; + $repositoryFullName = $data['repository']['full_name'] ?? null; if (empty($repositoryFullName)) { throw new BadRequestHttpException('No repository name!'); } @@ -94,6 +87,6 @@ private function authenticate($hash, $key, $data) throw new \RuntimeException('"hash" extension is needed to check request signature.'); } - return hash_equals($hash, 'sha1='.hash_hmac('sha1', $data, $key)); + return hash_equals($hash, 'sha1='.hash_hmac('sha1', (string) $data, (string) $key)); } } diff --git a/src/Service/LabelNameExtractor.php b/src/Service/LabelNameExtractor.php index 56426e5e..00fd8abf 100644 --- a/src/Service/LabelNameExtractor.php +++ b/src/Service/LabelNameExtractor.php @@ -13,9 +13,6 @@ */ class LabelNameExtractor { - private $labelsApi; - private $logger; - private static $labelAliases = [ 'bridge\doctrine' => 'DoctrineBridge', 'bridge/doctrine' => 'DoctrineBridge', @@ -35,10 +32,8 @@ class LabelNameExtractor 'wdt' => 'WebProfilerBundle', ]; - public function __construct(LabelApi $labelsApi, LoggerInterface $logger) + public function __construct(private readonly LabelApi $labelsApi, private readonly LoggerInterface $logger) { - $this->labelsApi = $labelsApi; - $this->logger = $logger; } /** @@ -48,14 +43,14 @@ public function __construct(LabelApi $labelsApi, LoggerInterface $logger) public function extractLabels($title, Repository $repository) { $labels = []; - if (preg_match_all('/\[(?P.+)\]/U', $title, $matches)) { + if (preg_match_all('/\[(?P.+)\]/U', (string) $title, $matches)) { $validLabels = $this->getLabels($repository); foreach ($matches['labels'] as $label) { $label = $this->fixLabelName($label); // check case-insensitively, but then apply the correctly-cased label - if (isset($validLabels[strtolower($label)])) { - $labels[] = $validLabels[strtolower($label)]; + if (isset($validLabels[strtolower((string) $label)])) { + $labels[] = $validLabels[strtolower((string) $label)]; } } } @@ -82,9 +77,7 @@ public function getAliasesForLabel($label) private function getLabels(Repository $repository) { $allLabels = $this->labelsApi->getAllLabelsForRepository($repository); - $closure = function ($s) { - return strtolower($s); - }; + $closure = fn($s) => strtolower((string) $s); return array_combine(array_map($closure, $allLabels), $allLabels); } @@ -97,10 +90,6 @@ private function fixLabelName($label) { $labelAliases = self::$labelAliases; - if (isset($labelAliases[strtolower($label)])) { - return $labelAliases[strtolower($label)]; - } - - return $label; + return $labelAliases[strtolower((string) $label)] ?? $label; } } diff --git a/src/Service/RepositoryProvider.php b/src/Service/RepositoryProvider.php index 578d1c7f..8ea19d1b 100644 --- a/src/Service/RepositoryProvider.php +++ b/src/Service/RepositoryProvider.php @@ -21,7 +21,7 @@ public function __construct(array $repositories) throw new \InvalidArgumentException(sprintf('The repository name %s is invalid: it must be the form "username/repo_name"', $repositoryFullName)); } - list($vendorName, $repositoryName) = explode('/', $repositoryFullName); + [$vendorName, $repositoryName] = explode('/', $repositoryFullName); $this->addRepository(new Repository( $vendorName, diff --git a/src/Service/StaleIssueCommentGenerator.php b/src/Service/StaleIssueCommentGenerator.php index 32021dde..9e64fb5b 100644 --- a/src/Service/StaleIssueCommentGenerator.php +++ b/src/Service/StaleIssueCommentGenerator.php @@ -47,15 +47,11 @@ public function getClosingComment(): string */ public function getComment(string $type): string { - switch ($type) { - case IssueType::BUG: - return $this->bug(); - case IssueType::FEATURE: - case IssueType::RFC: - return $this->feature(); - default: - return $this->unknown(); - } + return match ($type) { + IssueType::BUG => $this->bug(), + IssueType::FEATURE, IssueType::RFC => $this->feature(), + default => $this->unknown(), + }; } private function bug(): string diff --git a/src/Service/SymfonyVersionProvider.php b/src/Service/SymfonyVersionProvider.php index 3fcd334b..b6d53bf8 100644 --- a/src/Service/SymfonyVersionProvider.php +++ b/src/Service/SymfonyVersionProvider.php @@ -10,20 +10,8 @@ class SymfonyVersionProvider { - /** - * @var HttpClientInterface - */ - private $httpClient; - - /** - * @var CacheInterface - */ - private $cache; - - public function __construct(HttpClientInterface $httpClient, CacheInterface $cache) + public function __construct(private HttpClientInterface $httpClient, private readonly CacheInterface $cache) { - $this->httpClient = $httpClient; - $this->cache = $cache; } public function getCurrentVersion(): string @@ -36,7 +24,7 @@ public function getCurrentVersion(): string $response = $httpClient->request('GET', 'https://symfony.com/releases.json'); $data = $response->toArray(true); $version = $data['latest_stable_version'] ?? $defaultValue; - } catch (\Throwable $e) { + } catch (\Throwable) { $version = $defaultValue; } diff --git a/src/Service/TaskHandler/CloseDraftHandler.php b/src/Service/TaskHandler/CloseDraftHandler.php index f8eb5132..2ebee590 100644 --- a/src/Service/TaskHandler/CloseDraftHandler.php +++ b/src/Service/TaskHandler/CloseDraftHandler.php @@ -15,17 +15,8 @@ */ class CloseDraftHandler implements TaskHandlerInterface { - private $issueApi; - private $repositoryProvider; - private $pullRequestApi; - private $logger; - - public function __construct(PullRequestApi $pullRequestApi, IssueApi $issueApi, RepositoryProvider $repositoryProvider, LoggerInterface $logger) + public function __construct(private readonly PullRequestApi $pullRequestApi, private readonly IssueApi $issueApi, private readonly RepositoryProvider $repositoryProvider, private readonly LoggerInterface $logger) { - $this->issueApi = $issueApi; - $this->repositoryProvider = $repositoryProvider; - $this->pullRequestApi = $pullRequestApi; - $this->logger = $logger; } public function handle(Task $task): void diff --git a/src/Service/TaskHandler/CloseStaleIssuesHandler.php b/src/Service/TaskHandler/CloseStaleIssuesHandler.php index 42ee6e8c..25124f0f 100644 --- a/src/Service/TaskHandler/CloseStaleIssuesHandler.php +++ b/src/Service/TaskHandler/CloseStaleIssuesHandler.php @@ -15,17 +15,8 @@ */ class CloseStaleIssuesHandler implements TaskHandlerInterface { - private $issueApi; - private $repositoryProvider; - private $labelApi; - private $commentGenerator; - - public function __construct(LabelApi $labelApi, IssueApi $issueApi, RepositoryProvider $repositoryProvider, StaleIssueCommentGenerator $commentGenerator) + public function __construct(private readonly LabelApi $labelApi, private readonly IssueApi $issueApi, private readonly RepositoryProvider $repositoryProvider, private readonly StaleIssueCommentGenerator $commentGenerator) { - $this->issueApi = $issueApi; - $this->repositoryProvider = $repositoryProvider; - $this->labelApi = $labelApi; - $this->commentGenerator = $commentGenerator; } /** diff --git a/src/Service/TaskHandler/InformAboutClosingStaleIssuesHandler.php b/src/Service/TaskHandler/InformAboutClosingStaleIssuesHandler.php index 26412756..cd08d85b 100644 --- a/src/Service/TaskHandler/InformAboutClosingStaleIssuesHandler.php +++ b/src/Service/TaskHandler/InformAboutClosingStaleIssuesHandler.php @@ -17,19 +17,8 @@ */ class InformAboutClosingStaleIssuesHandler implements TaskHandlerInterface { - private $issueApi; - private $repositoryProvider; - private $labelApi; - private $commentGenerator; - private $scheduler; - - public function __construct(LabelApi $labelApi, IssueApi $issueApi, RepositoryProvider $repositoryProvider, StaleIssueCommentGenerator $commentGenerator, TaskScheduler $scheduler) + public function __construct(private readonly LabelApi $labelApi, private readonly IssueApi $issueApi, private readonly RepositoryProvider $repositoryProvider, private readonly StaleIssueCommentGenerator $commentGenerator, private readonly TaskScheduler $scheduler) { - $this->issueApi = $issueApi; - $this->repositoryProvider = $repositoryProvider; - $this->labelApi = $labelApi; - $this->commentGenerator = $commentGenerator; - $this->scheduler = $scheduler; } /** diff --git a/src/Service/TaskRunner.php b/src/Service/TaskRunner.php index db8e4058..d768c095 100644 --- a/src/Service/TaskRunner.php +++ b/src/Service/TaskRunner.php @@ -12,18 +12,15 @@ */ class TaskRunner { - /** - * @var iterable - */ - private $handlers; - private $repository; - private $logger; - - public function __construct(TaskRepository $repository, iterable $handlers, LoggerInterface $logger) + public function __construct( + private readonly TaskRepository $repository, + /** + * @var iterable + */ + private readonly iterable $handlers, + private readonly LoggerInterface $logger + ) { - $this->handlers = $handlers; - $this->repository = $repository; - $this->logger = $logger; } public function run(Task $task) diff --git a/src/Service/TaskScheduler.php b/src/Service/TaskScheduler.php index 9c46014e..d67fb298 100644 --- a/src/Service/TaskScheduler.php +++ b/src/Service/TaskScheduler.php @@ -15,11 +15,8 @@ */ class TaskScheduler { - private $taskRepo; - - public function __construct(TaskRepository $taskRepo) + public function __construct(private readonly TaskRepository $taskRepo) { - $this->taskRepo = $taskRepo; } public function runLater(Repository $repository, int $number, int $action, \DateTimeImmutable $checkAt) diff --git a/src/Subscriber/AllowEditFromMaintainerSubscriber.php b/src/Subscriber/AllowEditFromMaintainerSubscriber.php index 3b63a39b..020dd96f 100644 --- a/src/Subscriber/AllowEditFromMaintainerSubscriber.php +++ b/src/Subscriber/AllowEditFromMaintainerSubscriber.php @@ -12,11 +12,8 @@ */ class AllowEditFromMaintainerSubscriber implements EventSubscriberInterface { - private $commentsApi; - - public function __construct(IssueApi $commentsApi) + public function __construct(private readonly IssueApi $commentsApi) { - $this->commentsApi = $commentsApi; } public function onPullRequest(GitHubEvent $event) diff --git a/src/Subscriber/ApproveCiForNonContributors.php b/src/Subscriber/ApproveCiForNonContributors.php index 90ae8442..7b1e16a5 100644 --- a/src/Subscriber/ApproveCiForNonContributors.php +++ b/src/Subscriber/ApproveCiForNonContributors.php @@ -14,11 +14,8 @@ */ class ApproveCiForNonContributors implements EventSubscriberInterface { - private WorkflowApi $workflowApi; - - public function __construct(WorkflowApi $workflowApi) + public function __construct(private readonly WorkflowApi $workflowApi) { - $this->workflowApi = $workflowApi; } public function onPullRequest(GitHubEvent $event) diff --git a/src/Subscriber/AutoLabelFromContentSubscriber.php b/src/Subscriber/AutoLabelFromContentSubscriber.php index 96f144ac..b14dccac 100644 --- a/src/Subscriber/AutoLabelFromContentSubscriber.php +++ b/src/Subscriber/AutoLabelFromContentSubscriber.php @@ -13,14 +13,8 @@ */ class AutoLabelFromContentSubscriber implements EventSubscriberInterface { - private $labelsApi; - - private $labelExtractor; - - public function __construct(LabelApi $labelsApi, LabelNameExtractor $labelExtractor) + public function __construct(private readonly LabelApi $labelsApi, private readonly LabelNameExtractor $labelExtractor) { - $this->labelsApi = $labelsApi; - $this->labelExtractor = $labelExtractor; } public function onPullRequest(GitHubEvent $event) @@ -43,16 +37,16 @@ public function onPullRequest(GitHubEvent $event) } // the PR body usually indicates if this is a Bug, Feature, BC Break or Deprecation - if (preg_match('/\|\s*Bug fix\?\s*\|\s*yes\s*/i', $prBody, $matches)) { + if (preg_match('/\|\s*Bug fix\?\s*\|\s*yes\s*/i', (string) $prBody, $matches)) { $prLabels[] = 'Bug'; } - if (preg_match('/\|\s*New feature\?\s*\|\s*yes\s*/i', $prBody, $matches)) { + if (preg_match('/\|\s*New feature\?\s*\|\s*yes\s*/i', (string) $prBody, $matches)) { $prLabels[] = 'Feature'; } - if (preg_match('/\|\s*BC breaks\?\s*\|\s*yes\s*/i', $prBody, $matches)) { + if (preg_match('/\|\s*BC breaks\?\s*\|\s*yes\s*/i', (string) $prBody, $matches)) { $prLabels[] = 'BC Break'; } - if (preg_match('/\|\s*Deprecations\?\s*\|\s*yes\s*/i', $prBody, $matches)) { + if (preg_match('/\|\s*Deprecations\?\s*\|\s*yes\s*/i', (string) $prBody, $matches)) { $prLabels[] = 'Deprecation'; } diff --git a/src/Subscriber/AutoUpdateTitleWithLabelSubscriber.php b/src/Subscriber/AutoUpdateTitleWithLabelSubscriber.php index 0e6626b5..00a60bf0 100644 --- a/src/Subscriber/AutoUpdateTitleWithLabelSubscriber.php +++ b/src/Subscriber/AutoUpdateTitleWithLabelSubscriber.php @@ -16,15 +16,8 @@ */ class AutoUpdateTitleWithLabelSubscriber implements EventSubscriberInterface { - private $labelExtractor; - private $pullRequestApi; - private $lockFactory; - - public function __construct(LabelNameExtractor $labelExtractor, PullRequestApi $pullRequestApi, LockFactory $lockFactory) + public function __construct(private readonly LabelNameExtractor $labelExtractor, private readonly PullRequestApi $pullRequestApi, private readonly LockFactory $lockFactory) { - $this->labelExtractor = $labelExtractor; - $this->pullRequestApi = $pullRequestApi; - $this->lockFactory = $lockFactory; } public function onPullRequest(GitHubEvent $event) @@ -53,7 +46,7 @@ public function onPullRequest(GitHubEvent $event) $validLabels = []; foreach ($githubPullRequest['labels'] ?? [] as $label) { - if ('dddddd' === strtolower($label['color'])) { + if ('dddddd' === strtolower((string) $label['color'])) { $validLabels[] = $label['name']; // Remove label name from title $prTitle = str_ireplace('['.$label['name'].']', '', $prTitle); @@ -80,7 +73,7 @@ public function onPullRequest(GitHubEvent $event) $prTitle = preg_replace('@^[\h\s]+@u', '', html_entity_decode($prTitle)); // Add back labels - $prTitle = trim($prPrefix.' '.trim($prTitle)); + $prTitle = trim($prPrefix.' '.trim((string) $prTitle)); if ($originalTitle === $prTitle) { return; } diff --git a/src/Subscriber/BugLabelNewIssueSubscriber.php b/src/Subscriber/BugLabelNewIssueSubscriber.php index 75a4b4b1..804a32bf 100644 --- a/src/Subscriber/BugLabelNewIssueSubscriber.php +++ b/src/Subscriber/BugLabelNewIssueSubscriber.php @@ -10,14 +10,8 @@ class BugLabelNewIssueSubscriber implements EventSubscriberInterface { - /** - * @var StatusApi - */ - private $statusApi; - - public function __construct(StatusApi $statusApi) + public function __construct(private readonly StatusApi $statusApi) { - $this->statusApi = $statusApi; } /** @@ -33,7 +27,7 @@ public function onIssues(GitHubEvent $event) $responseData = ['issue' => $issueNumber = $data['issue']['number']]; // Ignore non-bugs or issue which already has a status - if ('bug' !== strtolower($data['label']['name']) || null !== $this->statusApi->getIssueStatus($issueNumber, $repository)) { + if ('bug' !== strtolower((string) $data['label']['name']) || null !== $this->statusApi->getIssueStatus($issueNumber, $repository)) { $responseData['status_change'] = null; $event->setResponseData($responseData); diff --git a/src/Subscriber/CloseDraftPRSubscriber.php b/src/Subscriber/CloseDraftPRSubscriber.php index c5250c9b..d6d12392 100644 --- a/src/Subscriber/CloseDraftPRSubscriber.php +++ b/src/Subscriber/CloseDraftPRSubscriber.php @@ -14,13 +14,8 @@ */ class CloseDraftPRSubscriber implements EventSubscriberInterface { - private $issueApi; - private $scheduler; - - public function __construct(IssueApi $issueApi, TaskScheduler $scheduler) + public function __construct(private readonly IssueApi $issueApi, private readonly TaskScheduler $scheduler) { - $this->issueApi = $issueApi; - $this->scheduler = $scheduler; } public function onPullRequest(GitHubEvent $event) diff --git a/src/Subscriber/MilestoneMergedPRSubscriber.php b/src/Subscriber/MilestoneMergedPRSubscriber.php index 3a6dc6bb..49c7d255 100644 --- a/src/Subscriber/MilestoneMergedPRSubscriber.php +++ b/src/Subscriber/MilestoneMergedPRSubscriber.php @@ -12,11 +12,8 @@ */ class MilestoneMergedPRSubscriber implements EventSubscriberInterface { - private $milestonesApi; - - public function __construct(MilestoneApi $milestonesApi) + public function __construct(private readonly MilestoneApi $milestonesApi) { - $this->milestonesApi = $milestonesApi; } /** diff --git a/src/Subscriber/MilestoneNewPRSubscriber.php b/src/Subscriber/MilestoneNewPRSubscriber.php index 99ba7184..51d39982 100644 --- a/src/Subscriber/MilestoneNewPRSubscriber.php +++ b/src/Subscriber/MilestoneNewPRSubscriber.php @@ -13,17 +13,8 @@ */ class MilestoneNewPRSubscriber implements EventSubscriberInterface { - private $milestonesApi; - private $symfonyVersionProvider; - private $ignoreCurrentVersion; - private $ignoreDefaultBranch; - - public function __construct(MilestoneApi $milestonesApi, SymfonyVersionProvider $symfonyVersionProvider, $ignoreCurrentVersion = false, $ignoreDefaultBranch = false) + public function __construct(private readonly MilestoneApi $milestonesApi, private readonly SymfonyVersionProvider $symfonyVersionProvider, private $ignoreCurrentVersion = false, private $ignoreDefaultBranch = false) { - $this->milestonesApi = $milestonesApi; - $this->symfonyVersionProvider = $symfonyVersionProvider; - $this->ignoreCurrentVersion = $ignoreCurrentVersion; - $this->ignoreDefaultBranch = $ignoreDefaultBranch; } /** diff --git a/src/Subscriber/MismatchBranchDescriptionSubscriber.php b/src/Subscriber/MismatchBranchDescriptionSubscriber.php index c47785de..e278dc64 100644 --- a/src/Subscriber/MismatchBranchDescriptionSubscriber.php +++ b/src/Subscriber/MismatchBranchDescriptionSubscriber.php @@ -15,13 +15,8 @@ */ class MismatchBranchDescriptionSubscriber implements EventSubscriberInterface { - private IssueApi $issueApi; - private LoggerInterface $logger; - - public function __construct(IssueApi $issueApi, LoggerInterface $logger) + public function __construct(private readonly IssueApi $issueApi, private readonly LoggerInterface $logger) { - $this->issueApi = $issueApi; - $this->logger = $logger; } public function onPullRequest(GitHubEvent $event): void @@ -83,7 +78,7 @@ private function extractDescriptionBranchFromBody(string $body): ?string $rowDescriptionBranch = $rowsDescriptionBranch[0]; // row matching - $descriptionBranchParts = \explode('|', $rowDescriptionBranch); + $descriptionBranchParts = \explode('|', (string) $rowDescriptionBranch); if (false === array_key_exists(2, $descriptionBranchParts)) { // Branch description is in second Markdown table column return null; } diff --git a/src/Subscriber/NeedsReviewNewPRSubscriber.php b/src/Subscriber/NeedsReviewNewPRSubscriber.php index 11ec513c..7905c547 100644 --- a/src/Subscriber/NeedsReviewNewPRSubscriber.php +++ b/src/Subscriber/NeedsReviewNewPRSubscriber.php @@ -11,11 +11,8 @@ class NeedsReviewNewPRSubscriber implements EventSubscriberInterface { - private $statusApi; - - public function __construct(StatusApi $statusApi) + public function __construct(private readonly StatusApi $statusApi) { - $this->statusApi = $statusApi; } /** diff --git a/src/Subscriber/RemoveStalledLabelOnCommentSubscriber.php b/src/Subscriber/RemoveStalledLabelOnCommentSubscriber.php index 9318f7da..8689e015 100644 --- a/src/Subscriber/RemoveStalledLabelOnCommentSubscriber.php +++ b/src/Subscriber/RemoveStalledLabelOnCommentSubscriber.php @@ -15,13 +15,8 @@ */ class RemoveStalledLabelOnCommentSubscriber implements EventSubscriberInterface { - private $labelApi; - private $botUsername; - - public function __construct(LabelApi $labelApi, string $botUsername) + public function __construct(private readonly LabelApi $labelApi, private readonly string $botUsername) { - $this->labelApi = $labelApi; - $this->botUsername = $botUsername; } public function onIssueComment(GitHubEvent $event) diff --git a/src/Subscriber/RewriteUnwantedPhrasesSubscriber.php b/src/Subscriber/RewriteUnwantedPhrasesSubscriber.php index 3eb5e949..dba74100 100644 --- a/src/Subscriber/RewriteUnwantedPhrasesSubscriber.php +++ b/src/Subscriber/RewriteUnwantedPhrasesSubscriber.php @@ -16,13 +16,8 @@ */ class RewriteUnwantedPhrasesSubscriber implements EventSubscriberInterface { - private $pullRequestApi; - private $lockFactory; - - public function __construct(PullRequestApi $pullRequestApi, LockFactory $lockFactory) + public function __construct(private readonly PullRequestApi $pullRequestApi, private readonly LockFactory $lockFactory) { - $this->pullRequestApi = $pullRequestApi; - $this->lockFactory = $lockFactory; } public function onPullRequest(GitHubEvent $event) diff --git a/src/Subscriber/StatusChangeByCommentSubscriber.php b/src/Subscriber/StatusChangeByCommentSubscriber.php index 909a3284..3a4215c9 100644 --- a/src/Subscriber/StatusChangeByCommentSubscriber.php +++ b/src/Subscriber/StatusChangeByCommentSubscriber.php @@ -10,12 +10,9 @@ class StatusChangeByCommentSubscriber extends AbstractStatusChangeSubscriber { - private $logger; - - public function __construct(StatusApi $statusApi, LoggerInterface $logger) + public function __construct(StatusApi $statusApi, private readonly LoggerInterface $logger) { parent::__construct($statusApi); - $this->logger = $logger; } /** diff --git a/src/Subscriber/StatusChangeByReviewSubscriber.php b/src/Subscriber/StatusChangeByReviewSubscriber.php index 34c4434a..4858c48f 100644 --- a/src/Subscriber/StatusChangeByReviewSubscriber.php +++ b/src/Subscriber/StatusChangeByReviewSubscriber.php @@ -15,12 +15,9 @@ */ class StatusChangeByReviewSubscriber extends AbstractStatusChangeSubscriber { - private $logger; - - public function __construct(StatusApi $statusApi, LoggerInterface $logger) + public function __construct(StatusApi $statusApi, private readonly LoggerInterface $logger) { parent::__construct($statusApi); - $this->logger = $logger; } /** @@ -39,18 +36,11 @@ public function onReview(GitHubEvent $event) $newStatus = null; // Set status based on review state - switch (strtolower($data['review']['state'])) { - case 'approved': - $newStatus = Status::REVIEWED; - - break; - case 'changes_requested': - $newStatus = Status::NEEDS_WORK; - - break; - default: - $newStatus = $this->parseStatusFromText($data['review']['body']); - } + $newStatus = match (strtolower((string) $data['review']['state'])) { + 'approved' => Status::REVIEWED, + 'changes_requested' => Status::NEEDS_WORK, + default => $this->parseStatusFromText($data['review']['body']), + }; if (Status::REVIEWED === $newStatus && false === $this->isUserAllowedToReview($data)) { $newStatus = null; diff --git a/src/Subscriber/StatusChangeOnPushSubscriber.php b/src/Subscriber/StatusChangeOnPushSubscriber.php index d38e03bf..e84b6d6f 100644 --- a/src/Subscriber/StatusChangeOnPushSubscriber.php +++ b/src/Subscriber/StatusChangeOnPushSubscriber.php @@ -17,11 +17,8 @@ */ class StatusChangeOnPushSubscriber implements EventSubscriberInterface { - private $statusApi; - - public function __construct(StatusApi $statusApi) + public function __construct(private readonly StatusApi $statusApi) { - $this->statusApi = $statusApi; } public function onPullRequest(GitHubEvent $event) diff --git a/src/Subscriber/UnsupportedBranchSubscriber.php b/src/Subscriber/UnsupportedBranchSubscriber.php index fb8c8714..683f5df8 100644 --- a/src/Subscriber/UnsupportedBranchSubscriber.php +++ b/src/Subscriber/UnsupportedBranchSubscriber.php @@ -14,15 +14,8 @@ */ class UnsupportedBranchSubscriber implements EventSubscriberInterface { - private $symfonyVersionProvider; - private $issueApi; - private $logger; - - public function __construct(SymfonyVersionProvider $symfonyVersionProvider, IssueApi $issueApi, LoggerInterface $logger) + public function __construct(private readonly SymfonyVersionProvider $symfonyVersionProvider, private readonly IssueApi $issueApi, private readonly LoggerInterface $logger) { - $this->symfonyVersionProvider = $symfonyVersionProvider; - $this->issueApi = $issueApi; - $this->logger = $logger; } /** diff --git a/src/Subscriber/UpdateMilestoneWhenLabeledWaitingCodeMergeSubscriber.php b/src/Subscriber/UpdateMilestoneWhenLabeledWaitingCodeMergeSubscriber.php index 8fb66c11..7a6b5b10 100644 --- a/src/Subscriber/UpdateMilestoneWhenLabeledWaitingCodeMergeSubscriber.php +++ b/src/Subscriber/UpdateMilestoneWhenLabeledWaitingCodeMergeSubscriber.php @@ -14,11 +14,8 @@ */ class UpdateMilestoneWhenLabeledWaitingCodeMergeSubscriber implements EventSubscriberInterface { - private MilestoneApi $milestoneApi; - - public function __construct(MilestoneApi $milestoneApi) + public function __construct(private readonly MilestoneApi $milestoneApi) { - $this->milestoneApi = $milestoneApi; } public function onLabel(GitHubEvent $event) diff --git a/src/Subscriber/WelcomeFirstTimeContributorSubscriber.php b/src/Subscriber/WelcomeFirstTimeContributorSubscriber.php index 69b4a399..97cc0b7c 100644 --- a/src/Subscriber/WelcomeFirstTimeContributorSubscriber.php +++ b/src/Subscriber/WelcomeFirstTimeContributorSubscriber.php @@ -13,13 +13,8 @@ */ class WelcomeFirstTimeContributorSubscriber implements EventSubscriberInterface { - private $commentsApi; - private $pullRequestApi; - - public function __construct(IssueApi $commentsApi, PullRequestApi $pullRequestApi) + public function __construct(private readonly IssueApi $commentsApi, private readonly PullRequestApi $pullRequestApi) { - $this->commentsApi = $commentsApi; - $this->pullRequestApi = $pullRequestApi; } public function onPullRequest(GitHubEvent $event) diff --git a/tests/Api/Label/GithubLabelApiTest.php b/tests/Api/Label/GithubLabelApiTest.php index e62816d0..34be8921 100644 --- a/tests/Api/Label/GithubLabelApiTest.php +++ b/tests/Api/Label/GithubLabelApiTest.php @@ -42,9 +42,7 @@ protected function setUp(): void ->disableOriginalConstructor() ->setMethods(['fetchAll']) ->getMock(); - $resultPager->method('fetchAll')->willReturnCallback(function () { - return $this->backendApi->all('x', 'y'); - }); + $resultPager->method('fetchAll')->willReturnCallback(fn() => $this->backendApi->all('x', 'y')); $this->api = new GithubLabelApi($this->backendApi, $resultPager, new NullAdapter(), new NullLogger()); $this->repository = new Repository( diff --git a/tests/Controller/WebhookControllerTest.php b/tests/Controller/WebhookControllerTest.php index e62c9a49..6d50fa7e 100644 --- a/tests/Controller/WebhookControllerTest.php +++ b/tests/Controller/WebhookControllerTest.php @@ -49,8 +49,8 @@ public function testIssueComment($eventHeader, $payloadFilename, $expectedRespon $client->request('POST', '/webhooks/github', [], [], ['HTTP_X-Github-Event' => $eventHeader], $body); $response = $client->getResponse(); - $responseData = json_decode($response->getContent(), true); - $this->assertResponseIsSuccessful(isset($responseData['error']) ? $responseData['error'] : 'An error occurred.'); + $responseData = json_decode((string) $response->getContent(), true); + $this->assertResponseIsSuccessful($responseData['error'] ?? 'An error occurred.'); // a weak sanity check that we went down "the right path" in the controller $this->assertSame($expectedResponse, $responseData);