Repository: symfony/symfony
File: src/Symfony/Component/Translation/Bridge/Lokalise/LokaliseProvider.php (PHP)

Code before:

        $newfile = \sprintf('%s%s%s.zip', sys_get_temp_dir(), \DIRECTORY_SEPARATOR, uniqid());
        $extractPath = \sprintf('%s%s%s', sys_get_temp_dir(), \DIRECTORY_SEPARATOR, uniqid());
        if (!copy($downloadUrl, $newfile)) {
            throw new LogicException(\sprintf('failed to copy "%s".', $downloadUrl));
        }

        try {
            $zip = new \ZipArchive();
            if (!$zip->open($newfile, \ZipArchive::CREATE)) {
                throw new LogicException(\sprintf('failed to open zip file "%s".', $newfile));
            }

            try {
                if (!$zip->extractTo($extractPath)) {
                    throw new LogicException(\sprintf('failed to extract content from zip file "%s".', $newfile));
                }
            } finally {
                $zip->close();
            }

            $fileContents = $this->getFileContents($extractPath);

            // Lokalise returns languages with "-" separator, we need to reformat them to "_" separator.
            /** @var array<array-key, array-key> $reformattedLanguages */
            $reformattedLanguages = array_map(function ($language) {
                return str_replace('-', '_', $language);
            }, array_keys($fileContents));

            return array_combine($reformattedLanguages, $fileContents);
        } finally {
            unlink($newfile);
            $this->removeDir($extractPath);
        }
    }

    private function getFileContents(string $dir, string $baseDir = ''): array
    {
        $fileContents = [];
        foreach (scandir($dir) as $filename) {
            if (\in_array($filename, ['.', '..'])) {
                continue;
            }
            $path = \sprintf('%s%s%s', $dir, \DIRECTORY_SEPARATOR, $filename);
            if (is_dir($path)) {
                $fileContents = array_merge($fileContents, $this->getFileContents($path, $filename));
                continue;
            }
            $fileContents[$baseDir][$filename]['content'] = file_get_contents($path);
        }


Review comment (suggestion):
```suggestion
            $reformattedLanguages = array_map(static fn ($language) => 
```

Code after:
            throw new ProviderException(\sprintf('Unable to download translations file from Lokalise: "%s".', $response->getContent(false)), $response);
        }
        $zipFile = tempnam(sys_get_temp_dir(), 'lokalise');
        $extractPath = $zipFile.'.dir';
        try {
            if (!$h = @fopen($zipFile, 'w')) {
                throw new RuntimeException(error_get_last()['message'] ?? 'Failed to create temporary file.');
            }
            foreach ($this->client->stream($response) as $chunk) {
                fwrite($h, $chunk->getContent());
            }
            fclose($h);

            $zip = new \ZipArchive();
            if (!$zip->open($zipFile)) {
                throw new RuntimeException('Failed to open zipped translations from Lokalise.');
            }

            try {
                if (!$zip->extractTo($extractPath)) {
                    throw new RuntimeException('Failed to unzip translations from Lokalize.');
                }
            } finally {
                $zip->close();
            }

            return $this->getZipContents($extractPath);
        } finally {
            if (is_resource($h)) {
                fclose($h);
            }
            @unlink($zipFile);
            $this->removeDir($extractPath);
        }
    }

    private function getZipContents(string $dir): array
    {
        $contents = [];
        foreach (scandir($dir) as $lang) {
            if (\in_array($lang, ['.', '..'], true)) {
                continue;
            }
            $path = $dir.'/'.$lang;
            // Lokalise returns languages with "-" separator, we need to reformat them to "_" separator.
            $lang = str_replace('-', '_', $lang);
            foreach (scandir($path) as $name) {
                if (!\in_array($name, ['.', '..'], true)) {
                    $contents[$lang][$name]['content'] = file_get_contents($path.'/'.$name);
                }
            }