Skip to content

Commit b69bc47

Browse files
MAGETWO-87492: Removing unbounded while loops from upgrade script
1 parent 79c5692 commit b69bc47

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

dev/tools/UpgradeScripts/pre_composer_update_2.3.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
throw new InvalidArgumentException("Supplied Magento root directory '$rootDir' does not exist");
8484
}
8585

86-
$tempDir = findUnusedFilename("$rootDir/temp_project");
86+
$tempDir = findUnusedFilename($rootDir, "temp_project");
8787

8888
// The composer command uses the Magento root as the working directory so this script can be run from anywhere
8989
$cmd = (!empty($opts['composer']) ? $opts['composer'] : 'composer') . " --working-dir='$rootDir'";
@@ -176,7 +176,7 @@
176176

177177
/**** Execute Updates ****/
178178

179-
$composerBackup = findUnusedFilename("$rootDir/composer.json.bak");
179+
$composerBackup = findUnusedFilename($rootDir, "composer.json.bak");
180180
output("Backing up $rootDir/composer.json to $composerBackup");
181181
copy("$rootDir/composer.json", $composerBackup);
182182

@@ -185,9 +185,13 @@
185185
$repoLabels = array_map('strtolower',array_keys($composer['repositories']));
186186
$newLabel = 'magento';
187187
if (in_array($newLabel, $repoLabels)) {
188-
$i = 1;
189-
while (in_array("$newLabel-$i", $repoLabels)) $i++;
190-
$newLabel = "$newLabel-$i";
188+
$count = count($repoLabels);
189+
for ($i = 1; $i <= $count; $i++) {
190+
if (!in_array("$newLabel-$i", $repoLabels)) {
191+
$newLabel = "$newLabel-$i";
192+
break;
193+
}
194+
}
191195
}
192196
output("Adding $repo to composer repositories under label '$newLabel'");
193197
runComposer("config repositories.$newLabel composer $repo");
@@ -226,7 +230,7 @@
226230

227231
// Update Magento/Updater if it's installed
228232
if (file_exists("$rootDir/update")) {
229-
$updateBackup = findUnusedFilename("$rootDir/update.bak");
233+
$updateBackup = findUnusedFilename($rootDir, "update.bak");
230234
output("Backing up Magento/Updater directory $rootDir/update to $updateBackup");
231235
rename("$rootDir/update", $updateBackup);
232236
output('Updating Magento/Updater');
@@ -272,16 +276,17 @@
272276
/**
273277
* Gets a variant of a filename that doesn't already exist so we don't overwrite anything
274278
*
279+
* @param string $dir
275280
* @param string $filename
276281
* @return string
277282
*/
278-
function findUnusedFilename($filename) {
279-
if (file_exists($filename)) {
280-
$i = 1;
281-
while (file_exists($filename . "_$i")) $i++;
282-
$filename = $filename . "_$i";
283+
function findUnusedFilename($dir, $filename) {
284+
$unique = "$dir/$filename";
285+
if (file_exists($unique)) {
286+
$unique = tempnam($dir, "$filename.");
287+
unlink($unique);
283288
}
284-
return $filename;
289+
return $unique;
285290
}
286291

287292
/**

0 commit comments

Comments
 (0)