From d7e64348088afeb2cd589189d647adfdc72c9868 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Fri, 17 Jan 2025 09:34:24 +0900 Subject: [PATCH] New DeprecatedHelper namespace For temporary wrapper functions for deprecated calls that need this PHP 8.4 fputcsv/fgetcsv/str_getcsv encoding default change deprecated warning Note this does not cover the SqlFileInfo class as this is not used in our code --- www/admin/class_test.deprecated.helper.php | 62 ++++++++++++ www/admin/class_test.php | 1 + www/admin/class_test.phpv.php | 2 - .../DeprecatedHelper/Deprecated84.php | 95 +++++++++++++++++++ 4 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 www/admin/class_test.deprecated.helper.php create mode 100644 www/lib/CoreLibs/DeprecatedHelper/Deprecated84.php diff --git a/www/admin/class_test.deprecated.helper.php b/www/admin/class_test.deprecated.helper.php new file mode 100644 index 00000000..2c858be9 --- /dev/null +++ b/www/admin/class_test.deprecated.helper.php @@ -0,0 +1,62 @@ + BASE . LOG, + 'log_file_id' => $LOG_FILE_ID, + 'log_per_date' => true, +]); +$_phpv = new CoreLibs\Check\PhpVersion(); +$phpv_class = 'CoreLibs\Check\PhpVersion'; + +$PAGE_NAME = 'TEST CLASS: PHP VERSION'; +print ""; +print "" . $PAGE_NAME . ""; +print ""; +print '
Class Test Master
'; +print '

' . $PAGE_NAME . '

'; + +// fputcsv +print "

\CoreLibs\DeprecatedHelper\Deprecated84::fputcsv()

"; +$test_csv = BASE . TMP . 'DeprecatedHelper.test.csv'; +print "File: $test_csv
"; + +$fp = fopen($test_csv, "w"); +if (!is_resource($fp)) { + die("Cannot open file: $test_csv"); +} +\CoreLibs\DeprecatedHelper\Deprecated84::fputcsv($fp, ["A", "B", "C"]); +fclose($fp); + +$fp = fopen($test_csv, "r"); +if (!is_resource($fp)) { + die("Cannot open file: $test_csv"); +} +while ($entry = \CoreLibs\DeprecatedHelper\Deprecated84::fgetcsv($fp)) { + print "fgetcsv:
" . print_r($entry, true) . "
"; +} +fclose($fp); + +$out = \CoreLibs\DeprecatedHelper\Deprecated84::str_getcsv("A,B,C"); +print "str_getcsv:
" . print_r($out, true) . "
"; + +print ""; + +// __END__ diff --git a/www/admin/class_test.php b/www/admin/class_test.php index a54b2bce..8772e07b 100644 --- a/www/admin/class_test.php +++ b/www/admin/class_test.php @@ -141,6 +141,7 @@ $test_files = [ 'class_test.error_msg.php' => 'Class Test: ERROR MSG', 'class_test.url-requests.curl.php' => 'Class Test: URL REQUESTS: CURL', 'subfolder/class_test.config.direct.php' => 'Class Test: CONFIG DIRECT SUB', + 'class_test.deprecated.helper.php' => 'Class Test: DEPRECATED HELPERS', ]; asort($test_files); diff --git a/www/admin/class_test.phpv.php b/www/admin/class_test.phpv.php index 9e0d077d..648dca85 100644 --- a/www/admin/class_test.phpv.php +++ b/www/admin/class_test.phpv.php @@ -28,8 +28,6 @@ $log = new CoreLibs\Logging\Logging([ $_phpv = new CoreLibs\Check\PhpVersion(); $phpv_class = 'CoreLibs\Check\PhpVersion'; -// define a list of from to color sets for conversion test - $PAGE_NAME = 'TEST CLASS: PHP VERSION'; print ""; print "" . $PAGE_NAME . ""; diff --git a/www/lib/CoreLibs/DeprecatedHelper/Deprecated84.php b/www/lib/CoreLibs/DeprecatedHelper/Deprecated84.php new file mode 100644 index 00000000..ce01faca --- /dev/null +++ b/www/lib/CoreLibs/DeprecatedHelper/Deprecated84.php @@ -0,0 +1,95 @@ + $fields + * @param string $separator + * @param string $enclosure + * @param string $escape + * @param string $eol + * @return int|false + * @throws InvalidArgumentException + */ + public static function fputcsv( + mixed $stream, + array $fields, + string $separator = ",", + string $enclosure = '"', + string $escape = '', // set to empty for future compatible + string $eol = PHP_EOL + ): int | false { + if (!is_resource($stream)) { + throw new \InvalidArgumentException("fputcsv stream parameter must be a resrouce"); + } + return fputcsv($stream, $fields, $separator, $enclosure, $escape, $eol); + } + + /** + * This is a wrapper for fgetcsv to fix deprecated warning for $escape parameter + * See: https://www.php.net/manual/en/function.fgetcsv.php + * escape parameter deprecation and recommend to set to "" for compatible with PHP 9.0 + * + * @param mixed $stream + * @param null|int $length + * @param string $separator + * @param string $enclosure + * @param string $escape + * @return array|false + * @throws InvalidArgumentException + */ + public static function fgetcsv( + mixed $stream, + ?int $length = null, + string $separator = ',', + string $enclosure = '"', + string $escape = '' // set to empty for future compatible + ): array | false { + if (!is_resource($stream)) { + throw new \InvalidArgumentException("fgetcsv stream parameter must be a resrouce"); + } + return fgetcsv($stream, $length, $separator, $enclosure, $escape); + } + + /** + * This is a wrapper for str_getcsv to fix deprecated warning for $escape parameter + * See: https://www.php.net/manual/en/function.str-getcsv.php + * escape parameter deprecation and recommend to set to "" for compatible with PHP 9.0 + * + * @param string $string + * @param string $separator + * @param string $enclosure + * @param string $escape + * @return array + */ + // phpcs:disable PSR1.Methods.CamelCapsMethodName + public static function str_getcsv( + string $string, + string $separator = ",", + string $enclosure = '"', + string $escape = '' // set to empty for future compatible + ): array { + return str_getcsv($string, $separator, $enclosure, $escape); + } + // phpcs:enable PSR1.Methods.CamelCapsMethodName +} + +// __END__