diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index aa68ef4..defda12 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -23,16 +23,10 @@ jobs: strategy: fail-fast: false matrix: - php-version: ["7.2", "7.3", "7.4", "8.0", "8.1", "8.2"] + php-version: ["8.0", "8.1", "8.2"] experimental: [false] os: [ubuntu-latest] coverage-extension: [pcov] - include: - #- { php-version: '5.3', experimental: false, os: ubuntu-latest, coverage-extension: 'xdebug' } - #- { php-version: '5.4', experimental: false, os: ubuntu-latest, coverage-extension: 'xdebug' } - - { php-version: '5.5', experimental: false, os: ubuntu-latest, coverage-extension: 'xdebug' } - - { php-version: '5.6', experimental: false, os: ubuntu-latest, coverage-extension: 'xdebug' } - - { php-version: '7.1', experimental: false, os: ubuntu-latest, coverage-extension: 'xdebug' } steps: - uses: actions/checkout@v4 - name: Use php ${{ matrix.php-version }} diff --git a/.gitignore b/.gitignore index 291bb86..4dd7d5e 100644 --- a/.gitignore +++ b/.gitignore @@ -7,10 +7,14 @@ **/.vagrant **/auth.json **/nbproject +**/temp.php +**/test.php .phpdoc .phpunit.cache .phpunit.result.cache composer.lock +ecs.php phpunit.xml +rector.php target vendor diff --git a/README.md b/README.md index 1cd5595..aa318b6 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ All artifacts are generated in the target directory. Examples are located in the `example` directory. -Start a development server (requires PHP 5.4) using the command: +Start a development server (requires PHP 8.0+) using the command: ``` make server @@ -78,7 +78,7 @@ Create a composer.json in your projects root-directory: ```json { "require": { - "tecnickcom/tc-lib-file": "^1.6" + "tecnickcom/tc-lib-file": "^2.0" } } ``` @@ -86,7 +86,7 @@ Create a composer.json in your projects root-directory: Or add to an existing project with: ```bash -composer require tecnickcom/tc-lib-file ^1.6 +composer require tecnickcom/tc-lib-file ^2.0 ``` ## Packaging diff --git a/VERSION b/VERSION index c0b0d22..157e54f 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.7.39 +2.0.6 diff --git a/composer.json b/composer.json index 99eb6ce..cc1a135 100644 --- a/composer.json +++ b/composer.json @@ -22,15 +22,15 @@ } ], "require": { - "php": ">=5.4", + "php": ">=8.0", "ext-curl": "*", "ext-pcre": "*" }, "require-dev": { "pdepend/pdepend": "2.13.0", "phpmd/phpmd": "2.13.0", - "phpunit/phpunit": "10.1.2 || 9.6.7 || 8.5.31 || 7.5.20 || 6.5.14 || 5.7.27 || 4.8.36", - "squizlabs/php_codesniffer": "3.7.2 || 2.9.2" + "phpunit/phpunit": "10.1.2 || 9.6.13", + "squizlabs/php_codesniffer": "3.7.2" }, "autoload": { "psr-4": { diff --git a/phpstan.neon b/phpstan.neon index c42b364..bf592ce 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,5 @@ parameters: - level: 5 + level: max paths: - src - test diff --git a/resources/debian/control b/resources/debian/control index ccfee43..0db9edd 100644 --- a/resources/debian/control +++ b/resources/debian/control @@ -10,6 +10,6 @@ Vcs-Git: https://github.com/~#VENDOR#~/~#PROJECT#~.git Package: ~#PKGNAME#~ Provides: php-~#PROJECT#~ Architecture: all -Depends: php (>= 5.4.0), php-curl, ${misc:Depends} +Depends: php (>= 8.0.0), php-curl, ${misc:Depends} Description: PHP Color library This library includes PHP classes to read byte-level data from files diff --git a/resources/rpm/rpm.spec b/resources/rpm/rpm.spec index 82c6098..ec90d3d 100644 --- a/resources/rpm/rpm.spec +++ b/resources/rpm/rpm.spec @@ -16,7 +16,7 @@ URL: https://github.com/%{gh_owner}/%{gh_project} BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-%(%{__id_u} -n) BuildArch: noarch -Requires: php(language) >= 5.4.0 +Requires: php(language) >= 8.0.0 Requires: php-curl Requires: php-pcre diff --git a/src/Byte.php b/src/Byte.php index 36b1740..6cd52e6 100644 --- a/src/Byte.php +++ b/src/Byte.php @@ -3,13 +3,13 @@ /** * Byte.php * - * @since 2015-07-28 - * @category Library - * @package File - * @author Nicola Asuni - * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-file + * @since 2015-07-28 + * @category Library + * @package File + * @author Nicola Asuni + * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-file * * This file is part of tc-lib-file software library. */ @@ -21,31 +21,27 @@ * * Function to read byte-level data * - * @since 2015-07-28 - * @category Library - * @package File - * @author Nicola Asuni - * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-file + * @since 2015-07-28 + * @category Library + * @package File + * @author Nicola Asuni + * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-file */ class Byte { - /** - * String to process - * - * @var string - */ - protected $str = ''; - /** * Initialize a new string to be processed * * @param string $str String from where to extract values */ - public function __construct($str) - { - $this->str = $str; + public function __construct( + /** + * String to process + */ + protected string $str + ) { } /** @@ -54,12 +50,11 @@ public function __construct($str) * @param int $offset Point from where to read the data. * * @return int 8 bit value - * */ - public function getByte($offset) + public function getByte(int $offset): int { $val = unpack('Ci', substr($this->str, $offset, 1)); - return $val['i']; + return $val === false ? 0 : $val['i']; } /** @@ -69,10 +64,10 @@ public function getByte($offset) * * @return int 32 bit value */ - public function getULong($offset) + public function getULong(int $offset): int { $val = unpack('Ni', substr($this->str, $offset, 4)); - return $val['i']; + return $val === false ? 0 : $val['i']; } /** @@ -82,10 +77,10 @@ public function getULong($offset) * * @return int 16 bit value */ - public function getUShort($offset) + public function getUShort(int $offset): int { $val = unpack('ni', substr($this->str, $offset, 2)); - return $val['i']; + return $val === false ? 0 : $val['i']; } /** @@ -95,10 +90,10 @@ public function getUShort($offset) * * @return int 16 bit value */ - public function getShort($offset) + public function getShort(int $offset): int { $val = unpack('si', substr($this->str, $offset, 2)); - return $val['i']; + return $val === false ? 0 : $val['i']; } /** @@ -108,7 +103,7 @@ public function getShort($offset) * * @return int 16 bit value */ - public function getUFWord($offset) + public function getUFWord(int $offset): int { return $this->getUShort($offset); } @@ -120,12 +115,13 @@ public function getUFWord($offset) * * @return int 16 bit value */ - public function getFWord($offset) + public function getFWord(int $offset): int { $val = $this->getUShort($offset); if ($val > 0x7fff) { $val -= 0x10000; } + return $val; } @@ -133,13 +129,10 @@ public function getFWord($offset) * Get FIXED from string (32-bit signed fixed-point number (16.16). * * @param int $offset Point from where to read the data. - * - * @return float - * */ - public function getFixed($offset) + public function getFixed(int $offset): float { // mantissa.fraction - return (float)($this->getFWord($offset) . '.' . $this->getUShort($offset + 2)); + return (float) ($this->getFWord($offset) . '.' . $this->getUShort($offset + 2)); } } diff --git a/src/Cache.php b/src/Cache.php index c282e1e..8189e42 100644 --- a/src/Cache.php +++ b/src/Cache.php @@ -3,13 +3,13 @@ /** * Cache.php * - * @since 2011-05-23 - * @category Library - * @package File - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-filecache + * @since 2011-05-23 + * @category Library + * @package File + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-filecache * * This file is part of tc-lib-pdf-filecache software library. */ @@ -19,13 +19,13 @@ /** * Com\Tecnick\Pdf\File\Cache * - * @since 2011-05-23 - * @category Library - * @package File - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-filecache + * @since 2011-05-23 + * @category Library + * @package File + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-filecache */ class Cache { @@ -38,10 +38,8 @@ class Cache /** * File prefix - * - * @var string */ - protected static $prefix; + protected static string $prefix; /** * Set the file prefix (common name) @@ -53,17 +51,16 @@ public function __construct($prefix = null) $this->defineSystemCachePath(); $this->setCachePath(); if ($prefix === null) { - $prefix = rtrim(base64_encode(pack('H*', md5(uniqid((string)rand(), true)))), '='); + $prefix = rtrim(base64_encode(pack('H*', md5(uniqid((string) random_int(0, mt_getrandmax()), true)))), '='); } + self::$prefix = '_' . preg_replace('/[^a-zA-Z0-9_\-]/', '', strtr($prefix, '+/', '-_')) . '_'; } /** * Get the cache directory path - * - * @return string */ - public function getCachePath() + public function getCachePath(): string { return self::$path; } @@ -71,24 +68,23 @@ public function getCachePath() /** * Set the default cache directory path * - * @param string|null $path Cache directory path; if null use the K_PATH_CACHE value + * @param ?string $path Cache directory path; if null use the K_PATH_CACHE value */ - public function setCachePath($path = null) + public function setCachePath(?string $path = null): void { - if (($path === null) || !is_writable($path)) { + if (($path === null) || ! is_writable($path)) { /* @phpstan-ignore-next-line */ self::$path = K_PATH_CACHE; return; } + self::$path = $this->normalizePath($path); } /** * Get the file prefix - * - * @return string */ - public function getFilePrefix() + public function getFilePrefix(): string { return self::$prefix; } @@ -99,9 +95,9 @@ public function getFilePrefix() * @param string $type Type of file * @param string $key File key (used to retrieve file from cache) * - * @return string filename + * @return string|false filename */ - public function getNewFileName($type = 'tmp', $key = '0') + public function getNewFileName(string $type = 'tmp', string $key = '0'): string|bool { return tempnam(self::$path, self::$prefix . $type . '_' . $key . '_'); } @@ -109,11 +105,10 @@ public function getNewFileName($type = 'tmp', $key = '0') /** * Delete cached files * - * @param string $type Type of files to delete - * @param string $key Specific file key to delete - * + * @param ?string $type Type of files to delete + * @param ?string $key Specific file key to delete */ - public function delete($type = null, $key = null) + public function delete(?string $type = null, ?string $key = null): void { $path = self::$path . self::$prefix; if ($type !== null) { @@ -122,22 +117,30 @@ public function delete($type = null, $key = null) $path .= $key . '_'; } } + $path .= '*'; $files = glob($path); - if (!empty($files)) { - array_map('unlink', $files); + if ($files === []) { + return; } + + if ($files === false) { + return; + } + + array_map('unlink', $files); } /** * Set the K_PATH_CACHE constant (if not set) to the default system directory for temporary files */ - protected function defineSystemCachePath() + protected function defineSystemCachePath(): void { if (defined('K_PATH_CACHE')) { return; } - $kPathCache = ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir(); + + $kPathCache = ini_get('upload_tmp_dir') ?: sys_get_temp_dir(); define('K_PATH_CACHE', $this->normalizePath($kPathCache)); } @@ -145,15 +148,18 @@ protected function defineSystemCachePath() * Normalize cache path * * @param string $path Path to normalize - * - * @return string */ - protected function normalizePath($path) + protected function normalizePath(string $path): string { - $path = realpath($path); - if (substr($path, -1) != '/') { - $path .= '/'; + $rpath = realpath($path); + if ($rpath === false) { + return ''; + } + + if (! str_ends_with($rpath, '/')) { + $rpath .= '/'; } - return $path; + + return $rpath; } } diff --git a/src/Dir.php b/src/Dir.php index 7313fd4..11657e6 100644 --- a/src/Dir.php +++ b/src/Dir.php @@ -3,13 +3,13 @@ /** * Dir.php * - * @since 2015-07-28 - * @category Library - * @package File - * @author Nicola Asuni - * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-file + * @since 2015-07-28 + * @category Library + * @package File + * @author Nicola Asuni + * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-file * * This file is part of tc-lib-file software library. */ @@ -21,13 +21,13 @@ * * Function to read byte-level data * - * @since 2015-07-28 - * @category Library - * @package File - * @author Nicola Asuni - * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-file + * @since 2015-07-28 + * @category Library + * @package File + * @author Nicola Asuni + * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-file */ class Dir { @@ -39,21 +39,25 @@ class Dir * * @return string Directory name */ - public function findParentDir($name, $dir = __DIR__) + public function findParentDir(string $name, string $dir = __DIR__): string { - while (!empty($dir)) { - if ($dir == dirname($dir)) { + while ($dir !== '') { + if ($dir === dirname($dir)) { $dir = ''; } + if (@is_writable($dir . DIRECTORY_SEPARATOR . $name)) { $dir = $dir . DIRECTORY_SEPARATOR . $name; break; } + $dir = dirname($dir); } + if (substr($dir, -1) !== DIRECTORY_SEPARATOR) { $dir .= DIRECTORY_SEPARATOR; } + return $dir; } } diff --git a/src/Exception.php b/src/Exception.php index 50bab83..ddf214c 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -3,13 +3,13 @@ /** * Exception.php * - * @since 2015-07-28 - * @category Library - * @package File - * @author Nicola Asuni - * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-file + * @since 2015-07-28 + * @category Library + * @package File + * @author Nicola Asuni + * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-file * * This file is part of tc-lib-file software library. */ @@ -21,13 +21,13 @@ * * Custom Exception class * - * @since 2015-07-28 - * @category Library - * @package File - * @author Nicola Asuni - * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-file + * @since 2015-07-28 + * @category Library + * @package File + * @author Nicola Asuni + * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-file */ class Exception extends \Exception { diff --git a/src/File.php b/src/File.php index be1454a..2439cb3 100644 --- a/src/File.php +++ b/src/File.php @@ -3,13 +3,13 @@ /** * File.php * - * @since 2015-07-28 - * @category Library - * @package File - * @author Nicola Asuni - * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-file + * @since 2015-07-28 + * @category Library + * @package File + * @author Nicola Asuni + * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-file * * This file is part of tc-lib-file software library. */ @@ -23,13 +23,13 @@ * * Function to read byte-level data * - * @since 2015-07-28 - * @category Library - * @package File - * @author Nicola Asuni - * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-file + * @since 2015-07-28 + * @category Library + * @package File + * @author Nicola Asuni + * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-file * * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) */ @@ -45,31 +45,38 @@ class File * * @throws FileException in case of error */ - public function fopenLocal($filename, $mode) + public function fopenLocal(string $filename, string $mode): mixed { - if (strpos($filename, '://') === false) { + if (! str_contains($filename, '://')) { $filename = 'file://' . $filename; - } elseif (strpos($filename, 'file://') !== 0) { + } elseif (! str_starts_with($filename, 'file://')) { throw new FileException('this is not a local file'); } + $handler = @fopen($filename, $mode); if ($handler === false) { throw new FileException('unable to open the file: ' . $filename); } + return $handler; } /** * Read a 4-byte (32 bit) integer from file. * - * @param resource $handle A file system pointer resource that is typically created using fopen(). + * @param resource $resource A file system pointer resource that is typically created using fopen(). * * @return int 4-byte integer */ - public function fReadInt($handle) + public function fReadInt(mixed $resource): int { - $val = unpack('Ni', fread($handle, 4)); - return $val['i']; + $data = fread($resource, 4); + if ($data === false) { + throw new FileException('unable to read the file'); + } + + $val = unpack('Ni', $data); + return $val === false ? 0 : $val['i']; } /** @@ -78,29 +85,30 @@ public function fReadInt($handle) * Reading stops as soon as one of the following conditions is met: * length bytes have been read; EOF (end of file) is reached. * - * @param resource $handle A file system pointer resource that is typically created using fopen(). - * @param int $length Number of bytes to read. - * - * @return string + * @param ?resource $resource A file system pointer resource that is typically created using fopen(). + * @param int<0, max> $length Number of bytes to read. * * @throws FileException in case of error */ - public function rfRead($handle, $length) + public function rfRead(mixed $resource, int $length): string { $data = false; - if (is_resource($handle)) { - $data = @fread($handle, $length); + if (is_resource($resource)) { + $data = @fread($resource, $length); } - if ($data === false) { + + if (($data === false) || ($resource === null)) { throw new FileException('unable to read the file'); } + $rest = ($length - strlen($data)); - if (($rest > 0) && !feof($handle)) { - $stream_meta_data = stream_get_meta_data($handle); + if (($rest > 0) && ! feof($resource)) { + $stream_meta_data = stream_get_meta_data($resource); if ($stream_meta_data['unread_bytes'] > 0) { - $data .= $this->rfRead($handle, $rest); + $data .= $this->rfRead($resource, $rest); } } + return $data; } @@ -109,10 +117,8 @@ public function rfRead($handle, $length) * The file can be also an URL. * * @param string $file Name of the file or URL to read. - * - * @return string File content */ - public function fileGetContents($file) + public function fileGetContents(string $file): string { $alt = $this->getAltFilePaths($file); foreach ($alt as $path) { @@ -121,6 +127,7 @@ public function fileGetContents($file) return $ret; } } + throw new FileException('unable to read the file: ' . $file); } @@ -130,14 +137,15 @@ public function fileGetContents($file) * * @param string $file Name of the file or URL to read. * - * @return string File content or FALSE in case the file is unreadable + * @return string|false File content or FALSE in case the file is unreadable */ - public function getFileData($file) + public function getFileData(string $file): string|false { $ret = @file_get_contents($file); if ($ret !== false) { return $ret; } + // try to use CURL for URLs return $this->getUrlData($file); } @@ -147,38 +155,46 @@ public function getFileData($file) * * @param string $url URL to read. * - * @return string|bool + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ - public function getUrlData($url) + public function getUrlData(string $url): string|false { if ( - (ini_get('allow_url_fopen') && !defined('FORCE_CURL')) - || !function_exists('curl_init') - || !preg_match('%^(https?|ftp)://%', $url) + (ini_get('allow_url_fopen') && ! defined('FORCE_CURL')) + || (! function_exists('curl_init')) + || preg_match('%^(https?|ftp)://%', $url) === 0 + || preg_match('%^(https?|ftp)://%', $url) === false ) { return false; } + // try to get remote file data using cURL - $crs = curl_init(); - curl_setopt($crs, CURLOPT_URL, $url); - curl_setopt($crs, CURLOPT_BINARYTRANSFER, true); - curl_setopt($crs, CURLOPT_FAILONERROR, true); - curl_setopt($crs, CURLOPT_RETURNTRANSFER, true); - if ((ini_get('open_basedir') == '') && (!ini_get('safe_mode'))) { - curl_setopt($crs, CURLOPT_FOLLOWLOCATION, true); + $curlHandle = curl_init(); + curl_setopt($curlHandle, CURLOPT_URL, $url); + curl_setopt($curlHandle, CURLOPT_BINARYTRANSFER, true); + curl_setopt($curlHandle, CURLOPT_FAILONERROR, true); + curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); + if ((ini_get('open_basedir') == '') && (ini_get('safe_mode') === '' || ini_get('safe_mode') === false)) { + curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, true); } - curl_setopt($crs, CURLOPT_CONNECTTIMEOUT, 5); - curl_setopt($crs, CURLOPT_TIMEOUT, 30); - curl_setopt($crs, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($crs, CURLOPT_SSL_VERIFYHOST, false); - curl_setopt($crs, CURLOPT_USERAGENT, 'tc-lib-file'); - curl_setopt($crs, CURLOPT_MAXREDIRS, 5); + + curl_setopt($curlHandle, CURLOPT_CONNECTTIMEOUT, 5); + curl_setopt($curlHandle, CURLOPT_TIMEOUT, 30); + curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($curlHandle, CURLOPT_SSL_VERIFYHOST, false); + curl_setopt($curlHandle, CURLOPT_USERAGENT, 'tc-lib-file'); + curl_setopt($curlHandle, CURLOPT_MAXREDIRS, 5); if (defined('CURLOPT_PROTOCOLS')) { - curl_setopt($crs, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS | CURLPROTO_HTTP | CURLPROTO_FTP | CURLPROTO_FTPS); + curl_setopt( + $curlHandle, + CURLOPT_PROTOCOLS, + CURLPROTO_HTTPS | CURLPROTO_HTTP | CURLPROTO_FTP | CURLPROTO_FTPS + ); } - $ret = curl_exec($crs); - curl_close($crs); - return $ret; + + $ret = curl_exec($curlHandle); + curl_close($curlHandle); + return $ret === true ? '' : $ret; } /** @@ -186,11 +202,11 @@ public function getUrlData($url) * * @param string $file Name of the file or URL to read. * - * @return array + * @return array List of possible alternative file paths or URLs. */ - public function getAltFilePaths($file) + public function getAltFilePaths(string $file): array { - $alt = array($file); + $alt = [$file]; $alt[] = $this->getAltLocalUrlPath($file); $url = $this->getAltMissingUrlProtocol($file); $alt[] = $url; @@ -203,23 +219,22 @@ public function getAltFilePaths($file) * Replace URL relative path with full real server path * * @param string $file Relative URL path - * - * @return string */ - protected function getAltLocalUrlPath($file) + protected function getAltLocalUrlPath(string $file): string { if ( (strlen($file) > 1) && ($file[0] === '/') && ($file[1] !== '/') - && !empty($_SERVER['DOCUMENT_ROOT']) + && ! empty($_SERVER['DOCUMENT_ROOT']) && ($_SERVER['DOCUMENT_ROOT'] !== '/') ) { - $findroot = strpos($file, $_SERVER['DOCUMENT_ROOT']); + $findroot = strpos($file, (string) $_SERVER['DOCUMENT_ROOT']); if (($findroot === false) || ($findroot > 1)) { $file = htmlspecialchars_decode(urldecode($_SERVER['DOCUMENT_ROOT'] . $file)); } } + return $file; } @@ -230,25 +245,25 @@ protected function getAltLocalUrlPath($file) * * @return string local path or original $file */ - protected function getAltMissingUrlProtocol($file) + protected function getAltMissingUrlProtocol(string $file): string { - if (preg_match('%^//%', $file) && !empty($_SERVER['HTTP_HOST'])) { + if (preg_match('%^//%', $file) && ! empty($_SERVER['HTTP_HOST'])) { $file = $this->getDefaultUrlProtocol() . ':' . str_replace(' ', '%20', $file); } + return htmlspecialchars_decode($file); } /** * Get the default URL protocol (http or https) - * - * @return string */ - protected function getDefaultUrlProtocol() + protected function getDefaultUrlProtocol(): string { $protocol = 'http'; - if (!empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off')) { + if (! empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off')) { $protocol .= 's'; } + return $protocol; } @@ -259,25 +274,29 @@ protected function getDefaultUrlProtocol() * * @return string local path or original $file */ - protected function getAltPathFromUrl($url) + protected function getAltPathFromUrl(string $url): string { if ( - !preg_match('%^(https?)://%', $url) + preg_match('%^(https?)://%', $url) === 0 + || preg_match('%^(https?)://%', $url) === false || empty($_SERVER['HTTP_HOST']) || empty($_SERVER['DOCUMENT_ROOT']) ) { return $url; } + $urldata = parse_url($url); - if (!empty($urldata['query'])) { + if (isset($urldata['query']) && $urldata['query'] !== '') { return $url; } + $host = $this->getDefaultUrlProtocol() . '://' . $_SERVER['HTTP_HOST']; - if (strpos($url, $host) === 0) { + if (str_starts_with($url, $host)) { // convert URL to full server path $tmp = str_replace($host, $_SERVER['DOCUMENT_ROOT'], $url); return htmlspecialchars_decode(urldecode($tmp)); } + return $url; } @@ -286,18 +305,25 @@ protected function getAltPathFromUrl($url) * * @param string $file File name and path * - * @return string + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ - protected function getAltUrlFromPath($file) + protected function getAltUrlFromPath(string $file): string { if ( isset($_SERVER['SCRIPT_URI']) - && !preg_match('%^(https?|ftp)://%', $file) - && !preg_match('%^//%', $file) + && (preg_match('%^(https?|ftp)://%', $file) === 0 + || preg_match('%^(https?|ftp)://%', $file) === false) + && (preg_match('%^//%', $file) === 0 + || preg_match('%^//%', $file) === false) ) { $urldata = @parse_url($_SERVER['SCRIPT_URI']); + if (! is_array($urldata) || ! isset($urldata['scheme']) || ! isset($urldata['host'])) { + return $file; + } + return $urldata['scheme'] . '://' . $urldata['host'] . (($file[0] == '/') ? '' : '/') . $file; } + return $file; } } diff --git a/test/ByteTest.php b/test/ByteTest.php index c50b41f..912184a 100644 --- a/test/ByteTest.php +++ b/test/ByteTest.php @@ -3,35 +3,33 @@ /** * ByteTest.php * - * @since 2015-07-28 - * @category Library - * @package File - * @author Nicola Asuni - * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-file + * @since 2015-07-28 + * @category Library + * @package File + * @author Nicola Asuni + * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-file * * This file is part of tc-lib-file software library. */ namespace Test; -use PHPUnit\Framework\TestCase; - /** * Byte Color class test * - * @since 2015-07-28 - * @category Library - * @package File - * @author Nicola Asuni - * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-file + * @since 2015-07-28 + * @category Library + * @package File + * @author Nicola Asuni + * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-file */ class ByteTest extends TestUtil { - protected function getTestObject() + protected function getTestObject(): \Com\Tecnick\File\Byte { $str = chr(0) . chr(0) . chr(0) . chr(0) . chr(1) . chr(3) . chr(7) . chr(15) @@ -45,241 +43,259 @@ protected function getTestObject() /** * @dataProvider getByteDataProvider */ - public function testGetByte($offset, $expected) + public function testGetByte(int $offset, int $expected): void { - $testObj = $this->getTestObject(); - $res = $testObj->getByte($offset); + $byte = $this->getTestObject(); + $res = $byte->getByte($offset); $this->assertEquals($expected, $res); } - public static function getByteDataProvider() + /** + * @return array + */ + public static function getByteDataProvider(): array { - return array( - array(0, 0), - array(1, 0), - array(2, 0), - array(3, 0), - array(4, 1), - array(5, 3), - array(6, 7), - array(7, 15), - array(8, 31), - array(9, 63), - array(10, 127), - array(11, 255), - array(12, 254), - array(13, 252), - array(14, 248), - array(15, 240), - array(16, 224), - array(17, 192), - array(18, 128), - array(19, 0), - array(20, 255), - array(21, 255), - array(22, 255), - array(23, 255) - ); + return [ + [0, 0], + [1, 0], + [2, 0], + [3, 0], + [4, 1], + [5, 3], + [6, 7], + [7, 15], + [8, 31], + [9, 63], + [10, 127], + [11, 255], + [12, 254], + [13, 252], + [14, 248], + [15, 240], + [16, 224], + [17, 192], + [18, 128], + [19, 0], + [20, 255], + [21, 255], + [22, 255], + [23, 255], + ]; } /** * @dataProvider getULongDataProvider */ - public function testGetULong($offset, $expected) + public function testGetULong(int $offset, int $expected): void { - $testObj = $this->getTestObject(); - $res = $testObj->getULong($offset); + $byte = $this->getTestObject(); + $res = $byte->getULong($offset); $this->assertEquals($expected, $res); } - public static function getULongDataProvider() + /** + * @return array + */ + public static function getULongDataProvider(): array { - return array( - array(0, 0), - array(1, 1), - array(2, 259), - array(3, 66311), - array(4, 16975631), - array(5, 50794271), - array(6, 118431551), - array(7, 253706111), - array(8, 524255231), - array(9, 1065353214), - array(10, 2147483388), - array(11, 4294900984), - array(12, 4277991664), - array(13, 4244173024), - array(14, 4176535744), - array(15, 4041261184), - array(16, 3770712064), - array(17, 3229614335), - array(18, 2147549183), - array(19, 16777215), - array(20, 4294967295) - ); + return [ + [0, 0], + [1, 1], + [2, 259], + [3, 66311], + [4, 16_975_631], + [5, 50_794_271], + [6, 118_431_551], + [7, 253_706_111], + [8, 524_255_231], + [9, 1_065_353_214], + [10, 2_147_483_388], + [11, 4_294_900_984], + [12, 4_277_991_664], + [13, 4_244_173_024], + [14, 4_176_535_744], + [15, 4_041_261_184], + [16, 3_770_712_064], + [17, 3_229_614_335], + [18, 2_147_549_183], + [19, 16_777_215], + [20, 4_294_967_295], + ]; } /** * @dataProvider getUShortDataProvider */ - public function testGetUShort($offset, $expected) + public function testGetUShort(int $offset, int $expected): void { - $testObj = $this->getTestObject(); - $res = $testObj->getUShort($offset); + $byte = $this->getTestObject(); + $res = $byte->getUShort($offset); $this->assertEquals($expected, $res); } /** * @dataProvider getUShortDataProvider */ - public function testGetUFWord($offset, $expected) + public function testGetUFWord(int $offset, int $expected): void { - $testObj = $this->getTestObject(); - $res = $testObj->getUFWord($offset); + $byte = $this->getTestObject(); + $res = $byte->getUFWord($offset); $this->assertEquals($expected, $res); } - public static function getUShortDataProvider() + /** + * @return array + */ + public static function getUShortDataProvider(): array { - return array( - array(0, 0), - array(1, 0), - array(2, 0), - array(3, 1), - array(4, 259), - array(5, 775), - array(6, 1807), - array(7, 3871), - array(8, 7999), - array(9, 16255), - array(10, 32767), - array(11, 65534), - array(12, 65276), - array(13, 64760), - array(14, 63728), - array(15, 61664), - array(16, 57536), - array(17, 49280), - array(18, 32768), - array(19, 255), - array(20, 65535), - array(21, 65535), - array(22, 65535) - ); + return [ + [0, 0], + [1, 0], + [2, 0], + [3, 1], + [4, 259], + [5, 775], + [6, 1807], + [7, 3871], + [8, 7999], + [9, 16255], + [10, 32767], + [11, 65534], + [12, 65276], + [13, 64760], + [14, 63728], + [15, 61664], + [16, 57536], + [17, 49280], + [18, 32768], + [19, 255], + [20, 65535], + [21, 65535], + [22, 65535], + ]; } /** * @dataProvider getShortDataProvider */ - public function testGetShort($offset, $expected) + public function testGetShort(int $offset, int $expected): void { - $testObj = $this->getTestObject(); - $res = $testObj->getShort($offset); + $byte = $this->getTestObject(); + $res = $byte->getShort($offset); $this->assertEquals($expected, $res); } - public static function getShortDataProvider() + /** + * @return array + */ + public static function getShortDataProvider(): array { - return array( - array(0, 0), - array(1, 0), - array(2, 0), - array(3, 256), - array(4, 769), - array(5, 1795), - array(6, 3847), - array(7, 7951), - array(8, 16159), - array(9, 32575), - array(10, -129), - array(11, -257), - array(12, -770), - array(13, -1796), - array(14, -3848), - array(15, -7952), - array(16, -16160), - array(17, -32576), - array(18, 128), - array(19, -256), - array(20, -1), - array(21, -1), - array(22, -1) - ); + return [ + [0, 0], + [1, 0], + [2, 0], + [3, 256], + [4, 769], + [5, 1795], + [6, 3847], + [7, 7951], + [8, 16159], + [9, 32575], + [10, -129], + [11, -257], + [12, -770], + [13, -1796], + [14, -3848], + [15, -7952], + [16, -16160], + [17, -32576], + [18, 128], + [19, -256], + [20, -1], + [21, -1], + [22, -1], + ]; } /** * @dataProvider getFWordDataProvider */ - public function testGetFWord($offset, $expected) + public function testGetFWord(int $offset, int $expected): void { - $testObj = $this->getTestObject(); - $res = $testObj->getFWord($offset); + $byte = $this->getTestObject(); + $res = $byte->getFWord($offset); $this->assertEquals($expected, $res); } - public static function getFWordDataProvider() + /** + * @return array + */ + public static function getFWordDataProvider(): array { - return array( - array(0, 0), - array(1, 0), - array(2, 0), - array(3, 1), - array(4, 259), - array(5, 775), - array(6, 1807), - array(7, 3871), - array(8, 7999), - array(9, 16255), - array(10, 32767), - array(11, -2), - array(12, -260), - array(13, -776), - array(14, -1808), - array(15, -3872), - array(16, -8000), - array(17, -16256), - array(18, -32768), - array(19, 255), - array(20, -1), - array(21, -1), - array(22, -1) - ); + return [ + [0, 0], + [1, 0], + [2, 0], + [3, 1], + [4, 259], + [5, 775], + [6, 1807], + [7, 3871], + [8, 7999], + [9, 16255], + [10, 32767], + [11, -2], + [12, -260], + [13, -776], + [14, -1808], + [15, -3872], + [16, -8000], + [17, -16256], + [18, -32768], + [19, 255], + [20, -1], + [21, -1], + [22, -1], + ]; } /** * @dataProvider getFixedDataProvider */ - public function testGetFixed($offset, $expected) + public function testGetFixed(int $offset, int|float $expected): void { - $testObj = $this->getTestObject(); - $res = $testObj->getFixed($offset); + $byte = $this->getTestObject(); + $res = $byte->getFixed($offset); $this->assertEquals($expected, $res); } - public static function getFixedDataProvider() + /** + * @return array + */ + public static function getFixedDataProvider(): array { - return array( - array(0, 0), - array(1, 0.1), - array(2, 0.259), - array(3, 1.775), - array(4, 259.1807), - array(5, 775.3871), - array(6, 1807.7999), - array(7, 3871.16255), - array(8, 7999.32767), - array(9, 16255.65534), - array(10, 32767.65276), - array(11, -2.64760), - array(12, -260.63728), - array(13, -776.61664), - array(14, -1808.57536), - array(15, -3872.49280), - array(16, -8000.32768), - array(17, -16256.255), - array(18, -32768.65535), - array(19, 255.65535), - array(20, -1.65535) - ); + return [ + [0, 0], + [1, 0.1], + [2, 0.259], + [3, 1.775], + [4, 259.1807], + [5, 775.3871], + [6, 1807.7999], + [7, 3871.16255], + [8, 7999.32767], + [9, 16255.65534], + [10, 32767.65276], + [11, -2.64760], + [12, -260.63728], + [13, -776.61664], + [14, -1808.57536], + [15, -3872.49280], + [16, -8000.32768], + [17, -16256.255], + [18, -32768.65535], + [19, 255.65535], + [20, -1.65535], + ]; } } diff --git a/test/CacheTest.php b/test/CacheTest.php index 502bcc4..54157df 100644 --- a/test/CacheTest.php +++ b/test/CacheTest.php @@ -3,96 +3,100 @@ /** * CacheTest.php * - * @since 2011-05-23 - * @category Library - * @package File - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-filecache + * @since 2011-05-23 + * @category Library + * @package File + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-filecache * * This file is part of tc-lib-pdf-filecache software library. */ namespace Test; -use PHPUnit\Framework\TestCase; - /** * Unit Test * - * @since 2011-05-23 - * @category Library - * @package File - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-filecache + * @since 2011-05-23 + * @category Library + * @package File + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-filecache */ class CacheTest extends TestUtil { - protected function getTestObject() + protected function getTestObject(): \Com\Tecnick\File\Cache { return new \Com\Tecnick\File\Cache('1_2-a+B/c'); } - public function testAutoPrefix() + public function testAutoPrefix(): void { - $obj = new \Com\Tecnick\File\Cache(); - $this->assertNotEmpty($obj->getFilePrefix()); + $cache = new \Com\Tecnick\File\Cache(); + $this->assertNotEmpty($cache->getFilePrefix()); } - public function testGetCachePath() + public function testGetCachePath(): void { - $testObj = $this->getTestObject(); - $val = $testObj->getCachePath(); - $this->assertEquals('/', $val[0]); - $this->assertEquals('/', substr($val, -1)); + $cache = $this->getTestObject(); + $cachePath = $cache->getCachePath(); + $this->assertEquals('/', $cachePath[0]); + $this->assertEquals('/', substr($cachePath, -1)); - $testObj->setCachePath(); - $this->assertEquals($val, $testObj->getCachePath()); + $cache->setCachePath(); + $this->assertEquals($cachePath, $cache->getCachePath()); $path = '/tmp'; - $testObj->setCachePath($path); - $this->assertEquals('/tmp/', $testObj->getCachePath()); + $cache->setCachePath($path); + $this->assertEquals('/tmp/', $cache->getCachePath()); } - public function testGetFilePrefix() + public function testGetFilePrefix(): void { - $testObj = $this->getTestObject(); - $val = $testObj->getFilePrefix(); - $this->assertEquals('_1_2-a-B_c_', $val); + $cache = $this->getTestObject(); + $filePrefix = $cache->getFilePrefix(); + $this->assertEquals('_1_2-a-B_c_', $filePrefix); } - public function testGetNewFileName() + public function testGetNewFileName(): void { - $testObj = $this->getTestObject(); - $val = $testObj->getNewFileName('tst', '0123'); + $cache = $this->getTestObject(); + $val = $cache->getNewFileName('tst', '0123'); + $this->assertNotFalse($val); $this->bcAssertMatchesRegularExpression('/_1_2-a-B_c_tst_0123_/', $val); } - public function testDelete() + public function testDelete(): void { - $testObj = $this->getTestObject(); + $cache = $this->getTestObject(); $idk = 0; for ($idx = 1; $idx <= 2; ++$idx) { for ($idy = 1; $idy <= 2; ++$idy) { - $file[$idk] = $testObj->getNewFileName($idx, $idy); + $file[$idk] = $cache->getNewFileName((string) $idx, (string) $idy); + $this->assertNotFalse($file[$idk]); file_put_contents($file[$idk], ''); $this->assertTrue(file_exists($file[$idk])); ++$idk; } } - $testObj->delete('2', '1'); + $cache->delete('2', '1'); + $this->assertNotFalse($file[2]); $this->assertFalse(file_exists($file[2])); - $testObj->delete('1'); + $cache->delete('1'); + $this->assertNotFalse($file[0]); $this->assertFalse(file_exists($file[0])); + $this->assertNotFalse($file[1]); $this->assertFalse(file_exists($file[1])); + $this->assertNotFalse($file[3]); $this->assertTrue(file_exists($file[3])); - $testObj->delete(); + $cache->delete(); $this->assertFalse(file_exists($file[3])); } } diff --git a/test/DirTest.php b/test/DirTest.php index 5cb3caf..f7b4157 100644 --- a/test/DirTest.php +++ b/test/DirTest.php @@ -3,35 +3,33 @@ /** * DirTest.php * - * @since 2015-07-28 - * @category Library - * @package File - * @author Nicola Asuni - * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-file + * @since 2015-07-28 + * @category Library + * @package File + * @author Nicola Asuni + * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-file * * This file is part of tc-lib-file software library. */ namespace Test; -use PHPUnit\Framework\TestCase; - /** * File Color class test * - * @since 2015-07-28 - * @category Library - * @package File - * @author Nicola Asuni - * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-file + * @since 2015-07-28 + * @category Library + * @package File + * @author Nicola Asuni + * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-file */ class DirTest extends TestUtil { - protected function getTestObject() + protected function getTestObject(): \Com\Tecnick\File\Dir { return new \Com\Tecnick\File\Dir(); } @@ -39,19 +37,18 @@ protected function getTestObject() /** * @dataProvider getAltFilePathsDataProvider */ - public function testGetAltFilePaths($name, $expected) + public function testGetAltFilePaths(string $name, string $expected): void { $testObj = $this->getTestObject(); $dir = $testObj->findParentDir($name); $this->bcAssertMatchesRegularExpression('#' . $expected . '#', $dir); } - public static function getAltFilePathsDataProvider() + /** + * @return array + */ + public static function getAltFilePathsDataProvider(): array { - return array( - array('', '/src/'), - array('missing', '/'), - array('src', '/src/'), - ); + return [['', '/src/'], ['missing', '/'], ['src', '/src/']]; } } diff --git a/test/FileTest.php b/test/FileTest.php index d780f2e..443df28 100644 --- a/test/FileTest.php +++ b/test/FileTest.php @@ -3,93 +3,96 @@ /** * FileTest.php * - * @since 2015-07-28 - * @category Library - * @package File - * @author Nicola Asuni - * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-file + * @since 2015-07-28 + * @category Library + * @package File + * @author Nicola Asuni + * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-file * * This file is part of tc-lib-file software library. */ namespace Test; -use PHPUnit\Framework\TestCase; - /** * File Color class test * - * @since 2015-07-28 - * @category Library - * @package File - * @author Nicola Asuni - * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-file + * @since 2015-07-28 + * @category Library + * @package File + * @author Nicola Asuni + * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-file */ class FileTest extends TestUtil { - protected function getTestObject() + protected function getTestObject(): \Com\Tecnick\File\File { return new \Com\Tecnick\File\File(); } - public function testFopenLocal() + public function testFopenLocal(): void { - $testObj = $this->getTestObject(); - $handle = $testObj->fopenLocal(__FILE__, 'r'); + $file = $this->getTestObject(); + $handle = $file->fopenLocal(__FILE__, 'r'); $this->bcAssertIsResource($handle); fclose($handle); } - public function testFopenLocalNonLocal() + public function testFopenLocalNonLocal(): void { - $this->bcExpectException('\Com\Tecnick\File\Exception'); - $testObj = $this->getTestObject(); - $testObj->fopenLocal('http://www.example.com/test.txt', 'r'); + $this->bcExpectException('\\' . \Com\Tecnick\File\Exception::class); + $file = $this->getTestObject(); + $file->fopenLocal('http://www.example.com/test.txt', 'r'); } - public function testFopenLocalMissing() + public function testFopenLocalMissing(): void { - $this->bcExpectException('\Com\Tecnick\File\Exception'); - $testObj = $this->getTestObject(); - $testObj->fopenLocal('/missing_error.txt', 'r'); + $this->bcExpectException('\\' . \Com\Tecnick\File\Exception::class); + $file = $this->getTestObject(); + $file->fopenLocal('/missing_error.txt', 'r'); } - public function testfReadInt() + public function testfReadInt(): void { - $testObj = $this->getTestObject(); + $file = $this->getTestObject(); $handle = fopen(__FILE__, 'r'); - $res = $testObj->fReadInt($handle); + $this->assertNotFalse($handle); + $res = $file->fReadInt($handle); // 'assertEquals(1010790504, $res); + $this->assertEquals(1_010_790_504, $res); fclose($handle); } - public function testRfRead() + public function testRfRead(): void { - $testObj = $this->getTestObject(); + $file = $this->getTestObject(); $handle = fopen(dirname(__DIR__) . '/src/File.php', 'rb'); - $res = $testObj->rfRead($handle, 2); + $this->assertNotFalse($handle); + $res = $file->rfRead($handle, 2); $this->assertEquals('rfRead($handle, 3); + $res = $file->rfRead($handle, 3); $this->assertEquals('php', $res); fclose($handle); } - public function testRfReadException() + public function testRfReadException(): void { - $this->bcExpectException('\Com\Tecnick\File\Exception'); - $testObj = $this->getTestObject(); - $testObj->rfRead(null, 2); + $this->bcExpectException('\\' . \Com\Tecnick\File\Exception::class); + $file = $this->getTestObject(); + $file->rfRead(null, 2); } /** + * @param string $file File path + * @param array{string, array} $expected Expected result + * * @dataProvider getAltFilePathsDataProvider */ - public function testGetAltFilePaths($file, $expected) + public function testGetAltFilePaths(string $file, array $expected): void { $testObj = $this->getTestObject(); $_SERVER['DOCUMENT_ROOT'] = '/var/www'; @@ -100,73 +103,78 @@ public function testGetAltFilePaths($file, $expected) $this->assertEquals($expected, $alt); } - public static function getAltFilePathsDataProvider() + /** + * Data provider for testGetAltFilePaths + * + * @return array}> + */ + public static function getAltFilePathsDataProvider(): array { - return array( - array( + return [ + [ 'http://www.example.com/test.txt', - array( - 0 => 'http://www.example.com/test.txt' - ) - ), - array( + [ + 0 => 'http://www.example.com/test.txt', + ], + ], + [ 'https://localhost/path/test.txt', - array( + [ 0 => 'https://localhost/path/test.txt', - 3 => '/var/www/path/test.txt' - ) - ), - array( + 3 => '/var/www/path/test.txt', + ], + ], + [ '//www.example.com/space test.txt', - array( + [ 0 => '//www.example.com/space test.txt', - 2 => 'https://www.example.com/space%20test.txt' - ) - ), - array( + 2 => 'https://www.example.com/space%20test.txt', + ], + ], + [ '/path/test.txt', - array( + [ 0 => '/path/test.txt', 1 => '/var/www/path/test.txt', - 4 => 'https://localhost/path/test.txt' - ) - ), - array( + 4 => 'https://localhost/path/test.txt', + ], + ], + [ 'https://localhost/path/test.php?a=0&b=1&c=2;&d="a+b%20c"', - array( - 0 => 'https://localhost/path/test.php?a=0&b=1&c=2;&d="a+b%20c"', - 2 => 'https://localhost/path/test.php?a=0&b=1&c=2;&d="a+b%20c"', - ) - ), - array( + [ + 0 => 'https://localhost/path/test.php?a=0&b=1&c=2;&d="a+b%20c"', + 2 => 'https://localhost/path/test.php?a=0&b=1&c=2;&d="a+b%20c"', + ], + ], + [ 'path/test.txt', - array( + [ 0 => 'path/test.txt', - 4 => 'https://localhost/path/test.txt' - ) - ), - ); + 4 => 'https://localhost/path/test.txt', + ], + ], + ]; } - public function testFileGetContentsException() + public function testFileGetContentsException(): void { - $this->bcExpectException('\Com\Tecnick\File\Exception'); - $testObj = $this->getTestObject(); - $testObj->fileGetContents('missing.txt'); + $this->bcExpectException('\\' . \Com\Tecnick\File\Exception::class); + $file = $this->getTestObject(); + $file->fileGetContents('missing.txt'); } - public function testFileGetContents() + public function testFileGetContents(): void { - $testObj = $this->getTestObject(); - $res = $testObj->fileGetContents(__FILE__); + $file = $this->getTestObject(); + $res = $file->fileGetContents(__FILE__); $this->assertEquals('bcExpectException('\Com\Tecnick\File\Exception'); - $testObj = $this->getTestObject(); + $this->bcExpectException('\\' . \Com\Tecnick\File\Exception::class); + $file = $this->getTestObject(); define('FORCE_CURL', true); - $testObj->fileGetContents('http://www.example.com/test.txt'); + $file->fileGetContents('http://www.example.com/test.txt'); } } diff --git a/test/TestUtil.php b/test/TestUtil.php index 60c4112..1311cbb 100644 --- a/test/TestUtil.php +++ b/test/TestUtil.php @@ -3,13 +3,13 @@ /** * TestUtil.php * - * @since 2020-12-19 - * @category Library - * @package file - * @author Nicola Asuni - * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-file + * @since 2020-12-19 + * @category Library + * @package file + * @author Nicola Asuni + * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-file * * This file is part of tc-lib-file software library. */ @@ -21,53 +21,40 @@ /** * Test Util * - * @since 2020-12-19 - * @category Library - * @package file - * @author Nicola Asuni - * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-file + * @since 2020-12-19 + * @category Library + * @package file + * @author Nicola Asuni + * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-file */ class TestUtil extends TestCase { - public function bcAssertEqualsWithDelta($expected, $actual, $delta = 0.01, $message = '') - { - if (\is_callable([self::class, 'assertEqualsWithDelta'])) { - parent::assertEqualsWithDelta($expected, $actual, $delta, $message); - return; - } - /* @phpstan-ignore-next-line */ - $this->assertEquals($expected, $actual, $message, $delta); + public function bcAssertEqualsWithDelta( + mixed $expected, + mixed $actual, + float $delta = 0.01, + string $message = '' + ): void { + parent::assertEqualsWithDelta($expected, $actual, $delta, $message); } - public function bcExpectException($exception) + /** + * @param class-string<\Throwable> $exception + */ + public function bcExpectException($exception): void { - if (\is_callable([self::class, 'expectException'])) { - parent::expectException($exception); - return; - } - /* @phpstan-ignore-next-line */ - parent::setExpectedException($exception); + parent::expectException($exception); } - public function bcAssertIsResource($res) + public function bcAssertIsResource(mixed $res): void { - if (\is_callable([self::class, 'assertIsResource'])) { - parent::assertIsResource($res); - return; - } - /* @phpstan-ignore-next-line */ - parent::assertInternalType('resource', $res); + parent::assertIsResource($res); } - public function bcAssertMatchesRegularExpression($pattern, $string, $message = '') + public function bcAssertMatchesRegularExpression(string $pattern, string $string, string $message = ''): void { - if (\is_callable([self::class, 'assertMatchesRegularExpression'])) { - parent::assertMatchesRegularExpression($pattern, $string, $message); - return; - } - /* @phpstan-ignore-next-line */ - parent::assertRegExp($pattern, $string, $message); + parent::assertMatchesRegularExpression($pattern, $string, $message); } }