From 8959733cad5fa20681ff0e58a4581d34fb12aae8 Mon Sep 17 00:00:00 2001 From: Denis Smet Date: Sun, 5 Jun 2022 20:11:24 +0300 Subject: [PATCH] Minimal PHP version is 7.4 (#31) --- .github/workflows/main.yml | 17 +--- composer.json | 10 +- phpunit.xml.dist | 27 ++--- src/Email.php | 2 +- src/IP.php | 18 +--- src/Image.php | 4 +- src/Str.php | 15 +-- src/Url.php | 2 +- src/Xml.php | 11 +- src/defines.php | 4 +- tests/EmailTest.php | 2 +- tests/FileSystemTest.php | 2 +- tests/SysTest.php | 6 +- tests/TimerTest.php | 4 +- tests/UtilsReadmeTest.php | 2 +- tests/XmlTest.php | 199 ++++++++++++++++++++++--------------- 16 files changed, 167 insertions(+), 158 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index fe10f41..e63f9d1 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -35,13 +35,9 @@ jobs: JBZOO_COMPOSER_UPDATE_FLAGS: ${{ matrix.composer_flags }} strategy: matrix: - php-version: [ 7.2, 7.3, 7.4, 8.0 ] - experimental: [ false ] + php-version: [ 7.4, 8.0, 8.1 ] coverage: [ xdebug, none ] composer_flags: [ "--prefer-lowest", "" ] - include: - - php-version: "8.1" - experimental: true steps: - name: Checkout code uses: actions/checkout@v2 @@ -56,18 +52,15 @@ jobs: tools: composer - name: Build the Project - continue-on-error: ${{ matrix.experimental }} run: make update --no-print-directory - name: ๐Ÿงช PHPUnit Tests - continue-on-error: ${{ matrix.experimental }} run: make test --no-print-directory - name: Uploading coverage to coveralls - if: ${{ matrix.coverage == 'xdebug' }} env: COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: make report-coveralls --no-print-directory + run: make report-coveralls --no-print-directory || true - name: Upload Artifacts uses: actions/upload-artifact@v2 @@ -81,7 +74,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-version: [ 7.2, 7.3, 7.4 ] + php-version: [ 7.4, 8.0, 8.1 ] steps: - name: Checkout code uses: actions/checkout@v2 @@ -112,7 +105,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-version: [ 7.2, 7.3, 7.4, 8.0 ] + php-version: [ 7.4, 8.0, 8.1 ] steps: - name: Checkout code uses: actions/checkout@v2 @@ -126,11 +119,9 @@ jobs: tools: composer - name: Build the Project - continue-on-error: ${{ matrix.experimental }} run: make update --no-print-directory - name: ๐Ÿ“ Build Reports - continue-on-error: ${{ matrix.experimental }} run: make report-all --no-print-directory - name: Upload Artifacts diff --git a/composer.json b/composer.json index c414486..4ef7737 100644 --- a/composer.json +++ b/composer.json @@ -26,15 +26,15 @@ ], "require" : { - "php" : ">=7.2", + "php" : ">=7.4", "ext-posix" : "*", "ext-filter" : "*", "ext-dom" : "*" }, "require-dev" : { - "jbzoo/toolbox-dev" : "^3.1.0", - "symfony/process" : ">=4.4" + "jbzoo/toolbox-dev" : "^4.0.1", + "symfony/process" : ">=4.4,<6.0" }, "suggest" : { @@ -62,8 +62,8 @@ "config" : { "optimize-autoloader" : true, - "allow-plugins": { - "composer/package-versions-deprecated": false + "allow-plugins" : { + "composer/package-versions-deprecated" : false } }, diff --git a/phpunit.xml.dist b/phpunit.xml.dist index d810b1a..71fb99a 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -11,7 +11,8 @@ @copyright Copyright (C) JBZoo.com, All rights reserved. @link https://github.com/JBZoo/Utils --> - + + + src + + + + + + + + tests - - - src - - - - - - - + - diff --git a/src/Email.php b/src/Email.php index 0a18e47..5e4013f 100644 --- a/src/Email.php +++ b/src/Email.php @@ -80,7 +80,7 @@ public static function checkDns(string $email): bool $domain = self::extractDomain($email); - return !(\checkdnsrr($domain, 'MX') === false); + return \checkdnsrr($domain); } /** diff --git a/src/IP.php b/src/IP.php index 177339b..58568c6 100644 --- a/src/IP.php +++ b/src/IP.php @@ -78,22 +78,14 @@ public static function v4InRange(string $ipAddress, string $range): bool // $netMask is a CIDR size block // fix the range argument - $blocks = \explode('.', $range); - - $expectedNumOfParts = 4; - /** @phan-suppress-next-line PhanPossiblyInfiniteLoop */ - while (\count($blocks) < $expectedNumOfParts) { - $blocks[] = '0'; - } - - [$blockA, $blockB, $blockC, $blockD] = $blocks; + $blocks = \explode('.', $range, 4); $range = \sprintf( '%u.%u.%u.%u', - (int)(empty($blockA) ? '0' : $blockA), - (int)(empty($blockB) ? '0' : $blockB), - (int)(empty($blockC) ? '0' : $blockC), - (int)(empty($blockD) ? '0' : $blockD) + (int)($blocks[0] ?? 0), + (int)($blocks[1] ?? 0), + (int)($blocks[2] ?? 0), + (int)($blocks[3] ?? 0) ); $rangeDec = \ip2long($range); diff --git a/src/Image.php b/src/Image.php index bf9cc54..16bfcbc 100644 --- a/src/Image.php +++ b/src/Image.php @@ -239,8 +239,8 @@ public static function imageCopyMergeAlpha( // Get image width and height and percentage $opacity /= 100; - $width = (int)\imagesx($srcImg); - $height = (int)\imagesy($srcImg); + $width = \imagesx($srcImg) ?: 0; + $height = \imagesy($srcImg) ?: 0; // Turn alpha blending off self::addAlpha($srcImg, false); diff --git a/src/Str.php b/src/Str.php index 2d15d17..7629bf5 100644 --- a/src/Str.php +++ b/src/Str.php @@ -32,7 +32,7 @@ final class Str * * @var string */ - public static $encoding = 'UTF-8'; + public static string $encoding = 'UTF-8'; /** * Strip all whitespaces from the given string. @@ -340,19 +340,6 @@ public static function slug(string $text = '', bool $isCache = false): string return $cache[$text]; } - /** - * Check is mbstring overload standard functions - * @return bool - */ - public static function isOverload(): bool - { - if (\defined('MB_OVERLOAD_STRING') && self::isMBString()) { - return (bool)(Filter::int(Sys::iniGet('mbstring.func_overload')) & \MB_OVERLOAD_STRING); - } - - return false; - } - /** * Check is mbstring loaded * diff --git a/src/Url.php b/src/Url.php index bb58f9b..43cb647 100644 --- a/src/Url.php +++ b/src/Url.php @@ -63,7 +63,7 @@ final class Url */ public static function addArg(array $newParams, ?string $uri = null): string { - $uri = $uri ?? ($_SERVER['REQUEST_URI'] ?? ''); + $uri ??= ($_SERVER['REQUEST_URI'] ?? ''); // Parse the URI into it's components $parsedUri = data((array)\parse_url((string)$uri)); diff --git a/src/Xml.php b/src/Xml.php index 61e598d..bfaf8b9 100644 --- a/src/Xml.php +++ b/src/Xml.php @@ -42,13 +42,11 @@ public static function escape($rawXmlContent): string $rawXmlContent ); - $rawXmlContent = \str_replace( + return \str_replace( ['&', '<', '>', '"', "'"], ['&', '<', '>', '"', '''], $rawXmlContent ); - - return $rawXmlContent; } /** @@ -121,13 +119,10 @@ public static function array2Dom( $document = self::createFromString(); } - $domElement = $domElement ?? $document; + $domElement ??= $document; if (\array_key_exists('_text', $xmlAsArray) && $xmlAsArray['_text'] !== null) { - $newNode = $document->createTextNode($xmlAsArray['_text']); - if ($newNode !== false) { - $domElement->appendChild($newNode); - } + $domElement->appendChild(new \DOMText($xmlAsArray['_text'])); } if (\array_key_exists('_cdata', $xmlAsArray) && $xmlAsArray['_cdata'] !== null) { diff --git a/src/defines.php b/src/defines.php index d09aae9..61c630f 100644 --- a/src/defines.php +++ b/src/defines.php @@ -15,5 +15,5 @@ declare(strict_types=1); -$_SERVER['REQUEST_TIME_FLOAT'] = $_SERVER['REQUEST_TIME_FLOAT'] ?? microtime(true); -$_SERVER['REQUEST_TIME'] = $_SERVER['REQUEST_TIME'] ?? $_SERVER['REQUEST_TIME_FLOAT']; +$_SERVER['REQUEST_TIME_FLOAT'] ??= microtime(true); +$_SERVER['REQUEST_TIME'] ??= $_SERVER['REQUEST_TIME_FLOAT']; diff --git a/tests/EmailTest.php b/tests/EmailTest.php index 714035f..d3c4003 100644 --- a/tests/EmailTest.php +++ b/tests/EmailTest.php @@ -260,7 +260,7 @@ public function getGravatarUrlProvider(): array public function testRandomEmail(): void { - isTrue(Email::check(Email::random())); + isTrue((bool)Email::check(Email::random())); isTrue(Email::isValid(Email::random())); isNotSame(Email::random(), Email::random()); diff --git a/tests/FileSystemTest.php b/tests/FileSystemTest.php index 7fbc16b..89e1625 100644 --- a/tests/FileSystemTest.php +++ b/tests/FileSystemTest.php @@ -138,7 +138,7 @@ public function testFirstLine(): void public function testPerms(): void { - isFalse(FS::perms('/no/such/file')); + isFalse((bool)FS::perms('/no/such/file')); } public function testWritable(): void diff --git a/tests/SysTest.php b/tests/SysTest.php index 65b7649..18297cc 100644 --- a/tests/SysTest.php +++ b/tests/SysTest.php @@ -77,9 +77,9 @@ public function testIsPHPVersion(): void public function testGetMemory(): void { - isTrue(Sys::getMemory()); - isTrue(Sys::getMemory(true)); - isTrue(Sys::getMemory(false)); + isTrue((bool)Sys::getMemory()); + isTrue((bool)Sys::getMemory(true)); + isTrue((bool)Sys::getMemory(false)); } public function testGetDocumentRoot(): void diff --git a/tests/TimerTest.php b/tests/TimerTest.php index 62401c6..d7e613d 100644 --- a/tests/TimerTest.php +++ b/tests/TimerTest.php @@ -50,12 +50,12 @@ public function testSecondsToTimeStringInMillisecond($string, $seconds): void public function testGetRequestTime(): void { - isTrue(Timer::getRequestTime()); + isTrue(Timer::getRequestTime() > 0); } public function testTimeSinceStart(): void { - isTrue(Timer::timeSinceStart()); + isTrue(Timer::timeSinceStart() > 0); } /** diff --git a/tests/UtilsReadmeTest.php b/tests/UtilsReadmeTest.php index 98d3dd7..d6a1930 100644 --- a/tests/UtilsReadmeTest.php +++ b/tests/UtilsReadmeTest.php @@ -47,7 +47,7 @@ */ class UtilsReadmeTest extends AbstractReadmeTest { - protected $packageName = 'Utils'; + protected string $packageName = 'Utils'; protected function setUp(): void { diff --git a/tests/XmlTest.php b/tests/XmlTest.php index a3b867a..2c475a3 100644 --- a/tests/XmlTest.php +++ b/tests/XmlTest.php @@ -26,12 +26,12 @@ */ class XmlTest extends PHPUnit { - private $xmlFixture = PROJECT_ROOT . '/tests/resources/some-xml-file.xml'; + private string $xmlFixture = PROJECT_ROOT . '/tests/resources/some-xml-file.xml'; /** * @var array */ - private $expected = [ + private array $expected = [ '_node' => '#document', '_text' => null, '_cdata' => null, @@ -122,24 +122,26 @@ class XmlTest extends PHPUnit /** * @var string[] */ - private $expectedXml = [ + private array $expectedXml = [ '', - '', + '', + ' ', + ' ', + ' src', + ' ', + ' ', + ' ', + ' ', + ' ', + ' ', + ' ', ' ', ' ', ' tests', ' ', ' ', - ' ', - ' ', - ' src', - ' ', - ' ', ' ', - ' ', - ' ', - ' ', - ' ', + ' ', ' ', '', '', @@ -148,86 +150,125 @@ class XmlTest extends PHPUnit /** * @var array */ - private $minimalSource = [ + private array $minimalSource = [ '_children' => [ [ - '_node' => 'phpunit', - '_attrs' => [ - 'bootstrap' => 'tests/autoload.php', - 'convertErrorsToExceptions' => 'true', - 'convertNoticesToExceptions' => 'true', - 'convertWarningsToExceptions' => 'true', - 'convertDeprecationsToExceptions' => 'true', - 'executionOrder' => 'random', - 'processIsolation' => 'false', - 'stopOnError' => 'false', - 'stopOnFailure' => 'false', - 'stopOnIncomplete' => 'false', - 'stopOnSkipped' => 'false', - 'stopOnRisky' => 'false', + "_node" => "phpunit", + "_text" => null, + "_cdata" => null, + "_attrs" => [ + "bootstrap" => "tests/autoload.php", + "convertErrorsToExceptions" => "true", + "convertNoticesToExceptions" => "true", + "convertWarningsToExceptions" => "true", + "convertDeprecationsToExceptions" => "true", + "executionOrder" => "random", + "processIsolation" => "false", + "stopOnError" => "false", + "stopOnFailure" => "false", + "stopOnIncomplete" => "false", + "stopOnSkipped" => "false", + "stopOnRisky" => "false", + "noNamespaceSchemaLocation" => "https://schema.phpunit.de/9.3/phpunit.xsd" ], - '_children' => [ + "_children" => [ [ - '_node' => 'testsuites', - '_children' => [ + "_node" => "coverage", + "_text" => null, + "_cdata" => null, + "_attrs" => ["processUncoveredFiles" => "true"], + "_children" => [ [ - '_node' => 'testsuite', - '_attrs' => ['name' => 'PHPUnit'], - '_children' => [ + "_node" => "include", + "_text" => null, + "_cdata" => null, + "_attrs" => [], + "_children" => [ [ - '_node' => 'directory', - '_text' => 'tests', - '_attrs' => ['suffix' => 'Test.php'], - ], - ], + "_node" => "directory", + "_text" => "src", + "_cdata" => null, + "_attrs" => ["suffix" => ".php"], + "_children" => [] + ] + ] ], - ], - ], - [ - '_node' => 'filter', - '_children' => [ [ - '_node' => 'whitelist', - '_attrs' => ['processUncoveredFilesFromWhitelist' => 'true'], - '_children' => [ + "_node" => "report", + "_text" => null, + "_cdata" => null, + "_attrs" => [], + "_children" => [ [ - '_node' => 'directory', - '_text' => 'src', - '_attrs' => ['suffix' => '.php'], + "_node" => "clover", + "_text" => null, + "_cdata" => null, + "_attrs" => ["outputFile" => "build/coverage_xml/main.xml"], + "_children" => [] ], - ], - ], - ], + [ + "_node" => "php", + "_text" => null, + "_cdata" => null, + "_attrs" => ["outputFile" => "build/coverage_cov/main.cov"], + "_children" => [] + ], + [ + "_node" => "text", + "_text" => null, + "_cdata" => null, + "_attrs" => [ + "outputFile" => "php://stdout", + "showUncoveredFiles" => "false", + "showOnlySummary" => "true" + ], + "_children" => [] + ] + ] + ] + ] ], [ - '_node' => 'logging', - '_children' => [ - [ - '_node' => 'log', - '_attrs' => ['type' => 'coverage-clover', 'target' => 'build/coverage_xml/main.xml'], - ], - [ - '_node' => 'log', - '_attrs' => ['type' => 'coverage-php', 'target' => 'build/coverage_cov/main.cov'], - ], + "_node" => "testsuites", + "_text" => null, + "_cdata" => null, + "_attrs" => [], + "_children" => [ [ - '_node' => 'log', - '_attrs' => ['type' => 'junit', 'target' => 'build/coverage_junit/main.xml'], - ], - [ - '_node' => 'log', - '_attrs' => [ - 'type' => 'coverage-text', - 'target' => 'php://stdout', - 'showUncoveredFiles' => 'false', - 'showOnlySummary' => 'true', - ], - ], - ], + "_node" => "testsuite", + "_text" => null, + "_cdata" => null, + "_attrs" => ["name" => "PHPUnit"], + "_children" => [ + [ + "_node" => "directory", + "_text" => "tests", + "_cdata" => null, + "_attrs" => ["suffix" => "Test.php"], + "_children" => [] + ] + ] + ] + ] ], - ], - ], - ], + [ + "_node" => "logging", + "_text" => null, + "_cdata" => null, + "_attrs" => [], + "_children" => [ + [ + "_node" => "junit", + "_text" => null, + "_cdata" => null, + "_attrs" => ["outputFile" => "build/coverage_junit/main.xml"], + "_children" => [] + ] + ] + ] + ] + ] + ] ]; public function testEscape(): void