Skip to content

Commit 66eaa7c

Browse files
author
Your Name
committed
fix: improve null safety in commit methods following PR #64 pattern
1 parent 49cfca8 commit 66eaa7c

1 file changed

Lines changed: 33 additions & 25 deletions

File tree

src/VCS/Adapter/Git/Gitea.php

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -401,25 +401,24 @@ public function getCommit(string $owner, string $repositoryName, string $commitH
401401

402402
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "token $this->accessToken"]);
403403

404-
$statusCode = $response['headers']['status-code'] ?? 0;
405-
if ($statusCode >= 400) {
404+
$responseHeaders = $response['headers'] ?? [];
405+
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
406+
if ($responseHeadersStatusCode >= 400) {
406407
throw new Exception("Commit not found or inaccessible");
407408
}
408409

409-
$body = $response['body'] ?? [];
410-
411-
// Response structure: body['commit']['message'], body['author'], body['committer']
412-
$commit = $body['commit'] ?? [];
413-
$author = $body['author'] ?? [];
414-
$committer = $body['committer'] ?? [];
410+
$responseBody = $response['body'] ?? [];
411+
$responseBodyCommit = $responseBody['commit'] ?? [];
412+
$responseBodyCommitAuthor = $responseBodyCommit['author'] ?? [];
413+
$responseBodyAuthor = $responseBody['author'] ?? [];
415414

416415
return [
417-
'commitAuthor' => $commit['author']['name'] ?? 'Unknown',
418-
'commitMessage' => $commit['message'] ?? 'No message',
419-
'commitAuthorAvatar' => $author['avatar_url'] ?? '',
420-
'commitAuthorUrl' => $author['html_url'] ?? '',
421-
'commitHash' => $body['sha'] ?? '',
422-
'commitUrl' => $body['html_url'] ?? '',
416+
'commitAuthor' => $responseBodyCommitAuthor['name'] ?? 'Unknown',
417+
'commitMessage' => $responseBodyCommit['message'] ?? 'No message',
418+
'commitAuthorAvatar' => $responseBodyAuthor['avatar_url'] ?? '',
419+
'commitAuthorUrl' => $responseBodyAuthor['html_url'] ?? '',
420+
'commitHash' => $responseBody['sha'] ?? '',
421+
'commitUrl' => $responseBody['html_url'] ?? '',
423422
];
424423
}
425424

@@ -441,24 +440,33 @@ public function getLatestCommit(string $owner, string $repositoryName, string $b
441440

442441
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "token $this->accessToken"]);
443442

444-
$statusCode = $response['headers']['status-code'] ?? 0;
445-
if ($statusCode >= 400 || empty($response['body'][0])) {
443+
$responseHeaders = $response['headers'] ?? [];
444+
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
445+
if ($responseHeadersStatusCode >= 400) {
446+
throw new Exception("Latest commit response failed with status code {$responseHeadersStatusCode}");
447+
}
448+
449+
$responseBody = $response['body'] ?? [];
450+
451+
if (empty($responseBody[0] ?? [])) {
446452
throw new Exception("Latest commit response is missing required information.");
447453
}
448454

449-
$body = $response['body'][0];
450-
$commit = $body['commit'] ?? [];
451-
$author = $body['author'] ?? [];
455+
$responseBodyFirst = $responseBody[0];
456+
$responseBodyFirstCommit = $responseBodyFirst['commit'] ?? [];
457+
$responseBodyFirstCommitAuthor = $responseBodyFirstCommit['author'] ?? [];
458+
$responseBodyFirstAuthor = $responseBodyFirst['author'] ?? [];
452459

453460
return [
454-
'commitAuthor' => $commit['author']['name'] ?? 'Unknown',
455-
'commitMessage' => $commit['message'] ?? 'No message',
456-
'commitHash' => $body['sha'] ?? '',
457-
'commitUrl' => $body['html_url'] ?? '',
458-
'commitAuthorAvatar' => $author['avatar_url'] ?? '',
459-
'commitAuthorUrl' => $author['html_url'] ?? '',
461+
'commitAuthor' => $responseBodyFirstCommitAuthor['name'] ?? 'Unknown',
462+
'commitMessage' => $responseBodyFirstCommit['message'] ?? 'No message',
463+
'commitHash' => $responseBodyFirst['sha'] ?? '',
464+
'commitUrl' => $responseBodyFirst['html_url'] ?? '',
465+
'commitAuthorAvatar' => $responseBodyFirstAuthor['avatar_url'] ?? '',
466+
'commitAuthorUrl' => $responseBodyFirstAuthor['html_url'] ?? '',
460467
];
461468
}
469+
462470
public function updateCommitStatus(string $repositoryName, string $commitHash, string $owner, string $state, string $description = '', string $target_url = '', string $context = ''): void
463471
{
464472
throw new Exception("Not implemented yet");

0 commit comments

Comments
 (0)