Skip to content

Commit

Permalink
Replace fopen, fwrite and fclose with file_put_contents
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Jul 13, 2021
1 parent be4a577 commit e4ed0cc
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 52 deletions.
11 changes: 3 additions & 8 deletions libraries/classes/Command/CacheWarmupCommand.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
16 changes: 2 additions & 14 deletions libraries/classes/Command/FixPoTwigCommand.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
14 changes: 2 additions & 12 deletions libraries/classes/Routing.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions test/classes/EncodingTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down
14 changes: 3 additions & 11 deletions test/classes/ZipExtensionTest.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit e4ed0cc

Please sign in to comment.