From 3144dfa1849c46f74e7684ac0ea2843fe67bc40f Mon Sep 17 00:00:00 2001 From: George Steel Date: Mon, 29 Aug 2022 11:00:37 +0100 Subject: [PATCH] CS Fixes - Union types for nullables and constructor property promotion - SlevomatCodingStandard.Classes.RequireConstructorPropertyPromotion - SlevomatCodingStandard.TypeHints.UnionTypeHintFormat Signed-off-by: George Steel --- .../Command/BumpChangelogForReleaseBranch.php | 22 +--- .../Command/CreateMergeUpPullRequest.php | 34 ++---- src/Application/Command/CreateMilestones.php | 10 +- src/Application/Command/ReleaseCommand.php | 42 ++----- .../SwitchDefaultBranchToNextMinor.php | 30 ++--- ...ommitChangelogVersionViaKeepAChangelog.php | 21 +--- src/Changelog/ChangelogReleaseNotes.php | 10 +- ...ommitReleaseChangelogViaKeepAChangelog.php | 21 +--- src/Environment/EnvironmentVariables.php | 37 ++----- ...rentUserByReplacingCurrentOriginRemote.php | 5 +- src/Git/Push.php | 2 +- src/Git/PushViaConsole.php | 2 +- .../Value/MergeTargetCandidateBranches.php | 17 +-- src/Git/Value/SemVerVersion.php | 14 +-- .../GetMilestoneChangelog/Response/Author.php | 12 +- .../Response/IssueOrPullRequest.php | 31 ++---- .../GetMilestoneChangelog/Response/Label.php | 15 +-- .../Response/Milestone.php | 32 ++---- ...MilestoneFirst100IssuesAndPullRequests.php | 103 +++++++++--------- src/Github/Api/GraphQL/RunGraphQLQuery.php | 15 +-- .../Api/V3/CreateMilestoneThroughApiCall.php | 21 +--- .../V3/CreatePullRequestThroughApiCall.php | 15 +-- .../Api/V3/CreateReleaseThroughApiCall.php | 16 +-- .../Api/V3/SetDefaultBranchThroughApiCall.php | 15 +-- .../CreateReleaseTextThroughChangelog.php | 21 ++-- .../CreateReleaseTextViaKeepAChangelog.php | 11 +- ...CurrentGithubEventFromGithubActionPath.php | 5 +- src/Github/Event/MilestoneClosedEvent.php | 13 +-- src/Github/JwageGenerateChangelog.php | 11 +- src/Github/MergeMultipleReleaseNotes.php | 2 +- .../Environment/EnvironmentVariablesTest.php | 2 +- 31 files changed, 174 insertions(+), 433 deletions(-) diff --git a/src/Application/Command/BumpChangelogForReleaseBranch.php b/src/Application/Command/BumpChangelogForReleaseBranch.php index 380845cb..7ab6f29b 100644 --- a/src/Application/Command/BumpChangelogForReleaseBranch.php +++ b/src/Application/Command/BumpChangelogForReleaseBranch.php @@ -18,26 +18,14 @@ final class BumpChangelogForReleaseBranch extends Command { - private Variables $environment; - private LoadCurrentGithubEvent $loadEvent; - private Fetch $fetch; - private GetMergeTargetCandidateBranches $getMergeTargets; - private BumpAndCommitChangelogVersion $bumpChangelogVersion; - public function __construct( - Variables $environment, - LoadCurrentGithubEvent $loadEvent, - Fetch $fetch, - GetMergeTargetCandidateBranches $getMergeTargets, - BumpAndCommitChangelogVersion $bumpChangelogVersion, + private readonly Variables $environment, + private readonly LoadCurrentGithubEvent $loadEvent, + private readonly Fetch $fetch, + private readonly GetMergeTargetCandidateBranches $getMergeTargets, + private readonly BumpAndCommitChangelogVersion $bumpChangelogVersion, ) { parent::__construct('laminas:automatic-releases:bump-changelog'); - - $this->environment = $environment; - $this->loadEvent = $loadEvent; - $this->fetch = $fetch; - $this->getMergeTargets = $getMergeTargets; - $this->bumpChangelogVersion = $bumpChangelogVersion; } public function execute(InputInterface $input, OutputInterface $output): int diff --git a/src/Application/Command/CreateMergeUpPullRequest.php b/src/Application/Command/CreateMergeUpPullRequest.php index 9fe0299e..6c34d334 100644 --- a/src/Application/Command/CreateMergeUpPullRequest.php +++ b/src/Application/Command/CreateMergeUpPullRequest.php @@ -23,35 +23,17 @@ final class CreateMergeUpPullRequest extends Command { - private Variables $variables; - private LoadCurrentGithubEvent $loadGithubEvent; - private Fetch $fetch; - private GetMergeTargetCandidateBranches $getMergeCandidates; - private GetGithubMilestone $getMilestone; - private CreateReleaseText $createReleaseText; - private Push $push; - private CreatePullRequest $createPullRequest; - public function __construct( - Variables $variables, - LoadCurrentGithubEvent $loadGithubEvent, - Fetch $fetch, - GetMergeTargetCandidateBranches $getMergeCandidates, - GetGithubMilestone $getMilestone, - CreateReleaseText $createReleaseText, - Push $push, - CreatePullRequest $createPullRequest, + private readonly Variables $variables, + private readonly LoadCurrentGithubEvent $loadGithubEvent, + private readonly Fetch $fetch, + private readonly GetMergeTargetCandidateBranches $getMergeCandidates, + private readonly GetGithubMilestone $getMilestone, + private readonly CreateReleaseText $createReleaseText, + private readonly Push $push, + private readonly CreatePullRequest $createPullRequest, ) { parent::__construct('laminas:automatic-releases:create-merge-up-pull-request'); - - $this->variables = $variables; - $this->loadGithubEvent = $loadGithubEvent; - $this->fetch = $fetch; - $this->getMergeCandidates = $getMergeCandidates; - $this->getMilestone = $getMilestone; - $this->createReleaseText = $createReleaseText; - $this->push = $push; - $this->createPullRequest = $createPullRequest; } public function execute(InputInterface $input, OutputInterface $output): int diff --git a/src/Application/Command/CreateMilestones.php b/src/Application/Command/CreateMilestones.php index 3c580fd1..d864dcef 100644 --- a/src/Application/Command/CreateMilestones.php +++ b/src/Application/Command/CreateMilestones.php @@ -15,17 +15,11 @@ final class CreateMilestones extends Command { - private LoadCurrentGithubEvent $loadEvent; - private CreateMilestone $createMilestone; - public function __construct( - LoadCurrentGithubEvent $loadEvent, - CreateMilestone $createMilestone, + private readonly LoadCurrentGithubEvent $loadEvent, + private readonly CreateMilestone $createMilestone, ) { parent::__construct('laminas:automatic-releases:create-milestones'); - - $this->loadEvent = $loadEvent; - $this->createMilestone = $createMilestone; } public function execute(InputInterface $input, OutputInterface $output): int diff --git a/src/Application/Command/ReleaseCommand.php b/src/Application/Command/ReleaseCommand.php index 52b35826..af346f5a 100644 --- a/src/Application/Command/ReleaseCommand.php +++ b/src/Application/Command/ReleaseCommand.php @@ -23,41 +23,19 @@ final class ReleaseCommand extends Command { - private Variables $environment; - private LoadCurrentGithubEvent $loadEvent; - private Fetch $fetch; - private GetMergeTargetCandidateBranches $getMergeTargets; - private GetGithubMilestone $getMilestone; - private CommitReleaseChangelog $commitChangelog; - private CreateReleaseText $createChangelogText; - private CreateTag $createTag; - private Push $push; - private CreateRelease $createRelease; - public function __construct( - Variables $environment, - LoadCurrentGithubEvent $loadEvent, - Fetch $fetch, - GetMergeTargetCandidateBranches $getMergeTargets, - GetGithubMilestone $getMilestone, - CommitReleaseChangelog $commitChangelog, - CreateReleaseText $createChangelogText, - CreateTag $createTag, - Push $push, - CreateRelease $createRelease, + private readonly Variables $environment, + private readonly LoadCurrentGithubEvent $loadEvent, + private readonly Fetch $fetch, + private readonly GetMergeTargetCandidateBranches $getMergeTargets, + private readonly GetGithubMilestone $getMilestone, + private readonly CommitReleaseChangelog $commitChangelog, + private readonly CreateReleaseText $createChangelogText, + private readonly CreateTag $createTag, + private readonly Push $push, + private readonly CreateRelease $createRelease, ) { parent::__construct('laminas:automatic-releases:release'); - - $this->environment = $environment; - $this->loadEvent = $loadEvent; - $this->fetch = $fetch; - $this->getMergeTargets = $getMergeTargets; - $this->getMilestone = $getMilestone; - $this->commitChangelog = $commitChangelog; - $this->createChangelogText = $createChangelogText; - $this->createTag = $createTag; - $this->push = $push; - $this->createRelease = $createRelease; } public function execute(InputInterface $input, OutputInterface $output): int diff --git a/src/Application/Command/SwitchDefaultBranchToNextMinor.php b/src/Application/Command/SwitchDefaultBranchToNextMinor.php index 87cfc562..8db1ce59 100644 --- a/src/Application/Command/SwitchDefaultBranchToNextMinor.php +++ b/src/Application/Command/SwitchDefaultBranchToNextMinor.php @@ -19,32 +19,16 @@ final class SwitchDefaultBranchToNextMinor extends Command { - private Variables $variables; - private LoadCurrentGithubEvent $loadGithubEvent; - private Fetch $fetch; - private GetMergeTargetCandidateBranches $getMergeCandidates; - private Push $push; - private SetDefaultBranch $switchDefaultBranch; - private BumpAndCommitChangelogVersion $bumpChangelogVersion; - public function __construct( - Variables $variables, - LoadCurrentGithubEvent $loadGithubEvent, - Fetch $fetch, - GetMergeTargetCandidateBranches $getMergeCandidates, - Push $push, - SetDefaultBranch $switchDefaultBranch, - BumpAndCommitChangelogVersion $bumpChangelogVersion, + private readonly Variables $variables, + private readonly LoadCurrentGithubEvent $loadGithubEvent, + private readonly Fetch $fetch, + private readonly GetMergeTargetCandidateBranches $getMergeCandidates, + private readonly Push $push, + private readonly SetDefaultBranch $switchDefaultBranch, + private readonly BumpAndCommitChangelogVersion $bumpChangelogVersion, ) { parent::__construct('laminas:automatic-releases:switch-default-branch-to-next-minor'); - - $this->variables = $variables; - $this->loadGithubEvent = $loadGithubEvent; - $this->fetch = $fetch; - $this->getMergeCandidates = $getMergeCandidates; - $this->push = $push; - $this->switchDefaultBranch = $switchDefaultBranch; - $this->bumpChangelogVersion = $bumpChangelogVersion; } public function execute(InputInterface $input, OutputInterface $output): int diff --git a/src/Changelog/BumpAndCommitChangelogVersionViaKeepAChangelog.php b/src/Changelog/BumpAndCommitChangelogVersionViaKeepAChangelog.php index 9ce6bb49..338fbd9b 100644 --- a/src/Changelog/BumpAndCommitChangelogVersionViaKeepAChangelog.php +++ b/src/Changelog/BumpAndCommitChangelogVersionViaKeepAChangelog.php @@ -25,24 +25,13 @@ class BumpAndCommitChangelogVersionViaKeepAChangelog implements BumpAndCommitCha Updates the %s file to add a changelog entry for a new %s version. COMMIT; - private ChangelogExists $changelogExists; - private CheckoutBranch $checkoutBranch; - private CommitFile $commitFile; - private Push $push; - private LoggerInterface $logger; - public function __construct( - ChangelogExists $changelogExists, - CheckoutBranch $checkoutBranch, - CommitFile $commitFile, - Push $push, - LoggerInterface $logger, + private readonly ChangelogExists $changelogExists, + private readonly CheckoutBranch $checkoutBranch, + private readonly CommitFile $commitFile, + private readonly Push $push, + private readonly LoggerInterface $logger, ) { - $this->changelogExists = $changelogExists; - $this->checkoutBranch = $checkoutBranch; - $this->commitFile = $commitFile; - $this->push = $push; - $this->logger = $logger; } public function __invoke( diff --git a/src/Changelog/ChangelogReleaseNotes.php b/src/Changelog/ChangelogReleaseNotes.php index 7c56f0d5..711df2c1 100644 --- a/src/Changelog/ChangelogReleaseNotes.php +++ b/src/Changelog/ChangelogReleaseNotes.php @@ -15,11 +15,6 @@ class ChangelogReleaseNotes { private const CONCATENATION_STRING = "\n\n-----\n\n"; - private ?ChangelogEntry $changelogEntry; - - /** @psalm-var non-empty-string */ - private string $contents; - /** @psalm-param non-empty-string $changelogFile */ public static function writeChangelogFile(string $changelogFile, self $releaseNotes): void { @@ -42,14 +37,13 @@ public static function writeChangelogFile(string $changelogFile, self $releaseNo /** @psalm-param non-empty-string $contents */ public function __construct( - string $contents, - ?ChangelogEntry $changelogEntry = null, + private string $contents, + private ChangelogEntry|null $changelogEntry = null, ) { if ($changelogEntry) { $changelogEntry = clone $changelogEntry; } - $this->contents = $contents; $this->changelogEntry = $changelogEntry; } diff --git a/src/Changelog/CommitReleaseChangelogViaKeepAChangelog.php b/src/Changelog/CommitReleaseChangelogViaKeepAChangelog.php index 241ded6c..a5beb2ae 100644 --- a/src/Changelog/CommitReleaseChangelogViaKeepAChangelog.php +++ b/src/Changelog/CommitReleaseChangelogViaKeepAChangelog.php @@ -24,24 +24,13 @@ final class CommitReleaseChangelogViaKeepAChangelog implements CommitReleaseChan Updates the %s to set the release date. COMMIT; - private ChangelogExists $changelogExists; - private CheckoutBranch $checkoutBranch; - private CommitFile $commitFile; - private Push $push; - private LoggerInterface $logger; - public function __construct( - ChangelogExists $changelogExists, - CheckoutBranch $checkoutBranch, - CommitFile $commitFile, - Push $push, - LoggerInterface $logger, + private readonly ChangelogExists $changelogExists, + private readonly CheckoutBranch $checkoutBranch, + private readonly CommitFile $commitFile, + private readonly Push $push, + private readonly LoggerInterface $logger, ) { - $this->changelogExists = $changelogExists; - $this->checkoutBranch = $checkoutBranch; - $this->commitFile = $commitFile; - $this->push = $push; - $this->logger = $logger; } /** @psalm-param non-empty-string $repositoryDirectory */ diff --git a/src/Environment/EnvironmentVariables.php b/src/Environment/EnvironmentVariables.php index 0b09237d..3906a998 100644 --- a/src/Environment/EnvironmentVariables.php +++ b/src/Environment/EnvironmentVariables.php @@ -33,20 +33,6 @@ class EnvironmentVariables implements Variables 'EMERGENCY', ]; - /** @psalm-var non-empty-string */ - private string $githubToken; - private SecretKeyId $signingSecretKey; - /** @psalm-var non-empty-string */ - private string $gitAuthorName; - /** @psalm-var non-empty-string */ - private string $gitAuthorEmail; - /** @psalm-var non-empty-string */ - private string $githubEventPath; - /** @psalm-var non-empty-string */ - private string $workspacePath; - /** @psalm-var non-empty-string */ - private string $logLevel; - /** * @psalm-param non-empty-string $githubToken * @psalm-param non-empty-string $gitAuthorName @@ -56,29 +42,20 @@ class EnvironmentVariables implements Variables * @psalm-param non-empty-string $logLevel */ private function __construct( - string $githubToken, - SecretKeyId $signingSecretKey, - string $gitAuthorName, - string $gitAuthorEmail, - string $githubEventPath, - string $workspacePath, - string $logLevel, + private readonly string $githubToken, + private readonly SecretKeyId $signingSecretKey, + private readonly string $gitAuthorName, + private readonly string $gitAuthorEmail, + private readonly string $githubEventPath, + private readonly string $workspacePath, + private readonly string $logLevel, ) { - $this->githubToken = $githubToken; - $this->signingSecretKey = $signingSecretKey; - $this->gitAuthorName = $gitAuthorName; - $this->gitAuthorEmail = $gitAuthorEmail; - $this->githubEventPath = $githubEventPath; - $this->workspacePath = $workspacePath; - /** @psalm-suppress ImpureFunctionCall the {@see \Psl\Iter\contains()} API is conditionally pure */ Psl\invariant( Iter\contains(self::LOG_LEVELS, $logLevel), 'LOG_LEVEL env MUST be a valid monolog/monolog log level constant name or value;' . ' see https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md#log-levels', ); - - $this->logLevel = $logLevel; } public static function fromEnvironment(ImportGpgKeyFromString $importKey): self diff --git a/src/Git/FetchAndSetCurrentUserByReplacingCurrentOriginRemote.php b/src/Git/FetchAndSetCurrentUserByReplacingCurrentOriginRemote.php index f34c97ec..23c51514 100644 --- a/src/Git/FetchAndSetCurrentUserByReplacingCurrentOriginRemote.php +++ b/src/Git/FetchAndSetCurrentUserByReplacingCurrentOriginRemote.php @@ -12,11 +12,8 @@ final class FetchAndSetCurrentUserByReplacingCurrentOriginRemote implements Fetch { - private EnvironmentVariables $variables; - - public function __construct(EnvironmentVariables $variables) + public function __construct(private readonly EnvironmentVariables $variables) { - $this->variables = $variables; } public function __invoke( diff --git a/src/Git/Push.php b/src/Git/Push.php index 1d50a0dd..db0c7db9 100644 --- a/src/Git/Push.php +++ b/src/Git/Push.php @@ -14,6 +14,6 @@ interface Push public function __invoke( string $repositoryDirectory, string $symbol, - ?string $alias = null, + string|null $alias = null, ): void; } diff --git a/src/Git/PushViaConsole.php b/src/Git/PushViaConsole.php index 98f2c54a..7627275e 100644 --- a/src/Git/PushViaConsole.php +++ b/src/Git/PushViaConsole.php @@ -12,7 +12,7 @@ final class PushViaConsole implements Push public function __invoke( string $repositoryDirectory, string $symbol, - ?string $alias = null, + string|null $alias = null, ): void { if ($alias === null) { Shell\execute('git', ['push', 'origin', $symbol], $repositoryDirectory); diff --git a/src/Git/Value/MergeTargetCandidateBranches.php b/src/Git/Value/MergeTargetCandidateBranches.php index d731d53f..c8c2f610 100644 --- a/src/Git/Value/MergeTargetCandidateBranches.php +++ b/src/Git/Value/MergeTargetCandidateBranches.php @@ -13,18 +13,11 @@ final class MergeTargetCandidateBranches { /** - * @var BranchName[] branches that can be used for releases, sorted in ascending version number - * @psalm-var list - */ - private array $sortedBranches; - - /** - * @param BranchName[] $sortedBranches + * @param BranchName[] $sortedBranches branches that can be used for releases, sorted in ascending version number * @psalm-param list $sortedBranches */ - private function __construct(array $sortedBranches) + private function __construct(private readonly array $sortedBranches) { - $this->sortedBranches = $sortedBranches; } public static function fromAllBranches(BranchName ...$branches): self @@ -40,7 +33,7 @@ public static function fromAllBranches(BranchName ...$branches): self return new self($mergeTargetBranches); } - public function targetBranchFor(SemVerVersion $version): ?BranchName + public function targetBranchFor(SemVerVersion $version): BranchName|null { foreach ($this->sortedBranches as $branch) { if ($branch->isForNewerVersionThan($version)) { @@ -55,7 +48,7 @@ public function targetBranchFor(SemVerVersion $version): ?BranchName return null; } - public function branchToMergeUp(SemVerVersion $version): ?BranchName + public function branchToMergeUp(SemVerVersion $version): BranchName|null { $targetBranch = $this->targetBranchFor($version); @@ -77,7 +70,7 @@ public function branchToMergeUp(SemVerVersion $version): ?BranchName : $branch; } - public function newestReleaseBranch(): ?BranchName + public function newestReleaseBranch(): BranchName|null { return Iter\first(Vec\reverse($this->sortedBranches)); } diff --git a/src/Git/Value/SemVerVersion.php b/src/Git/Value/SemVerVersion.php index ec758905..d1032e69 100644 --- a/src/Git/Value/SemVerVersion.php +++ b/src/Git/Value/SemVerVersion.php @@ -10,15 +10,11 @@ /** @psalm-immutable */ final class SemVerVersion { - private int $major; - private int $minor; - private int $patch; - - private function __construct(int $major, int $minor, int $patch) - { - $this->major = $major; - $this->minor = $minor; - $this->patch = $patch; + private function __construct( + private readonly int $major, + private readonly int $minor, + private readonly int $patch, + ) { } /** diff --git a/src/Github/Api/GraphQL/Query/GetMilestoneChangelog/Response/Author.php b/src/Github/Api/GraphQL/Query/GetMilestoneChangelog/Response/Author.php index e1e96f9b..6fa0a932 100644 --- a/src/Github/Api/GraphQL/Query/GetMilestoneChangelog/Response/Author.php +++ b/src/Github/Api/GraphQL/Query/GetMilestoneChangelog/Response/Author.php @@ -10,15 +10,11 @@ final class Author { - /** @psalm-var non-empty-string */ - private string $name; - private UriInterface $url; - /** @psalm-param non-empty-string $name */ - private function __construct(string $name, UriInterface $url) - { - $this->name = $name; - $this->url = $url; + private function __construct( + private readonly string $name, + private readonly UriInterface $url, + ) { } /** @param array $payload */ diff --git a/src/Github/Api/GraphQL/Query/GetMilestoneChangelog/Response/IssueOrPullRequest.php b/src/Github/Api/GraphQL/Query/GetMilestoneChangelog/Response/IssueOrPullRequest.php index 7d39371d..f1f05dc1 100644 --- a/src/Github/Api/GraphQL/Query/GetMilestoneChangelog/Response/IssueOrPullRequest.php +++ b/src/Github/Api/GraphQL/Query/GetMilestoneChangelog/Response/IssueOrPullRequest.php @@ -12,36 +12,19 @@ /** @psalm-immutable */ final class IssueOrPullRequest { - private int $number; - /** @psalm-var non-empty-string */ - private string $title; - private Author $author; - /** - * @var array - * @psalm-var list