From e4ed0cc8be35be06d68877fd891588cc27b4bdaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Tue, 13 Jul 2021 11:31:29 -0300 Subject: [PATCH] Replace fopen, fwrite and fclose with file_put_contents MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- libraries/classes/Command/CacheWarmupCommand.php | 11 +++-------- libraries/classes/Command/FixPoTwigCommand.php | 16 ++-------------- libraries/classes/Routing.php | 14 ++------------ test/classes/EncodingTest.php | 9 ++------- test/classes/ZipExtensionTest.php | 14 +++----------- 5 files changed, 12 insertions(+), 52 deletions(-) diff --git a/libraries/classes/Command/CacheWarmupCommand.php b/libraries/classes/Command/CacheWarmupCommand.php index 65392444a7f3..198196ae09b3 100644 --- a/libraries/classes/Command/CacheWarmupCommand.php +++ b/libraries/classes/Command/CacheWarmupCommand.php @@ -17,9 +17,7 @@ use Symfony\Component\Console\Output\OutputInterface; use Twig\Cache\CacheInterface; -use function fclose; -use function fopen; -use function fwrite; +use function file_put_contents; use function is_file; use function json_encode; use function sprintf; @@ -181,15 +179,12 @@ private function warmUpTwigCache( } $output->writeln('Writing replacements...', OutputInterface::VERBOSITY_VERY_VERBOSE); + // Store replacements in JSON - $handle = fopen($tmpDir . '/replace.json', 'w'); - if ($handle === false) { + if (file_put_contents($tmpDir . '/replace.json', (string) json_encode($replacements)) === false) { return Command::FAILURE; } - fwrite($handle, (string) json_encode($replacements)); - fclose($handle); - $output->writeln('Replacements written done.', OutputInterface::VERBOSITY_VERBOSE); $output->writeln('Warm up done.', OutputInterface::VERBOSITY_VERBOSE); diff --git a/libraries/classes/Command/FixPoTwigCommand.php b/libraries/classes/Command/FixPoTwigCommand.php index d0cb221bebcb..06138c84324c 100644 --- a/libraries/classes/Command/FixPoTwigCommand.php +++ b/libraries/classes/Command/FixPoTwigCommand.php @@ -8,10 +8,8 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use function fclose; use function file_get_contents; -use function fopen; -use function fwrite; +use function file_put_contents; use function intval; use function is_array; use function json_decode; @@ -75,17 +73,7 @@ static function (array $matches) use ($replacements): string { return Command::FAILURE; } - /* Write fixed file */ - $handle = fopen(self::POT_FILE, 'w'); - if ($handle === false) { - return Command::FAILURE; - } - - if (fwrite($handle, $pot) === false) { - return Command::FAILURE; - } - - if (! fclose($handle)) { + if (file_put_contents(self::POT_FILE, $pot) === false) { return Command::FAILURE; } diff --git a/libraries/classes/Routing.php b/libraries/classes/Routing.php index 9b5800fa3820..7acd2ab5a3e8 100644 --- a/libraries/classes/Routing.php +++ b/libraries/classes/Routing.php @@ -14,10 +14,8 @@ use RuntimeException; use function __; -use function fclose; use function file_exists; -use function fopen; -use function fwrite; +use function file_put_contents; use function htmlspecialchars; use function is_array; use function is_readable; @@ -118,15 +116,7 @@ private static function routesCachedDispatcher(callable $routeDefinitionCallback public static function writeCache(string $cacheContents): bool { - $handle = @fopen(self::ROUTES_CACHE_FILE, 'w'); - if ($handle === false) { - return false; - } - - $couldWrite = fwrite($handle, $cacheContents); - fclose($handle); - - return $couldWrite !== false; + return @file_put_contents(self::ROUTES_CACHE_FILE, $cacheContents) !== false; } public static function getCurrentRoute(): string diff --git a/test/classes/EncodingTest.php b/test/classes/EncodingTest.php index 5d8eaf4a5a59..f305de601848 100644 --- a/test/classes/EncodingTest.php +++ b/test/classes/EncodingTest.php @@ -7,11 +7,9 @@ use PhpMyAdmin\Encoding; use function _setlocale; -use function fclose; use function file_get_contents; -use function fopen; +use function file_put_contents; use function function_exists; -use function fwrite; use function mb_convert_encoding; use function mb_convert_kana; use function setlocale; @@ -186,10 +184,7 @@ public function testFileConv(): void { $file_str = '教育漢字常用漢字'; $filename = 'test.kanji'; - $file = fopen($filename, 'w'); - $this->assertNotFalse($file); - fwrite($file, $file_str); - fclose($file); + $this->assertNotFalse(file_put_contents($filename, $file_str)); $GLOBALS['kanji_encoding_list'] = 'ASCII,EUC-JP,SJIS,JIS'; $result = Encoding::kanjiFileConv($filename, 'JIS', 'kana'); diff --git a/test/classes/ZipExtensionTest.php b/test/classes/ZipExtensionTest.php index c5726b6cc13c..1468c1db62fc 100644 --- a/test/classes/ZipExtensionTest.php +++ b/test/classes/ZipExtensionTest.php @@ -7,9 +7,7 @@ use PhpMyAdmin\ZipExtension; use ZipArchive; -use function fclose; -use function fopen; -use function fwrite; +use function file_put_contents; use function tempnam; use function unlink; @@ -155,10 +153,7 @@ public function testCreateSingleFile(): void $tmp = tempnam('./', 'zip-test'); $this->assertNotFalse($tmp); - $handle = fopen($tmp, 'w'); - $this->assertNotFalse($handle); - fwrite($handle, $file); - fclose($handle); + $this->assertNotFalse(file_put_contents($tmp, $file)); $zip = new ZipArchive(); $this->assertTrue( @@ -207,10 +202,7 @@ public function testCreateMultiFile(): void $tmp = tempnam('./', 'zip-test'); $this->assertNotFalse($tmp); - $handle = fopen($tmp, 'w'); - $this->assertNotFalse($handle); - fwrite($handle, $file); - fclose($handle); + $this->assertNotFalse(file_put_contents($tmp, $file)); $zip = new ZipArchive(); $this->assertTrue(