From f21e9554cbe08c6ad31accbcd8370c06e7678150 Mon Sep 17 00:00:00 2001 From: Matthew Setter Date: Wed, 19 Jun 2024 11:32:16 +1000 Subject: [PATCH] Do not load config from empty cache file This change solves a fatal error that I've been encountering, whenever the cache file is present, but empty: Uncaught TypeError: Cannot assign int to property Laminas\ConfigAggregator\ConfigAggregator::$config of type array So, this change returns false if the config file is present but empty. Signed-off-by: Matthew Setter --- src/ConfigAggregator.php | 4 ++++ test/ConfigAggregatorTest.php | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/src/ConfigAggregator.php b/src/ConfigAggregator.php index 82fc43c..3b226ed 100644 --- a/src/ConfigAggregator.php +++ b/src/ConfigAggregator.php @@ -257,6 +257,10 @@ private function loadConfigFromCache(null|string $cachedConfigFile): bool return false; } + if (filesize($cachedConfigFile) === 0) { + return false; + } + $this->config = require $cachedConfigFile; return true; } diff --git a/test/ConfigAggregatorTest.php b/test/ConfigAggregatorTest.php index 8e8704e..6e644bf 100644 --- a/test/ConfigAggregatorTest.php +++ b/test/ConfigAggregatorTest.php @@ -230,6 +230,14 @@ public function testConfigAggregatorSetsHandlesUnwritableCache(): void self::assertFileDoesNotExist($this->cacheFile); } + public function testConfigAggregatorDoesNotLoadConfigFromCacheIfCacheFileIsEmpty(): void + { + file_put_contents($this->cacheFile, ''); + $aggregator = new ConfigAggregator([], $this->cacheFile); + + self::assertEmpty($aggregator->getMergedConfig()); + } + public function testConfigAggregatorCanLoadConfigFromCache(): void { $expected = [