From aebf1a3d9bd0a1c5d44dea5ce447b21dc98b0da9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Tvrd=C3=ADk?= Date: Tue, 27 Jan 2026 13:08:19 +0100 Subject: [PATCH] fix: reset temporaryFiles array in finally block Use a finally block in removeTemporaryFiles() to ensure the temporaryFiles array is always cleared after removal, even if an exception occurs. This prevents attempting to delete the same files multiple times when the method is called from both the shutdown function and destructor. --- src/Knp/Snappy/AbstractGenerator.php | 8 ++++++-- tests/Knp/Snappy/AbstractGeneratorTest.php | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Knp/Snappy/AbstractGenerator.php b/src/Knp/Snappy/AbstractGenerator.php index 529bc7c..0357a62 100644 --- a/src/Knp/Snappy/AbstractGenerator.php +++ b/src/Knp/Snappy/AbstractGenerator.php @@ -317,8 +317,12 @@ public function getCommand($input, $output, array $options = []) */ public function removeTemporaryFiles() { - foreach ($this->temporaryFiles as $file) { - $this->unlink($file); + try { + foreach ($this->temporaryFiles as $file) { + $this->unlink($file); + } + } finally { + $this->temporaryFiles = []; } } diff --git a/tests/Knp/Snappy/AbstractGeneratorTest.php b/tests/Knp/Snappy/AbstractGeneratorTest.php index fe5e07c..e5728c2 100644 --- a/tests/Knp/Snappy/AbstractGeneratorTest.php +++ b/tests/Knp/Snappy/AbstractGeneratorTest.php @@ -861,6 +861,8 @@ public function testCleanupEmptyTemporaryFiles(): void $remove = new ReflectionMethod($generator, 'removeTemporaryFiles'); $remove->invoke($generator); + + $this->assertCount(0, $files->getValue($generator)); } public function testleanupTemporaryFiles(): void @@ -887,6 +889,8 @@ public function testleanupTemporaryFiles(): void $remove = new ReflectionMethod($generator, 'removeTemporaryFiles'); $remove->invoke($generator); + + $this->assertCount(0, $files->getValue($generator)); } public function testResetOptions(): void