Skip to content

Commit 4e20c35

Browse files
Copilotdermatz
andcommitted
#feature-request - Final improvements: extract constants and handle race conditions
Co-authored-by: dermatz <6103201+dermatz@users.noreply.github.com>
1 parent b5e36dd commit 4e20c35

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

src/Console/Command/Static/CleanCommand.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
*/
2020
class CleanCommand extends AbstractCommand
2121
{
22+
/**
23+
* Areas to clean for themes
24+
*/
25+
private const AREAS = ['frontend', 'adminhtml'];
26+
2227
/**
2328
* @param ThemeList $themeList
2429
* @param Filesystem $filesystem
@@ -231,8 +236,7 @@ private function cleanThemeDirectories(string $basePath, string $themeCode): int
231236
$cleaned = 0;
232237

233238
// Scan for areas (frontend, adminhtml, etc.)
234-
$areas = ['frontend', 'adminhtml'];
235-
foreach ($areas as $area) {
239+
foreach (self::AREAS as $area) {
236240
$areaPath = $basePath . DIRECTORY_SEPARATOR . $area;
237241
if (!is_dir($areaPath)) {
238242
continue;
@@ -268,6 +272,11 @@ private function removeDirectory(string $dir): bool
268272
throw new \RuntimeException(sprintf('Directory is not writable: %s', $dir));
269273
}
270274

275+
// Re-check directory exists to handle race conditions
276+
if (!is_dir($dir)) {
277+
return true;
278+
}
279+
271280
$files = scandir($dir);
272281
if ($files === false) {
273282
throw new \RuntimeException(sprintf('Failed to scan directory: %s', $dir));
@@ -276,6 +285,12 @@ private function removeDirectory(string $dir): bool
276285
$files = array_diff($files, ['.', '..']);
277286
foreach ($files as $file) {
278287
$path = $dir . DIRECTORY_SEPARATOR . $file;
288+
289+
// Check if path still exists (handle race conditions)
290+
if (!file_exists($path)) {
291+
continue;
292+
}
293+
279294
if (is_dir($path)) {
280295
if (!$this->removeDirectory($path)) {
281296
throw new \RuntimeException(sprintf('Failed to remove directory: %s', $path));
@@ -284,13 +299,15 @@ private function removeDirectory(string $dir): bool
284299
if (!is_writable($path)) {
285300
throw new \RuntimeException(sprintf('File is not writable: %s', $path));
286301
}
287-
if (!unlink($path)) {
302+
// Re-check file exists before unlinking
303+
if (file_exists($path) && !unlink($path)) {
288304
throw new \RuntimeException(sprintf('Failed to remove file: %s', $path));
289305
}
290306
}
291307
}
292308

293-
if (!rmdir($dir)) {
309+
// Re-check directory exists before removing
310+
if (is_dir($dir) && !rmdir($dir)) {
294311
throw new \RuntimeException(sprintf('Failed to remove directory: %s', $dir));
295312
}
296313

0 commit comments

Comments
 (0)