From 417ba207700a2d6b003c52337ded0335d9e25ccc Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 2 Nov 2024 18:11:14 +0700 Subject: [PATCH] Update to latest PHP 8.1 syntax Signed-off-by: Abdul Malik Ikhsan --- src/Barcode/Code128.php | 2 +- src/EmailAddress.php | 2 +- src/Explode.php | 2 +- src/File/Exists.php | 9 ++++++--- src/File/NotExists.php | 9 ++++++--- src/HostWithPublicIPv4Address.php | 3 +-- src/Hostname.php | 2 +- src/Ip.php | 3 +-- src/UndisclosedPassword.php | 6 ++++-- src/ValidatorChain.php | 2 +- test/CallbackTest.php | 12 +++++------- test/ConditionalFactoryTest.php | 4 +--- test/ConditionalTest.php | 12 +++--------- test/StringLengthTest.php | 2 +- test/ValidatorChainTest.php | 4 ++-- 15 files changed, 35 insertions(+), 39 deletions(-) diff --git a/src/Barcode/Code128.php b/src/Barcode/Code128.php index d2e1634a..b7a62ce4 100644 --- a/src/Barcode/Code128.php +++ b/src/Barcode/Code128.php @@ -14,7 +14,7 @@ final class Code128 implements AdapterInterface { - private StringWrapperInterface $utf8StringWrapper; + private readonly StringWrapperInterface $utf8StringWrapper; public function __construct() { diff --git a/src/EmailAddress.php b/src/EmailAddress.php index c528999a..8918ecf9 100644 --- a/src/EmailAddress.php +++ b/src/EmailAddress.php @@ -345,7 +345,7 @@ public function isValid(mixed $value): bool $this->setValue($value); // Split email address up and disallow '..' - $split = $this->splitEmailParts($value); + $split = self::splitEmailParts($value); if ($split === false) { $this->error(self::INVALID_FORMAT); return false; diff --git a/src/Explode.php b/src/Explode.php index cc2951e5..e2c7066e 100644 --- a/src/Explode.php +++ b/src/Explode.php @@ -42,7 +42,7 @@ final class Explode extends AbstractValidator /** @var non-empty-string */ private readonly string $valueDelimiter; private ValidatorPluginManager|null $pluginManager; - private ValidatorInterface $validator; + private readonly ValidatorInterface $validator; private readonly bool $breakOnFirstFailure; protected int $count = 0; diff --git a/src/File/Exists.php b/src/File/Exists.php index 59e0f48c..97479409 100644 --- a/src/File/Exists.php +++ b/src/File/Exists.php @@ -126,8 +126,11 @@ private function resolveDirectories(string|array|null $directories): array $directories = explode(',', $directories); } - return array_values(array_filter(array_map(static function (string $directory): string { - return trim($directory); - }, $directories))); + return array_values( + array_filter( + array_map(static fn(string $directory): string + => trim($directory), $directories) + ) + ); } } diff --git a/src/File/NotExists.php b/src/File/NotExists.php index fbbda216..05d6581d 100644 --- a/src/File/NotExists.php +++ b/src/File/NotExists.php @@ -118,8 +118,11 @@ private function resolveDirectories(string|array|null $directories): array $directories = explode(',', $directories); } - return array_values(array_filter(array_map(static function (string $directory): string { - return trim($directory); - }, $directories))); + return array_values( + array_filter( + array_map(static fn(string $directory): string + => trim($directory), $directories) + ) + ); } } diff --git a/src/HostWithPublicIPv4Address.php b/src/HostWithPublicIPv4Address.php index d671cc76..37a9ebff 100644 --- a/src/HostWithPublicIPv4Address.php +++ b/src/HostWithPublicIPv4Address.php @@ -11,7 +11,6 @@ use function ip2long; use function is_array; use function is_string; -use function pow; use const FILTER_FLAG_GLOBAL_RANGE; use const FILTER_FLAG_IPV4; @@ -144,7 +143,7 @@ private function inReservedCidr(string $ip): bool foreach (self::RESERVED_CIDR as $cidr) { $cidr = explode('/', $cidr); $startIp = ip2long($cidr[0]); - $endIp = ip2long($cidr[0]) + pow(2, 32 - (int) $cidr[1]) - 1; + $endIp = ip2long($cidr[0]) + 2 ** (32 - (int) $cidr[1]) - 1; $int = ip2long($ip); diff --git a/src/Hostname.php b/src/Hostname.php index a2e04180..770b4223 100644 --- a/src/Hostname.php +++ b/src/Hostname.php @@ -2148,6 +2148,6 @@ private function decodePunycode(string $encoded): string|false } } - return implode($decoded); + return implode('', $decoded); } } diff --git a/src/Ip.php b/src/Ip.php index f89d9644..db8ce439 100644 --- a/src/Ip.php +++ b/src/Ip.php @@ -14,7 +14,6 @@ use function preg_match; use function str_contains; use function strlen; -use function strpos; use function strrpos; use function substr; use function substr_count; @@ -143,7 +142,7 @@ private function validateIPv6(string $value): bool return $value === '::'; } - if (strpos($value, '.') !== false) { + if (str_contains($value, '.')) { $lastcolon = strrpos($value, ':'); if (! ($lastcolon !== false && $this->validateIPv4(substr($value, $lastcolon + 1)))) { return false; diff --git a/src/UndisclosedPassword.php b/src/UndisclosedPassword.php index 1879c403..2d306fcc 100644 --- a/src/UndisclosedPassword.php +++ b/src/UndisclosedPassword.php @@ -43,8 +43,10 @@ final class UndisclosedPassword extends AbstractValidator ]; // phpcs:enable - public function __construct(private ClientInterface $httpClient, private RequestFactoryInterface $makeHttpRequest) - { + public function __construct( + private readonly ClientInterface $httpClient, + private readonly RequestFactoryInterface $makeHttpRequest + ) { parent::__construct(); } diff --git a/src/ValidatorChain.php b/src/ValidatorChain.php index 890b59dc..101a453e 100644 --- a/src/ValidatorChain.php +++ b/src/ValidatorChain.php @@ -180,7 +180,7 @@ public function attachByName( } } - $bc = $bc ?? $breakChainOnFailure; + $bc ??= $breakChainOnFailure; $this->attach($this->plugin($name, $options), $bc, $priority); } diff --git a/test/CallbackTest.php b/test/CallbackTest.php index ada50d85..5b862fcb 100644 --- a/test/CallbackTest.php +++ b/test/CallbackTest.php @@ -56,9 +56,8 @@ public function testOptionsArePassedAsThirdArgumentWhenContextIsNonEmpty(): void $givenContext = ['baz' => 'bat']; $validator = new Callback([ 'throwExceptions' => true, - 'callback' => static function (mixed $value, array $context, string $foo) use ($givenContext): bool { - return $value === 'test' && $foo === 'foo' && $context === $givenContext; - }, + 'callback' => static fn(mixed $value, array $context, string $foo): bool + => $value === 'test' && $foo === 'foo' && $context === $givenContext, 'callbackOptions' => ['foo' => 'foo'], ]); @@ -70,9 +69,8 @@ public function testContextIsSecondArgumentWhenGiven(): void $givenContext = ['baz' => 'bat']; $validator = new Callback([ 'throwExceptions' => true, - 'callback' => static function (mixed $value, array $context) use ($givenContext): bool { - return $value === 'test' && $context === $givenContext; - }, + 'callback' => static fn(mixed $value, array $context): bool + => $value === 'test' && $context === $givenContext, 'callbackOptions' => [], ]); @@ -109,7 +107,7 @@ public function testThatCallbackExceptionsCanBeAllowedToPropagate(): void try { $validator->isValid('whatever'); self::fail('An exception should have been thrown'); - } catch (Throwable $error) { + } catch (Throwable) { self::assertSame('Callback Exception', $exception->getMessage()); } diff --git a/test/ConditionalFactoryTest.php b/test/ConditionalFactoryTest.php index 49aa42aa..ed702d10 100644 --- a/test/ConditionalFactoryTest.php +++ b/test/ConditionalFactoryTest.php @@ -23,9 +23,7 @@ public function testThatOptionsArePassedToTheValidator(): void $factory = new ConditionalFactory(); $validator = $factory->__invoke($container, 'Whatever', [ - 'rule' => static function (array $context): bool { - return ($context['trigger'] ?? null) === true; - }, + 'rule' => static fn(array $context): bool => ($context['trigger'] ?? null) === true, 'validators' => [ ['name' => NotEmpty::class], ], diff --git a/test/ConditionalTest.php b/test/ConditionalTest.php index 65292283..7dbbac92 100644 --- a/test/ConditionalTest.php +++ b/test/ConditionalTest.php @@ -30,9 +30,7 @@ public function testConditionalValidationHappyPath(): void $validator = new Conditional( $this->factory, [ - 'rule' => static function (array $context): bool { - return ($context['trigger'] ?? null) === true; - }, + 'rule' => static fn(array $context): bool => ($context['trigger'] ?? null) === true, 'validators' => [ ['name' => NotEmpty::class], ], @@ -60,9 +58,7 @@ public function testMessagesFromTheValidationChainAreReturned(): void $validator = new Conditional( $this->factory, [ - 'rule' => static function (array $context): bool { - return ($context['trigger'] ?? null) === true; - }, + 'rule' => static fn(array $context): bool => ($context['trigger'] ?? null) === true, 'validators' => [ ['name' => Digits::class], ], @@ -101,9 +97,7 @@ public function testPluginManagerIntegration(): void $plugins = $container->get(ValidatorPluginManager::class); self::assertInstanceOf(ValidatorPluginManager::class, $plugins); $validator = $plugins->build(Conditional::class, [ - 'rule' => static function (array $context): bool { - return ($context['trigger'] ?? null) === true; - }, + 'rule' => static fn(array $context): bool => ($context['trigger'] ?? null) === true, 'validators' => [ ['name' => Digits::class], ], diff --git a/test/StringLengthTest.php b/test/StringLengthTest.php index 2a381da9..ba261837 100644 --- a/test/StringLengthTest.php +++ b/test/StringLengthTest.php @@ -89,7 +89,7 @@ public function testMalformedMultiByteDataWillCauseValidationFailure(): void * Warnings are silenced to prevent the test from failing */ // phpcs:disable - set_error_handler(function (int $_a, string $_b): bool { return true; }, E_WARNING); + set_error_handler(fn(int $_a, string $_b): bool => true, E_WARNING); // phpcs:enable $malformed = chr(0xED) . chr(0xA0) . chr(0x80); diff --git a/test/ValidatorChainTest.php b/test/ValidatorChainTest.php index 4b523507..fcc47db7 100644 --- a/test/ValidatorChainTest.php +++ b/test/ValidatorChainTest.php @@ -22,7 +22,7 @@ use function array_keys; use function array_shift; use function serialize; -use function strstr; +use function str_contains; use function unserialize; final class ValidatorChainTest extends TestCase @@ -196,7 +196,7 @@ public function testCountGivesCountOfAttachedValidators(): void #[Group('Laminas-2724')] public function handleNotFoundError(int $errnum, string $errstr): void { - if (strstr($errstr, 'No such file') !== false) { + if (str_contains($errstr, 'No such file')) { $this->error = true; } }