From cefe7c966fa99485d86b50d2ebef1b3ce983581f Mon Sep 17 00:00:00 2001 From: Jared Whiklo Date: Fri, 12 Apr 2024 08:45:36 -0500 Subject: [PATCH] Return file path in archive type exception --- composer.json | 2 +- src/Bag.php | 11 +++++------ tests/BagTest.php | 4 ++-- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index b9d1a26..64e3f28 100644 --- a/composer.json +++ b/composer.json @@ -50,7 +50,7 @@ "./vendor/bin/phpcpd --suffix='.php' src" ], "phpunit": [ - "phpdbg -qrr ./vendor/bin/phpunit -d memory_limit=-1 --testsuite BagIt" + "phpdbg -qrr ./vendor/bin/phpunit -d memory_limit=-1 --verbose --testsuite BagIt" ], "test": [ "@check", diff --git a/src/Bag.php b/src/Bag.php index 2c7cd4a..cb7e54b 100644 --- a/src/Bag.php +++ b/src/Bag.php @@ -418,7 +418,7 @@ public function package(string $filepath): void { if (!self::hasExtension($filepath, $this->packageExtensions)) { throw new BagItException( - "Unknown archive type, the file extension must be one of (" . + "Unknown archive type ($filepath), the file extension must be one of (" . implode(", ", $this->packageExtensions) . ")" ); } @@ -2186,18 +2186,17 @@ private static function getExtension(string $filepath): ?string { $filename = strtolower(basename($filepath)); $pathinfo = pathinfo($filename); - $extensions = [$pathinfo['extension']] ?? null; + $extensions = []; + $extensions[] = $pathinfo['extension'] ?? null; while (strpos($pathinfo['filename'], ".") > -1) { $pathinfo = pathinfo($pathinfo['filename']); $extensions[] = $pathinfo['extension'] ?? null; } $extensions = array_filter($extensions); if (count($extensions) > 0) { - $extension = implode(".", array_reverse($extensions)); - } else { - $extension = null; + return implode(".", array_reverse($extensions)); } - return $extension; + return null; } /** diff --git a/tests/BagTest.php b/tests/BagTest.php index 6cbfe8e..75e1fa2 100644 --- a/tests/BagTest.php +++ b/tests/BagTest.php @@ -838,8 +838,8 @@ public function testInvalidPackage(): void $this->assertFileDoesNotExist($archivefile); $this->expectException(BagItException::class); - $this->expectExceptionMessage("Unknown archive type, the file extension must be one of (tar, tgz, tar.gz, " . - "tar.bz2, zip)"); + $this->expectExceptionMessage("Unknown archive type ($archivefile), the file extension must be one of (tar, " . + "tgz, tar.gz, tar.bz2, zip)"); $bag->package($archivefile); }