Skip to content

Commit 9b5d151

Browse files
committed
Improve Git::checkGitRevision() return type declaration
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent e0e6ef8 commit 9b5d151

4 files changed

Lines changed: 59 additions & 124 deletions

File tree

‎libraries/classes/Git.php‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,11 @@ private function unPackGz(string $gitFolder, string $hash): array|false|null
360360
*
361361
* @param mixed[] $commit The commit body
362362
*
363-
* @return array<int,array<string,string>|string>
363+
* @return array{
364+
* array{name: string, email: string, date: string},
365+
* array{name: string, email: string, date: string},
366+
* string
367+
* }
364368
*/
365369
private function extractDataFormTextBody(array $commit): array
366370
{
@@ -409,7 +413,7 @@ private function isRemoteCommit(mixed $commit, bool &$isRemoteCommit, string $ha
409413

410414
// check if commit exists in Github
411415
if ($commit !== false && isset($_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash])) {
412-
$isRemoteCommit = $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash];
416+
$isRemoteCommit = (bool) $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash];
413417

414418
return null;
415419
}
@@ -440,7 +444,7 @@ private function isRemoteCommit(mixed $commit, bool &$isRemoteCommit, string $ha
440444
return null;
441445
}
442446

443-
/** @return mixed[] */
447+
/** @return array{string|null, string|false|null} */
444448
private function getHashFromHeadRef(string $gitFolder, string $refHead): array
445449
{
446450
// are we on any branch?
@@ -456,6 +460,7 @@ private function getHashFromHeadRef(string $gitFolder, string $refHead): array
456460
$branch = basename($refHead);
457461
}
458462

463+
$hash = null;
459464
$refFile = $gitFolder . '/' . $refHead;
460465
if (@file_exists($refFile)) {
461466
$hash = @file_get_contents($refFile);
@@ -524,6 +529,16 @@ private function getCommonDirContents(string $gitFolder): string|null
524529

525530
/**
526531
* detects Git revision, if running inside repo
532+
*
533+
* @return array{
534+
* hash: string,
535+
* branch: string|false,
536+
* message: string,
537+
* author: array{name: string, email: string, date: string},
538+
* committer: array{name: string, email: string, date: string},
539+
* is_remote_commit: bool,
540+
* is_remote_branch: bool,
541+
* }|null
527542
*/
528543
public function checkGitRevision(): array|null
529544
{
@@ -549,7 +564,7 @@ public function checkGitRevision(): array|null
549564
}
550565

551566
[$hash, $branch] = $this->getHashFromHeadRef($gitFolder, $refHead);
552-
if ($hash === null) {
567+
if ($hash === null || $branch === null) {
553568
return null;
554569
}
555570

@@ -576,7 +591,7 @@ public function checkGitRevision(): array|null
576591
if ($isRemoteCommit && $branch !== false) {
577592
// check if branch exists in Github
578593
if (isset($_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash])) {
579-
$isRemoteBranch = $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash];
594+
$isRemoteBranch = (bool) $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash];
580595
} else {
581596
$httpRequest = new HttpRequest();
582597
$link = 'https://www.phpmyadmin.net/api/tree/' . $branch . '/';
@@ -597,14 +612,14 @@ public function checkGitRevision(): array|null
597612
[$author, $committer, $message] = $this->extractDataFormTextBody($commit);
598613
} elseif (isset($commitJson->author, $commitJson->committer, $commitJson->message)) {
599614
$author = [
600-
'name' => $commitJson->author->name,
601-
'email' => $commitJson->author->email,
602-
'date' => $commitJson->author->date,
615+
'name' => (string) $commitJson->author->name,
616+
'email' => (string) $commitJson->author->email,
617+
'date' => (string) $commitJson->author->date,
603618
];
604619
$committer = [
605-
'name' => $commitJson->committer->name,
606-
'email' => $commitJson->committer->email,
607-
'date' => $commitJson->committer->date,
620+
'name' => (string) $commitJson->committer->name,
621+
'email' => (string) $commitJson->committer->email,
622+
'date' => (string) $commitJson->committer->date,
608623
];
609624
$message = trim($commitJson->message);
610625
} else {

‎phpstan-baseline.neon‎

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,7 @@ parameters:
14961496
path: libraries/classes/Controllers/GitInfoController.php
14971497

14981498
-
1499-
message: "#^Parameter \\#1 \\$timestamp of static method PhpMyAdmin\\\\Util\\:\\:localisedDate\\(\\) expects int, \\(int\\|false\\) given\\.$#"
1499+
message: "#^Parameter \\#1 \\$timestamp of static method PhpMyAdmin\\\\Util\\:\\:localisedDate\\(\\) expects int, int\\|false given\\.$#"
15001500
count: 2
15011501
path: libraries/classes/Controllers/GitInfoController.php
15021502

@@ -4335,11 +4335,6 @@ parameters:
43354335
count: 1
43364336
path: libraries/classes/Git.php
43374337

4338-
-
4339-
message: "#^Method PhpMyAdmin\\\\Git\\:\\:checkGitRevision\\(\\) return type has no value type specified in iterable type array\\.$#"
4340-
count: 1
4341-
path: libraries/classes/Git.php
4342-
43434338
-
43444339
message: "#^Method PhpMyAdmin\\\\Git\\:\\:isRemoteCommit\\(\\) should return stdClass\\|null but returns mixed\\.$#"
43454340
count: 1
@@ -4350,11 +4345,6 @@ parameters:
43504345
count: 1
43514346
path: libraries/classes/Git.php
43524347

4353-
-
4354-
message: "#^Parameter \\#2 \\$hash of method PhpMyAdmin\\\\Git\\:\\:unPackGz\\(\\) expects string, mixed given\\.$#"
4355-
count: 1
4356-
path: libraries/classes/Git.php
4357-
43584348
-
43594349
message: "#^Parameter \\#2 \\$length of function fread expects int\\<0, max\\>, int given\\.$#"
43604350
count: 1
@@ -4370,16 +4360,6 @@ parameters:
43704360
count: 1
43714361
path: libraries/classes/Git.php
43724362

4373-
-
4374-
message: "#^Parameter \\#2 \\$subject of function preg_match expects string, mixed given\\.$#"
4375-
count: 1
4376-
path: libraries/classes/Git.php
4377-
4378-
-
4379-
message: "#^Parameter \\#3 \\$hash of method PhpMyAdmin\\\\Git\\:\\:isRemoteCommit\\(\\) expects string, mixed given\\.$#"
4380-
count: 1
4381-
path: libraries/classes/Git.php
4382-
43834363
-
43844364
message: "#^Parameter \\#1 \\$params of static method PhpMyAdmin\\\\Url\\:\\:getCommon\\(\\) expects array\\<string, bool\\|int\\|string\\>, array\\<int\\|string, mixed\\> given\\.$#"
43854365
count: 2

‎psalm-baseline.xml‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,21 +1971,8 @@
19711971
</file>
19721972
<file src="libraries/classes/Controllers/GitInfoController.php">
19731973
<MixedArgument>
1974-
<code><![CDATA[$commit['author']['date']]]></code>
1975-
<code><![CDATA[$commit['committer']['date']]]></code>
19761974
<code><![CDATA[$this->config->get('ShowGitRevision') ?? true]]></code>
19771975
</MixedArgument>
1978-
<MixedArgumentTypeCoercion>
1979-
<code>$commit</code>
1980-
</MixedArgumentTypeCoercion>
1981-
<MixedArrayAccess>
1982-
<code><![CDATA[$commit['author']['date']]]></code>
1983-
<code><![CDATA[$commit['committer']['date']]]></code>
1984-
</MixedArrayAccess>
1985-
<MixedArrayAssignment>
1986-
<code><![CDATA[$commit['author']['date']]]></code>
1987-
<code><![CDATA[$commit['committer']['date']]]></code>
1988-
</MixedArrayAssignment>
19891976
<PossiblyUnusedMethod>
19901977
<code>__construct</code>
19911978
</PossiblyUnusedMethod>
@@ -7082,9 +7069,6 @@
70827069
<code>$commit</code>
70837070
<code><![CDATA[$commitJson->message]]></code>
70847071
<code>$dataline</code>
7085-
<code>$hash</code>
7086-
<code>$hash</code>
7087-
<code>$hash</code>
70887072
<code>$offset + ($position * 20)</code>
70897073
<code>$offset + ($position * 4)</code>
70907074
</MixedArgument>
@@ -7096,8 +7080,6 @@
70967080
<code>$dataline</code>
70977081
<code>$end</code>
70987082
<code>$gitLocation</code>
7099-
<code>$isRemoteBranch</code>
7100-
<code>$isRemoteCommit</code>
71017083
<code>$offset</code>
71027084
<code>$position</code>
71037085
<code>$position</code>
@@ -7108,10 +7090,7 @@
71087090
<code>stdClass|null</code>
71097091
</MixedInferredReturnType>
71107092
<MixedOperand>
7111-
<code>$branch</code>
71127093
<code>$fanout[256]</code>
7113-
<code>$hash</code>
7114-
<code>$hash</code>
71157094
<code>$offset</code>
71167095
<code>$position</code>
71177096
<code>$position</code>
@@ -7131,9 +7110,6 @@
71317110
<code>$packOffsets[1]</code>
71327111
<code>json_decode((string) $isFound)</code>
71337112
</MixedReturnStatement>
7134-
<PossiblyInvalidArrayOffset>
7135-
<code><![CDATA[$_SESSION['PMA_VERSION_COMMITDATA_' . $hash]]]></code>
7136-
</PossiblyInvalidArrayOffset>
71377113
<PossiblyNullArgument>
71387114
<code>$gitFolder</code>
71397115
</PossiblyNullArgument>

‎test/classes/GitTest.php‎

Lines changed: 32 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -187,52 +187,34 @@ public function testCheckGitRevisionPacksFolder(): void
187187
'# pack-refs with: peeled fully-peeled sorted' . "\n" .
188188
'c1f2ff2eb0c3fda741f859913fd589379f4e4a8f refs/tags/4.3.10' . "\n" .
189189
'^6f2e60343b0a324c65f2d1411bf4bd03e114fb98' . "\n" .
190-
'17bf8b7309919f8ac593d7c563b31472780ee83b refs/remotes/origin/master' . "\n",
190+
'8d660283c5c88a04bac7a2b3aa9ad9eaff0fd05e refs/remotes/origin/master' . "\n",
191191
);
192192
mkdir($this->testDir . '.git/objects/pack', 0777, true);//default = 0777, recursive mode
193193

194194
$commit = $this->object->checkGitRevision();
195195

196196
if (
197197
$commit === null
198-
&& ! isset($_SESSION['PMA_VERSION_REMOTECOMMIT_17bf8b7309919f8ac593d7c563b31472780ee83b'])
198+
&& ! isset($_SESSION['PMA_VERSION_REMOTECOMMIT_8d660283c5c88a04bac7a2b3aa9ad9eaff0fd05e'])
199199
) {
200200
$this->markTestSkipped('Unable to get remote commit information.');
201201
}
202202

203203
$this->assertIsArray($commit);
204-
$this->assertArrayHasKey('hash', $commit);
205-
$this->assertEquals('17bf8b7309919f8ac593d7c563b31472780ee83b', $commit['hash']);
206-
207-
$this->assertArrayHasKey('branch', $commit);
208-
$this->assertEquals('master', $commit['branch']);
209-
210-
$this->assertArrayHasKey('message', $commit);
211-
$this->assertIsString($commit['message']);
212-
213-
$this->assertArrayHasKey('is_remote_commit', $commit);
214-
$this->assertIsBool($commit['is_remote_commit']);
215-
216-
$this->assertArrayHasKey('is_remote_branch', $commit);
217-
$this->assertIsBool($commit['is_remote_branch']);
218-
219-
$this->assertArrayHasKey('author', $commit);
220-
$this->assertIsArray($commit['author']);
221-
$this->assertArrayHasKey('name', $commit['author']);
222-
$this->assertArrayHasKey('email', $commit['author']);
223-
$this->assertArrayHasKey('date', $commit['author']);
224-
$this->assertIsString($commit['author']['name']);
225-
$this->assertIsString($commit['author']['email']);
226-
$this->assertIsString($commit['author']['date']);
227-
228-
$this->assertArrayHasKey('committer', $commit);
229-
$this->assertIsArray($commit['committer']);
230-
$this->assertArrayHasKey('name', $commit['committer']);
231-
$this->assertArrayHasKey('email', $commit['committer']);
232-
$this->assertArrayHasKey('date', $commit['committer']);
233-
$this->assertIsString($commit['committer']['name']);
234-
$this->assertIsString($commit['committer']['email']);
235-
$this->assertIsString($commit['committer']['date']);
204+
$this->assertSame('8d660283c5c88a04bac7a2b3aa9ad9eaff0fd05e', $commit['hash']);
205+
$this->assertSame('master', $commit['branch']);
206+
$this->assertSame(
207+
'Update po files' . "\n\n" . '[ci skip]' . "\n\n" . 'Signed-off-by: phpMyAdmin bot <bot@phpmyadmin.net>',
208+
$commit['message'],
209+
);
210+
$this->assertTrue($commit['is_remote_commit']);
211+
$this->assertTrue($commit['is_remote_branch']);
212+
$this->assertSame('phpMyAdmin bot', $commit['author']['name']);
213+
$this->assertSame('bot@phpmyadmin.net', $commit['author']['email']);
214+
$this->assertSame('2023-05-14T00:19:49Z', $commit['author']['date']);
215+
$this->assertSame('phpMyAdmin bot', $commit['committer']['name']);
216+
$this->assertSame('bot@phpmyadmin.net', $commit['committer']['email']);
217+
$this->assertSame('2023-05-14T00:19:49Z', $commit['committer']['date']);
236218

237219
rmdir($this->testDir . '.git/objects/pack');
238220
rmdir($this->testDir . '.git/objects');
@@ -306,7 +288,7 @@ public function testCheckGitRevisionPacksFile(): void
306288
'# pack-refs with: peeled fully-peeled sorted' . "\n" .
307289
'c1f2ff2eb0c3fda741f859913fd589379f4e4a8f refs/tags/4.3.10' . "\n" .
308290
'^6f2e60343b0a324c65f2d1411bf4bd03e114fb98' . "\n" .
309-
'17bf8b7309919f8ac593d7c563b31472780ee83b refs/remotes/origin/master' . "\n",
291+
'8d660283c5c88a04bac7a2b3aa9ad9eaff0fd05e refs/remotes/origin/master' . "\n",
310292
);
311293
mkdir($this->testDir . '.git/objects/info', 0777, true);
312294
file_put_contents(
@@ -322,44 +304,26 @@ public function testCheckGitRevisionPacksFile(): void
322304

323305
if (
324306
$commit === null
325-
&& ! isset($_SESSION['PMA_VERSION_REMOTECOMMIT_17bf8b7309919f8ac593d7c563b31472780ee83b'])
307+
&& ! isset($_SESSION['PMA_VERSION_REMOTECOMMIT_8d660283c5c88a04bac7a2b3aa9ad9eaff0fd05e'])
326308
) {
327309
$this->markTestSkipped('Unable to get remote commit information.');
328310
}
329311

330312
$this->assertIsArray($commit);
331-
$this->assertArrayHasKey('hash', $commit);
332-
$this->assertEquals('17bf8b7309919f8ac593d7c563b31472780ee83b', $commit['hash']);
333-
334-
$this->assertArrayHasKey('branch', $commit);
335-
$this->assertEquals('master', $commit['branch']);
336-
337-
$this->assertArrayHasKey('message', $commit);
338-
$this->assertIsString($commit['message']);
339-
340-
$this->assertArrayHasKey('is_remote_commit', $commit);
341-
$this->assertIsBool($commit['is_remote_commit']);
342-
343-
$this->assertArrayHasKey('is_remote_branch', $commit);
344-
$this->assertIsBool($commit['is_remote_branch']);
345-
346-
$this->assertArrayHasKey('author', $commit);
347-
$this->assertIsArray($commit['author']);
348-
$this->assertArrayHasKey('name', $commit['author']);
349-
$this->assertArrayHasKey('email', $commit['author']);
350-
$this->assertArrayHasKey('date', $commit['author']);
351-
$this->assertIsString($commit['author']['name']);
352-
$this->assertIsString($commit['author']['email']);
353-
$this->assertIsString($commit['author']['date']);
354-
355-
$this->assertArrayHasKey('committer', $commit);
356-
$this->assertIsArray($commit['committer']);
357-
$this->assertArrayHasKey('name', $commit['committer']);
358-
$this->assertArrayHasKey('email', $commit['committer']);
359-
$this->assertArrayHasKey('date', $commit['committer']);
360-
$this->assertIsString($commit['committer']['name']);
361-
$this->assertIsString($commit['committer']['email']);
362-
$this->assertIsString($commit['committer']['date']);
313+
$this->assertSame('8d660283c5c88a04bac7a2b3aa9ad9eaff0fd05e', $commit['hash']);
314+
$this->assertSame('master', $commit['branch']);
315+
$this->assertSame(
316+
'Update po files' . "\n\n" . '[ci skip]' . "\n\n" . 'Signed-off-by: phpMyAdmin bot <bot@phpmyadmin.net>',
317+
$commit['message'],
318+
);
319+
$this->assertTrue($commit['is_remote_commit']);
320+
$this->assertTrue($commit['is_remote_branch']);
321+
$this->assertSame('phpMyAdmin bot', $commit['author']['name']);
322+
$this->assertSame('bot@phpmyadmin.net', $commit['author']['email']);
323+
$this->assertSame('2023-05-14T00:19:49Z', $commit['author']['date']);
324+
$this->assertSame('phpMyAdmin bot', $commit['committer']['name']);
325+
$this->assertSame('bot@phpmyadmin.net', $commit['committer']['email']);
326+
$this->assertSame('2023-05-14T00:19:49Z', $commit['committer']['date']);
363327

364328
unlink($this->testDir . '.git/objects/info/packs');
365329
rmdir($this->testDir . '.git/objects/info');

0 commit comments

Comments
 (0)