From 5817a0ead0d21d4be18f0c8a1294086871ac1bfa Mon Sep 17 00:00:00 2001 From: Patrick Beuks Date: Sun, 22 Jun 2025 12:33:42 +0200 Subject: [PATCH] Fix null passed to strlen in parsers When a command is run and fails it returns null. This is then passed to the parser and it handles it as an empty string Since PHP 8.0 it is deprecated to pass null to `strlen` so instead make this an empty String. --- src/Gitonomy/Git/Parser/ParserBase.php | 2 +- src/Gitonomy/Git/ReferenceBag.php | 2 +- tests/Gitonomy/Git/Tests/ReferenceBagTest.php | 9 +++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Gitonomy/Git/Parser/ParserBase.php b/src/Gitonomy/Git/Parser/ParserBase.php index be149bf0..45a80020 100644 --- a/src/Gitonomy/Git/Parser/ParserBase.php +++ b/src/Gitonomy/Git/Parser/ParserBase.php @@ -25,7 +25,7 @@ abstract protected function doParse(); public function parse($content) { $this->cursor = 0; - $this->content = $content; + $this->content = $content ?? ''; $this->length = strlen($this->content); $this->doParse(); diff --git a/src/Gitonomy/Git/ReferenceBag.php b/src/Gitonomy/Git/ReferenceBag.php index adde82c4..71ef9ae4 100644 --- a/src/Gitonomy/Git/ReferenceBag.php +++ b/src/Gitonomy/Git/ReferenceBag.php @@ -354,7 +354,7 @@ protected function initialize() $parser = new Parser\ReferenceParser(); $output = $this->repository->run('show-ref'); } catch (RuntimeException $e) { - $output = null; + return; } $parser->parse($output); diff --git a/tests/Gitonomy/Git/Tests/ReferenceBagTest.php b/tests/Gitonomy/Git/Tests/ReferenceBagTest.php index 35756bf0..f9b9c82c 100644 --- a/tests/Gitonomy/Git/Tests/ReferenceBagTest.php +++ b/tests/Gitonomy/Git/Tests/ReferenceBagTest.php @@ -44,4 +44,13 @@ public function testUnknownReference(Repository $repository) $this->assertArrayNotHasKey('refs/pull/1/head', $refs); $this->assertArrayNotHasKey('refs/notes/gtm-data', $refs); } + + /** + * @dataProvider provideEmpty + */ + public function testEmptyRepo(Repository $repository) + { + $refs = $repository->getReferences()->getAll(); + $this->assertSame([], $refs); + } }