Skip to content

Commit

Permalink
Merge pull request #397 from samsonasik/update-to-php81-syntax
Browse files Browse the repository at this point in the history
Update to latest PHP 8.1 syntax
  • Loading branch information
gsteel authored Nov 3, 2024
2 parents 6139271 + 417ba20 commit 7a7ed30
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/Barcode/Code128.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

final class Code128 implements AdapterInterface
{
private StringWrapperInterface $utf8StringWrapper;
private readonly StringWrapperInterface $utf8StringWrapper;

public function __construct()
{
Expand Down
2 changes: 1 addition & 1 deletion src/EmailAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Explode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions src/File/Exists.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
);
}
}
9 changes: 6 additions & 3 deletions src/File/NotExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
);
}
}
3 changes: 1 addition & 2 deletions src/HostWithPublicIPv4Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/Hostname.php
Original file line number Diff line number Diff line change
Expand Up @@ -2148,6 +2148,6 @@ private function decodePunycode(string $encoded): string|false
}
}

return implode($decoded);
return implode('', $decoded);
}
}
3 changes: 1 addition & 2 deletions src/Ip.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/UndisclosedPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion src/ValidatorChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public function attachByName(
}
}

$bc = $bc ?? $breakChainOnFailure;
$bc ??= $breakChainOnFailure;

$this->attach($this->plugin($name, $options), $bc, $priority);
}
Expand Down
12 changes: 5 additions & 7 deletions test/CallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
]);

Expand All @@ -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' => [],
]);

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

Expand Down
4 changes: 1 addition & 3 deletions test/ConditionalFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
],
Expand Down
12 changes: 3 additions & 9 deletions test/ConditionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
],
Expand Down Expand Up @@ -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],
],
Expand Down Expand Up @@ -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],
],
Expand Down
2 changes: 1 addition & 1 deletion test/StringLengthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions test/ValidatorChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 7a7ed30

Please sign in to comment.