Skip to content

Commit

Permalink
fix(Text2Image): Fix psalm issues
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
  • Loading branch information
marcelklehr committed Oct 20, 2023
1 parent 7b7f552 commit bc85acf
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions lib/private/TextToImage/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,11 @@ public function runTask(Task $task): void {
}
$this->logger->debug('Creating result files for Text2Image task');
$resources = [];
$files = [];
for ($i = 0; $i < $task->getNumberOfImages(); $i++) {
$resources[] = $folder->newFile((string) $i)->write();
$file = $folder->newFile((string) $i);
$files[] = $file;
$resources[] = $file->write();
if ($resource[count($resources) - 1] === false) {

Check failure on line 155 in lib/private/TextToImage/Manager.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedVariable

lib/private/TextToImage/Manager.php:155:10: UndefinedVariable: Cannot find referenced variable $resource (see https://psalm.dev/024)

Check failure

Code scanning / Psalm

UndefinedVariable Error

Cannot find referenced variable $resource
throw new RuntimeException('Text2Image generation using provider ' . $provider->getName() . ' failed: Couldn\'t open file to write.');
}
Expand All @@ -158,25 +161,28 @@ public function runTask(Task $task): void {
for ($i = 0; $i < $task->getNumberOfImages(); $i++) {
if (is_resource($resources[$i])) {
// If $resource hasn't been closed yet, we'll do that here
fclose($resource[$i]);
fclose($resources[$i]);
}
}
$task->setStatus(Task::STATUS_SUCCESSFUL);
$this->logger->debug('Updating Text2Image task in DB');
$this->taskMapper->update(DbTask::fromPublicTask($task));
return;
} catch (\RuntimeException|\Throwable $e) {
if (isset($resource) && is_resource($resource)) {
// If $resource hasn't been closed yet, we'll do that here
fclose($resource);
}
try {
if (isset($file)) {
$file->delete();
for ($i = 0; $i < $task->getNumberOfImages(); $i++) {
if (isset($resources[$i]) && is_resource($resources[$i])) {
// If $resource hasn't been closed yet, we'll do that here
fclose($resources[$i]);
}
if (isset($files, $files[$i])) {
try {
$files[$i]->delete();
} catch(NotPermittedException $e) {
$this->logger->warning('Failed to clean up Text2Image result file after error', ['exception' => $e]);
}
}
} catch(NotPermittedException $e) {
$this->logger->warning('Failed to clean up Text2Image result file after error', ['exception' => $e]);
}

$this->logger->info('Text2Image generation using provider ' . $provider->getName() . ' failed', ['exception' => $e]);
$task->setStatus(Task::STATUS_FAILED);
try {
Expand Down

0 comments on commit bc85acf

Please sign in to comment.