From 3676553a625c45e5cc53f394128445f4490855d4 Mon Sep 17 00:00:00 2001 From: Soner Sayakci Date: Fri, 20 Mar 2026 04:20:15 +0100 Subject: [PATCH] chore: update deps --- .github/workflows/phpunit.yml | 2 +- .php-cs-fixer.dist.php | 5 +++-- composer.json | 8 ++++---- phpstan.neon | 1 - src/Application.php | 2 +- src/Command/LocalCommand.php | 11 ++++++++--- .../Factory/GithubClientFactory.php | 2 +- .../Factory/GitlabClientFactory.php | 4 ++-- src/Platform/Github/Github.php | 5 +++-- src/Platform/Github/GithubCommenter.php | 11 ++++++++--- src/Platform/Gitlab/GitlabCommenter.php | 6 ++++-- src/Platform/PlatformDetector.php | 14 ++++++++++++-- src/Rule/DisallowRepeatedCommits.php | 2 +- src/Struct/Collection.php | 2 +- src/Struct/Github/PullRequest.php | 15 ++++++++++----- src/Struct/Gitlab/PullRequest.php | 1 + src/Struct/Local/LocalPullRequest.php | 5 +++-- src/Struct/PullRequest.php | 2 +- tests/RunnerTest.php | 4 ++-- tests/Struct/FileCollectionTest.php | 2 +- tests/configs/all.php | 5 +++-- tests/configs/warning.php | 5 +++-- 22 files changed, 73 insertions(+), 41 deletions(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 61ffd839..edff1870 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -16,7 +16,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: ['8.2', '8.3'] + php: ['8.2', '8.3', '8.4', '8.5'] steps: - uses: actions/checkout@v6 diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 727eef0b..d71eba0f 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -9,11 +9,12 @@ ->setRules([ '@PSR12' => true, '@PSR12:risky' => true, - '@PHP74Migration' => true, - '@PHP74Migration:risky' => true, + '@PHP82Migration' => true, + '@PHP80Migration:risky' => true, '@PhpCsFixer' => true, '@Symfony' => true, '@Symfony:risky' => true, + 'declare_strict_types' => ['strategy' => 'enforce'], // Ensure there is no code on the same line as the PHP open tag and it is followed by a blank line. 'blank_line_after_opening_tag' => false, // Using `isset($var) &&` multiple times should be done in one call. diff --git a/composer.json b/composer.json index 14681f10..7d1df60b 100644 --- a/composer.json +++ b/composer.json @@ -52,11 +52,11 @@ "require-dev": { "friendsofphp/php-cs-fixer": "dev-master", "phpunit/phpunit": "^11.5", - "phpstan/phpstan": "^1.12.9", - "phpstan/phpstan-phpunit": "^1.4.0", + "phpstan/phpstan": "^2.1", + "phpstan/phpstan-phpunit": "^2", "phpstan/extension-installer": "^1.4.3", - "phpstan/phpstan-deprecation-rules": "^1.2.1", - "phpstan/phpstan-strict-rules": "^1.6.1", + "phpstan/phpstan-deprecation-rules": "^2", + "phpstan/phpstan-strict-rules": "^2", "infection/infection": "^0.29.14" }, "scripts": { diff --git a/phpstan.neon b/phpstan.neon index 8dadedd8..093d716c 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,6 +1,5 @@ parameters: level: max - inferPrivatePropertyTypeFromConstructor: true reportUnmatchedIgnoredErrors: false paths: diff --git a/src/Application.php b/src/Application.php index fad321a1..d6d27d89 100644 --- a/src/Application.php +++ b/src/Application.php @@ -24,7 +24,7 @@ public function __construct() /** @var Command $command */ $command = $this->container->get($taggedService); - $this->add($command); + $this->addCommand($command); } } diff --git a/src/Command/LocalCommand.php b/src/Command/LocalCommand.php index 7cdadb73..a2237602 100644 --- a/src/Command/LocalCommand.php +++ b/src/Command/LocalCommand.php @@ -1,4 +1,5 @@ -mustRun(); - $headBranch = trim($process->getOutput()); + $headBranch = mb_trim($process->getOutput()); } $process = new Process(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], $root); $process->mustRun(); - $localBranch = trim($process->getOutput()); + $localBranch = mb_trim($process->getOutput()); $config = $this->configLoader->loadByPath($configPath); + if (!\is_string($headBranch)) { + throw new \InvalidArgumentException('Invalid head-branch option given'); + } + $this->localPlatform->load($root, $localBranch . '|' . $headBranch); $context = new Context($this->localPlatform); diff --git a/src/DependencyInjection/Factory/GithubClientFactory.php b/src/DependencyInjection/Factory/GithubClientFactory.php index d9a6241e..fbe7e7fe 100644 --- a/src/DependencyInjection/Factory/GithubClientFactory.php +++ b/src/DependencyInjection/Factory/GithubClientFactory.php @@ -12,7 +12,7 @@ public static function build(): Client { $client = new Client(); - if (isset($_SERVER['GITHUB_TOKEN'])) { + if (isset($_SERVER['GITHUB_TOKEN']) && \is_string($_SERVER['GITHUB_TOKEN'])) { $client->authenticate($_SERVER['GITHUB_TOKEN'], null, AuthMethod::ACCESS_TOKEN); } diff --git a/src/DependencyInjection/Factory/GitlabClientFactory.php b/src/DependencyInjection/Factory/GitlabClientFactory.php index 214a8883..eaaff953 100644 --- a/src/DependencyInjection/Factory/GitlabClientFactory.php +++ b/src/DependencyInjection/Factory/GitlabClientFactory.php @@ -11,11 +11,11 @@ public static function build(): Client { $client = new Client(); - if (isset($_SERVER['CI_SERVER_URL'])) { + if (isset($_SERVER['CI_SERVER_URL']) && \is_string($_SERVER['CI_SERVER_URL'])) { $client->setUrl($_SERVER['CI_SERVER_URL']); } - if (isset($_SERVER['DANGER_GITLAB_TOKEN'])) { + if (isset($_SERVER['DANGER_GITLAB_TOKEN']) && \is_string($_SERVER['DANGER_GITLAB_TOKEN'])) { $client->authenticate($_SERVER['DANGER_GITLAB_TOKEN'], Client::AUTH_HTTP_TOKEN); } diff --git a/src/Platform/Github/Github.php b/src/Platform/Github/Github.php index d9d7ed48..dd29c5ca 100644 --- a/src/Platform/Github/Github.php +++ b/src/Platform/Github/Github.php @@ -117,10 +117,11 @@ private function getReviews(string $owner, string $repository, string $id): arra { $requestedReviewers = array_map(static fn (array $reviewer): string => $reviewer['login'], $this->raw['requested_reviewers']); + /** @var list $reviewersRequest */ $reviewersRequest = $this->client->pullRequest()->reviews()->all($owner, $repository, (int) $id); - $reviewers = array_map(static fn (array $reviewer) => $reviewer['user']['login'], $reviewersRequest); + $reviewers = array_map(static fn (array $reviewer): string => $reviewer['user']['login'], $reviewersRequest); - return array_unique(array_merge($requestedReviewers, $reviewers)); + return array_values(array_unique(array_merge($requestedReviewers, $reviewers))); } public function hasDangerMessage(): bool diff --git a/src/Platform/Github/GithubCommenter.php b/src/Platform/Github/GithubCommenter.php index f9e008a4..f5bdc56e 100644 --- a/src/Platform/Github/GithubCommenter.php +++ b/src/Platform/Github/GithubCommenter.php @@ -27,11 +27,12 @@ public function comment(string $owner, string $repo, string $id, string $body, C private function commentUsingProxy(string $owner, string $repo, string $id, string $body, Config $config): string { $url = sprintf('%s/repos/%s/%s/issues/%s/comments', $config->getGithubCommentProxy(), $owner, $repo, $id); + /** @var array{html_url?: string} $response */ $response = $this->httpClient->request('POST', $url, [ 'json' => ['body' => $body, 'mode' => $config->getUpdateCommentMode()], 'headers' => [ 'User-Agent' => 'Comment-Proxy', - 'temporary-github-token' => $_SERVER['GITHUB_TOKEN'], + 'temporary-github-token' => \is_string($_SERVER['GITHUB_TOKEN'] ?? null) ? $_SERVER['GITHUB_TOKEN'] : '', ], ])->toArray(); @@ -54,6 +55,7 @@ private function commentUsingApiKey(string $owner, string $repo, string $id, str $this->client->issues()->comments()->remove($owner, $repo, $commentId); } + /** @var array{html_url: string} $comment */ $comment = $this->client->issues()->comments()->create($owner, $repo, (int) $id, ['body' => $body]); return $comment['html_url']; @@ -63,6 +65,7 @@ private function commentUsingApiKey(string $owner, string $repo, string $id, str * Could not find any comment. Lets create a new one */ if (\count($ids) === 0) { + /** @var array{html_url: string} $comment */ $comment = $this->client->issues()->comments()->create($owner, $repo, (int) $id, ['body' => $body]); return $comment['html_url']; @@ -75,6 +78,7 @@ private function commentUsingApiKey(string $owner, string $repo, string $id, str */ foreach ($ids as $i => $commentId) { if ($i === 0) { + /** @var array{html_url: string} $comment */ $comment = $this->client->issues()->comments()->update($owner, $repo, $commentId, ['body' => $body]); $url = $comment['html_url']; @@ -95,11 +99,12 @@ public function getCommentIds(string $owner, string $repo, string $id): array $ids = []; $pager = new ResultPager($this->client); + /** @var list $comments */ $comments = $pager->fetchAll($this->client->issues()->comments(), 'all', [$owner, $repo, (int) $id]); foreach ($comments as $comment) { if (str_contains($comment['body'], HTMLRenderer::MARKER)) { - $ids[] = (int) $comment['id']; + $ids[] = $comment['id']; } } @@ -114,7 +119,7 @@ public function remove(string $owner, string $repo, string $id, Config $config): 'json' => ['body' => 'delete', 'mode' => $config->getUpdateCommentMode()], 'headers' => [ 'User-Agent' => 'Comment-Proxy', - 'temporary-github-token' => $_SERVER['GITHUB_TOKEN'], + 'temporary-github-token' => \is_string($_SERVER['GITHUB_TOKEN'] ?? null) ? $_SERVER['GITHUB_TOKEN'] : '', ], ])->toArray(); diff --git a/src/Platform/Gitlab/GitlabCommenter.php b/src/Platform/Gitlab/GitlabCommenter.php index 2e92a591..56996e71 100644 --- a/src/Platform/Gitlab/GitlabCommenter.php +++ b/src/Platform/Gitlab/GitlabCommenter.php @@ -101,6 +101,7 @@ public function removeThread(string $projectIdentifier, int $prId): void public function getRelevantNoteIds(string $projectIdentifier, int $prId): array { $pager = new ResultPager($this->client, 100); + /** @var list $notes */ $notes = $pager->fetchAll($this->client->mergeRequests(), 'showNotes', [$projectIdentifier, $prId]); $ids = []; @@ -111,7 +112,7 @@ public function getRelevantNoteIds(string $projectIdentifier, int $prId): array } if (str_contains($note['body'], HTMLRenderer::MARKER)) { - $ids[] = (int) $note['id']; + $ids[] = $note['id']; } } @@ -124,6 +125,7 @@ public function getRelevantNoteIds(string $projectIdentifier, int $prId): array private function getRelevantThreadIds(string $projectIdentifier, int $prId): array { $pager = new ResultPager($this->client, 100); + /** @var list}> $threads */ $threads = $pager->fetchAll($this->client->mergeRequests(), 'showDiscussions', [$projectIdentifier, $prId]); $ids = []; @@ -132,7 +134,7 @@ private function getRelevantThreadIds(string $projectIdentifier, int $prId): arr if (str_contains($thread['notes'][0]['body'], HTMLRenderer::MARKER)) { $ids[] = [ 'threadId' => $thread['id'], - 'noteId' => (int) $thread['notes'][0]['id'], + 'noteId' => $thread['notes'][0]['id'], 'noteBody' => $thread['notes'][0]['body'], ]; } diff --git a/src/Platform/PlatformDetector.php b/src/Platform/PlatformDetector.php index 0f24e30d..932bb0b6 100644 --- a/src/Platform/PlatformDetector.php +++ b/src/Platform/PlatformDetector.php @@ -28,14 +28,24 @@ public function detect(): AbstractPlatform private function createFromGithubContext(): AbstractPlatform { - $this->github->load($_SERVER['GITHUB_REPOSITORY'], $_SERVER['GITHUB_PULL_REQUEST_ID']); + /** @var string $repository */ + $repository = $_SERVER['GITHUB_REPOSITORY']; + /** @var string $pullRequestId */ + $pullRequestId = $_SERVER['GITHUB_PULL_REQUEST_ID']; + + $this->github->load($repository, $pullRequestId); return $this->github; } private function createFromGitlabContext(): AbstractPlatform { - $this->gitlab->load($_SERVER['CI_PROJECT_ID'], $_SERVER['CI_MERGE_REQUEST_IID']); + /** @var string $projectId */ + $projectId = $_SERVER['CI_PROJECT_ID']; + /** @var string $mergeRequestIid */ + $mergeRequestIid = $_SERVER['CI_MERGE_REQUEST_IID']; + + $this->gitlab->load($projectId, $mergeRequestIid); return $this->gitlab; } diff --git a/src/Rule/DisallowRepeatedCommits.php b/src/Rule/DisallowRepeatedCommits.php index 0fddefb2..20ffdca9 100644 --- a/src/Rule/DisallowRepeatedCommits.php +++ b/src/Rule/DisallowRepeatedCommits.php @@ -19,7 +19,7 @@ public function __invoke(Context $context): void if ($context->platform instanceof Github) { $messages = array_filter( $messages, - fn ($message) => !(preg_match('/^Merge branch .* into .*$/', $message) === 1), + static fn ($message) => !(preg_match('/^Merge branch .* into .*$/', $message) === 1), ); } diff --git a/src/Struct/Collection.php b/src/Struct/Collection.php index ef017f9d..05d61cea 100644 --- a/src/Struct/Collection.php +++ b/src/Struct/Collection.php @@ -94,7 +94,7 @@ public function reduce(\Closure $closure, mixed $initial = null): mixed */ public function fmap(\Closure $closure): array { - return array_filter($this->map($closure)); + return array_filter($this->map($closure), static fn (mixed $value): bool => (bool) $value); } public function sort(\Closure $closure): void diff --git a/src/Struct/Github/PullRequest.php b/src/Struct/Github/PullRequest.php index 6e1a4fe0..0c6346c8 100644 --- a/src/Struct/Github/PullRequest.php +++ b/src/Struct/Github/PullRequest.php @@ -32,7 +32,7 @@ class PullRequest extends \Danger\Struct\PullRequest private ?CommentCollection $comments = null; /** - * @var list + * @var list */ public array $rawFiles = []; @@ -46,11 +46,13 @@ public function getCommits(): CommitCollection return $this->commits; } - $this->rawCommits = $this->client->pullRequest()->commits($this->owner, $this->repo, $this->id); + /** @var list $rawCommits */ + $rawCommits = $this->client->pullRequest()->commits($this->owner, $this->repo, $this->id); + $this->rawCommits = $rawCommits; $collection = new CommitCollection(); - foreach ($this->rawCommits as $rawGithubCommit) { + foreach ($rawCommits as $rawGithubCommit) { $commit = new Commit(); $commit->sha = $rawGithubCommit['sha']; $commit->createdAt = new \DateTime($rawGithubCommit['commit']['committer']['date']); @@ -76,13 +78,15 @@ public function getFiles(): FileCollection return $this->files; } - $this->rawFiles = (new ResultPager($this->client)) + /** @var list $rawFiles */ + $rawFiles = (new ResultPager($this->client)) ->fetchAll($this->client->pullRequest(), 'files', [$this->owner, $this->repo, $this->id]) ; + $this->rawFiles = $rawFiles; $collection = new FileCollection(); - foreach ($this->rawFiles as $rawGithubFile) { + foreach ($rawFiles as $rawGithubFile) { $file = new GithubFile($this->client, $this->owner, $this->repo, $rawGithubFile['filename'], $this->headSha); $file->name = $rawGithubFile['filename']; $file->status = $rawGithubFile['status']; @@ -107,6 +111,7 @@ public function getComments(): CommentCollection } $pager = new ResultPager($this->client); + /** @var list $list */ $list = $pager->fetchAll($this->client->pullRequest()->comments(), 'all', [$this->owner, $this->repo, $this->id]); $this->comments = new CommentCollection(); diff --git a/src/Struct/Gitlab/PullRequest.php b/src/Struct/Gitlab/PullRequest.php index aa8bf041..faa5ab3e 100644 --- a/src/Struct/Gitlab/PullRequest.php +++ b/src/Struct/Gitlab/PullRequest.php @@ -111,6 +111,7 @@ public function getComments(): CommentCollection $this->comments = new CommentCollection(); $pager = new ResultPager($this->client); + /** @var list $list */ $list = $pager->fetchAll($this->client->mergeRequests(), 'showNotes', [$this->projectIdentifier, (int) $this->id]); foreach ($list as $commentArray) { diff --git a/src/Struct/Local/LocalPullRequest.php b/src/Struct/Local/LocalPullRequest.php index f0b54345..e0bbc927 100644 --- a/src/Struct/Local/LocalPullRequest.php +++ b/src/Struct/Local/LocalPullRequest.php @@ -1,4 +1,5 @@ -repo . '/' . $file); $element->name = $file; diff --git a/src/Struct/PullRequest.php b/src/Struct/PullRequest.php index bce90432..2bf9c407 100644 --- a/src/Struct/PullRequest.php +++ b/src/Struct/PullRequest.php @@ -33,7 +33,7 @@ abstract class PullRequest public array $reviewers = []; /** - * @var array + * @var array */ public array $rawCommits = []; diff --git a/tests/RunnerTest.php b/tests/RunnerTest.php index e479e6e7..f625aa14 100644 --- a/tests/RunnerTest.php +++ b/tests/RunnerTest.php @@ -22,12 +22,12 @@ public function testRunner(): void $afterExecuted = false; $config = new Config(); - $config->useRule(function () use (&$ruleExecuted, &$afterExecuted): void { + $config->useRule(static function () use (&$ruleExecuted, &$afterExecuted): void { static::assertFalse($afterExecuted); /** @phpstan-ignore-line */ $ruleExecuted = true; }); - $config->after(function () use (&$ruleExecuted, &$afterExecuted): void { + $config->after(static function () use (&$ruleExecuted, &$afterExecuted): void { static::assertTrue($ruleExecuted); $afterExecuted = true; }); diff --git a/tests/Struct/FileCollectionTest.php b/tests/Struct/FileCollectionTest.php index ccf2eb68..d558af5b 100644 --- a/tests/Struct/FileCollectionTest.php +++ b/tests/Struct/FileCollectionTest.php @@ -141,7 +141,7 @@ public function testMap(): void $f1->name = 'A'; $c = new FileCollection([$f1]); - $list = $c->fmap(fn (FakeFile $file) => $file->name); + $list = $c->fmap(static fn (FakeFile $file) => $file->name); static::assertSame(['A'], $list); } diff --git a/tests/configs/all.php b/tests/configs/all.php index 74db4813..159daa85 100644 --- a/tests/configs/all.php +++ b/tests/configs/all.php @@ -1,10 +1,11 @@ -useRule(function (Danger\Context $context): void { + ->useRule(static function (Danger\Context $context): void { $context->failure('A Failure'); $context->warning('A Warning'); $context->notice('A Notice'); diff --git a/tests/configs/warning.php b/tests/configs/warning.php index dfb76d36..da1f1c81 100644 --- a/tests/configs/warning.php +++ b/tests/configs/warning.php @@ -1,10 +1,11 @@ -useRule(function (Danger\Context $context): void { + ->useRule(static function (Danger\Context $context): void { $context->warning('Test'); }) ;