Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Knp/Snappy/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Comment on lines +320 to +325

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It think it would be preferable to notify the file wasn't delete. This way we don't try to delete a file that cannot, but the server logs has the informations that the file has not been deleted during this process.

Moreover, I wonder why the $this->temporaryFiles array is not clean up after removing all files.

Here is a suggestion that should both resolve the conflict (the function was updated for security concerns) and add a log message with the @trigger_error call.

What do you think about?

Suggested change
try {
foreach ($this->temporaryFiles as $file) {
$this->unlink($file);
}
} finally {
$this->temporaryFiles = [];
foreach ($this->temporaryFiles as $file) {
$filePath = \realpath($file);
$temporaryFolderPath = \realpath($this->getTemporaryFolder());
if (
!$filePath
|| !$temporaryFolderPath
|| !\str_starts_with($filePath, $temporaryFolderPath)
) {
continue;
}
try {
$this->unlink($file);
} catch (\Throwable $t) {
@trigger_error(
"Cannot delete $file. {$file->getMessage()}",
E_USER_WARNING
);
}
}
$this->temporaryFiles = [];

}
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Knp/Snappy/AbstractGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,8 @@ public function testCleanupEmptyTemporaryFiles(): void

$remove = new ReflectionMethod($generator, 'removeTemporaryFiles');
$remove->invoke($generator);

$this->assertCount(0, $files->getValue($generator));

Copilot AI Jan 27, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a test case that verifies the temporaryFiles array is cleared even when unlink throws an exception. This would fully validate the purpose of the finally block introduced in this PR. The test could mock unlink to throw an exception and then verify that temporaryFiles is still emptied despite the exception.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This idea seams great. Maybe by updating the files permissions with a 444 file permissions before calling the removeTemporaryFiles method.

}

public function testleanupTemporaryFiles(): void
Expand All @@ -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
Expand Down
Loading