diff --git a/src/Between.php b/src/Between.php index 4aec6a22..8a34f4ae 100644 --- a/src/Between.php +++ b/src/Between.php @@ -23,10 +23,8 @@ class Between extends AbstractValidator /** * Retain if min and max are numeric values. Allow to not compare string and numeric types - * - * @var boolean */ - private $numeric; + private ?bool $numeric = null; /** * Validation failure message template definitions diff --git a/src/CreditCard.php b/src/CreditCard.php index ff59e97c..24030ed6 100644 --- a/src/CreditCard.php +++ b/src/CreditCard.php @@ -381,7 +381,7 @@ public function isValid($value) $foundl = false; foreach ($types as $type) { foreach ($this->cardType[$type] as $prefix) { - if (0 === strpos($value, $prefix)) { + if (0 === strpos($value, (string) $prefix)) { $foundp = true; if (in_array($length, $this->cardLength[$type])) { $foundl = true; diff --git a/src/DateStep.php b/src/DateStep.php index 3a3902e6..2ec86dac 100644 --- a/src/DateStep.php +++ b/src/DateStep.php @@ -25,7 +25,6 @@ use function is_array; use function max; use function min; -use function pow; use function preg_match; use function sprintf; use function strpos; @@ -463,7 +462,7 @@ private function computeMinStepAndRequiredIterations(array $intervalParts, array // If we use PHP_INT_MAX DateInterval::__construct falls over with a bad format error // before we reach the max on 64 bit machines - $maxInteger = min(pow(2, 31), PHP_INT_MAX); + $maxInteger = min(2 ** 31, PHP_INT_MAX); // check for integer overflow and split $minimum interval if needed $maximumInterval = max($intervalParts); $requiredStepIterations = 1; diff --git a/src/File/Upload.php b/src/File/Upload.php index 2f682255..10a0f2f3 100644 --- a/src/File/Upload.php +++ b/src/File/Upload.php @@ -2,7 +2,6 @@ namespace Laminas\Validator\File; -use Countable; use Laminas\Validator\AbstractValidator; use Laminas\Validator\Exception; use Psr\Http\Message\UploadedFileInterface; @@ -12,6 +11,7 @@ use function array_merge; use function count; use function is_array; +use function is_countable; use function is_string; use function is_uploaded_file; @@ -117,7 +117,7 @@ public function setFiles($files = []) { if ( null === $files - || ((is_array($files) || $files instanceof Countable) + || ((is_countable($files)) && count($files) === 0) ) { $this->options['files'] = $_FILES; diff --git a/src/IsCountable.php b/src/IsCountable.php index 8793db25..e62ad655 100644 --- a/src/IsCountable.php +++ b/src/IsCountable.php @@ -2,11 +2,11 @@ namespace Laminas\Validator; -use Countable; use Traversable; use function count; use function is_array; +use function is_countable; use function is_numeric; use function sprintf; use function ucfirst; @@ -94,7 +94,7 @@ public function setOptions($options = []) */ public function isValid($value) { - if (! (is_array($value) || $value instanceof Countable)) { + if (! is_countable($value)) { $this->error(self::NOT_COUNTABLE); return false; } diff --git a/src/UndisclosedPassword.php b/src/UndisclosedPassword.php index 7e01a1b8..aa89e13c 100644 --- a/src/UndisclosedPassword.php +++ b/src/UndisclosedPassword.php @@ -39,13 +39,9 @@ final class UndisclosedPassword extends AbstractValidator self::NOT_A_STRING => 'The provided password is not a string, please provide a correct password', ]; - // phpcs:enable - - /** @var ClientInterface */ - private $httpClient; + private ClientInterface $httpClient; - /** @var RequestFactoryInterface */ - private $makeHttpRequest; + private RequestFactoryInterface $makeHttpRequest; public function __construct(ClientInterface $httpClient, RequestFactoryInterface $makeHttpRequest) { diff --git a/test/BetweenTest.php b/test/BetweenTest.php index 5af9c8a2..64300cd6 100644 --- a/test/BetweenTest.php +++ b/test/BetweenTest.php @@ -150,14 +150,14 @@ public function providerBasic(): array ], 'inclusive-int-invalid-string' => [ 'min' => 0, - 'max' => 99999999, + 'max' => 99_999_999, 'inclusive' => true, 'expected' => false, 'value' => 'asdasd', ], 'inclusive-int-invalid-char' => [ 'min' => 0, - 'max' => 99999999, + 'max' => 99_999_999, 'inclusive' => true, 'expected' => false, 'value' => 'q', diff --git a/test/CallbackTest.php b/test/CallbackTest.php index e6c1ad8a..6b6219e4 100644 --- a/test/CallbackTest.php +++ b/test/CallbackTest.php @@ -90,9 +90,7 @@ public function testCanAcceptContextWithoutOptions(): void { $value = 'bar'; $context = ['foo' => 'bar', 'bar' => 'baz']; - $validator = new Callback(function ($v, $c) use ($value, $context) { - return ($value === $v) && ($context === $c); - }); + $validator = new Callback(static fn($v, $c) => ($value === $v) && ($context === $c)); $this->assertTrue($validator->isValid($value, $context)); } @@ -101,9 +99,10 @@ public function testCanAcceptContextWithOptions(): void $value = 'bar'; $context = ['foo' => 'bar', 'bar' => 'baz']; $options = ['baz' => 'bat']; - $validator = new Callback(function ($v, $c, $baz) use ($value, $context, $options) { - return ($value === $v) && ($context === $c) && ($options['baz'] === $baz); - }); + $validator = new Callback( + static fn($v, $c, $baz) => ($value === $v) + && ($context === $c) && ($options['baz'] === $baz) + ); $validator->setCallbackOptions($options); $this->assertTrue($validator->isValid($value, $context)); } diff --git a/test/DateTest.php b/test/DateTest.php index 22dae3a0..4b164890 100644 --- a/test/DateTest.php +++ b/test/DateTest.php @@ -80,11 +80,11 @@ public function datesDataProvider(): array ['6', 'd', true, false], ['06', 'd', true, true], [123, null, true, false], - [1340677235, null, true, false], - [1340677235, 'U', true, false], + [1_340_677_235, null, true, false], + [1_340_677_235, 'U', true, false], ['1340677235', 'U', true, true], // 32bit version of php will convert this to double - [999999999999, null, true, false], + [999_999_999_999, null, true, false], // double [12.12, null, false, false], // array diff --git a/test/ExplodeTest.php b/test/ExplodeTest.php index 16943023..db994707 100644 --- a/test/ExplodeTest.php +++ b/test/ExplodeTest.php @@ -189,7 +189,7 @@ public function testIsValidPassContext(): void $context = 'context'; $contextSame = false; $validator = new Explode([ - 'validator' => new Callback(function ($v, $c) use ($context, &$contextSame) { + 'validator' => new Callback(static function ($v, $c) use ($context, &$contextSame) { $contextSame = $context === $c; return true; }), diff --git a/test/File/CountTest.php b/test/File/CountTest.php index d8b4ea74..06a370e6 100644 --- a/test/File/CountTest.php +++ b/test/File/CountTest.php @@ -121,17 +121,17 @@ public function testGetMax() public function testSetMax() { $validator = new File\Count(['min' => 1000, 'max' => 10000]); - $validator->setMax(1000000); - $this->assertEquals(1000000, $validator->getMax()); + $validator->setMax(1_000_000); + $this->assertEquals(1_000_000, $validator->getMax()); $validator->setMin(100); - $this->assertEquals(1000000, $validator->getMax()); + $this->assertEquals(1_000_000, $validator->getMax()); } public function testCanSetMaxValueUsingAnArrayWithMaxKey(): void { $validator = new File\Count(['min' => 1000, 'max' => 10000]); - $maxValue = 33333333; + $maxValue = 33_333_333; $setMaxArray = ['max' => $maxValue]; $validator->setMax($setMaxArray); diff --git a/test/File/FilesSizeTest.php b/test/File/FilesSizeTest.php index a4c5e7be..3b12d36d 100644 --- a/test/File/FilesSizeTest.php +++ b/test/File/FilesSizeTest.php @@ -161,7 +161,7 @@ public function testGetMax() public function testSetMax() { $validator = new File\FilesSize(['min' => 1000, 'max' => 10000]); - $validator->setMax(1000000); + $validator->setMax(1_000_000); $this->assertEquals('976.56kB', $validator->getMax()); $validator->setMin(100); diff --git a/test/File/MimeTypeTest.php b/test/File/MimeTypeTest.php index 541663d1..b1708114 100644 --- a/test/File/MimeTypeTest.php +++ b/test/File/MimeTypeTest.php @@ -12,7 +12,6 @@ use function basename; use function current; -use function dirname; use function extension_loaded; use function getenv; use function is_array; @@ -240,7 +239,7 @@ public function testDisablingMagicFileByConstructor(): void $files = [ 'name' => 'picture.jpg', 'size' => 200, - 'tmp_name' => dirname(__FILE__) . '/_files/picture.jpg', + 'tmp_name' => __DIR__ . '/_files/picture.jpg', 'error' => 0, 'magicFile' => false, ]; diff --git a/test/File/SizeTest.php b/test/File/SizeTest.php index d29dabab..36fa13c4 100644 --- a/test/File/SizeTest.php +++ b/test/File/SizeTest.php @@ -159,7 +159,7 @@ public function testSetMax() $validator = new File\Size(['max' => 0, 'useByteString' => true]); $this->assertEquals('0B', $validator->getMax()); - $validator->setMax(1000000); + $validator->setMax(1_000_000); $this->assertEquals('976.56kB', $validator->getMax()); $validator->setMin(100); diff --git a/test/File/WordCountTest.php b/test/File/WordCountTest.php index 540b56bf..308868ab 100644 --- a/test/File/WordCountTest.php +++ b/test/File/WordCountTest.php @@ -139,11 +139,11 @@ public function testGetMax() public function testSetMax() { $validator = new File\WordCount(['min' => 1000, 'max' => 10000]); - $validator->setMax(1000000); - $this->assertEquals(1000000, $validator->getMax()); + $validator->setMax(1_000_000); + $this->assertEquals(1_000_000, $validator->getMax()); $validator->setMin(100); - $this->assertEquals(1000000, $validator->getMax()); + $this->assertEquals(1_000_000, $validator->getMax()); } /** @@ -216,7 +216,7 @@ public function testSettingMinValueRaisesExceptionForInvalidType($value): void public function testCanSetMaxValueUsingOptionsArray(): void { $validator = new File\WordCount(['min' => 1000, 'max' => 10000]); - $maxValue = 33333333; + $maxValue = 33_333_333; $options = ['max' => $maxValue]; $validator->setMax($options); diff --git a/test/IsCountableTest.php b/test/IsCountableTest.php index c1bb8134..7aaba8f1 100644 --- a/test/IsCountableTest.php +++ b/test/IsCountableTest.php @@ -12,6 +12,8 @@ use function json_encode; +use const JSON_THROW_ON_ERROR; + class IsCountableTest extends TestCase { /** @@ -66,7 +68,7 @@ public function testArrayIsValid(): void 'max' => 10, ]); - $this->assertTrue($sut->isValid(['Foo']), json_encode($sut->getMessages())); + $this->assertTrue($sut->isValid(['Foo']), json_encode($sut->getMessages(), JSON_THROW_ON_ERROR)); $this->assertCount(0, $sut->getMessages()); } @@ -74,7 +76,7 @@ public function testIteratorIsValid(): void { $sut = new IsCountable(); - $this->assertTrue($sut->isValid(new SplQueue()), json_encode($sut->getMessages())); + $this->assertTrue($sut->isValid(new SplQueue()), json_encode($sut->getMessages(), JSON_THROW_ON_ERROR)); $this->assertCount(0, $sut->getMessages()); } diff --git a/test/TestAsset/CustomTraversable.php b/test/TestAsset/CustomTraversable.php index 3bcfc932..7ba3356e 100644 --- a/test/TestAsset/CustomTraversable.php +++ b/test/TestAsset/CustomTraversable.php @@ -12,8 +12,7 @@ class CustomTraversable implements Iterator { - /** @var array */ - private $data; + private array $data; public function __construct(array $data) { diff --git a/test/TestAsset/Db/Join.php b/test/TestAsset/Db/Join.php index 3149c179..c685b0a2 100644 --- a/test/TestAsset/Db/Join.php +++ b/test/TestAsset/Db/Join.php @@ -34,10 +34,8 @@ class Join implements Iterator, Countable /** * Current iterator position. - * - * @var int */ - private $position = 0; + private int $position = 0; /** * JOIN specifications @@ -129,7 +127,7 @@ public function join($name, $on, $columns = [Select::SQL_STAR], $type = self::JO 'name' => $name, 'on' => $on, 'columns' => $columns, - 'type' => $type ? $type : self::JOIN_INNER, + 'type' => $type ?: self::JOIN_INNER, ]; return $this; diff --git a/test/TestAsset/Uri/Uri.php b/test/TestAsset/Uri/Uri.php index c6f7f812..9a02362e 100644 --- a/test/TestAsset/Uri/Uri.php +++ b/test/TestAsset/Uri/Uri.php @@ -1016,9 +1016,7 @@ public static function encodeUserInfo($userInfo) $regex = '/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:]|%(?![A-Fa-f0-9]{2}))/'; $escaper = static::getEscaper(); - $replace = function ($match) use ($escaper) { - return $escaper->escapeUrl($match[0]); - }; + $replace = static fn($match) => $escaper->escapeUrl($match[0]); return preg_replace_callback($regex, $replace, $userInfo); } @@ -1044,9 +1042,7 @@ public static function encodePath($path) $regex = '/(?:[^' . self::CHAR_UNRESERVED . ')(:@&=\+\$,\/;%]+|%(?![A-Fa-f0-9]{2}))/'; $escaper = static::getEscaper(); - $replace = function ($match) use ($escaper) { - return $escaper->escapeUrl($match[0]); - }; + $replace = static fn($match) => $escaper->escapeUrl($match[0]); return preg_replace_callback($regex, $replace, $path); } @@ -1073,9 +1069,7 @@ public static function encodeQueryFragment($input) $regex = '/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\/\?]+|%(?![A-Fa-f0-9]{2}))/'; $escaper = static::getEscaper(); - $replace = function ($match) use ($escaper) { - return $escaper->escapeUrl($match[0]); - }; + $replace = static fn($match) => $escaper->escapeUrl($match[0]); return preg_replace_callback($regex, $replace, $input); } @@ -1387,7 +1381,7 @@ protected static function normalizeFragment($fragment) */ protected static function decodeUrlEncodedChars($input, $allowed = '') { - $decodeCb = function ($match) use ($allowed) { + $decodeCb = static function ($match) use ($allowed) { $char = rawurldecode($match[0]); if (preg_match($allowed, $char)) { return $char; diff --git a/test/UndisclosedPasswordTest.php b/test/UndisclosedPasswordTest.php index 85384149..35a5303e 100644 --- a/test/UndisclosedPasswordTest.php +++ b/test/UndisclosedPasswordTest.php @@ -7,16 +7,17 @@ use Exception; use Laminas\Validator\UndisclosedPassword; use LaminasTest\Validator\TestAsset\HttpClientException; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Http\Client\ClientExceptionInterface; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; -use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use ReflectionClass; use stdClass; -use function rand; +use function assert; +use function random_int; use function sha1; use function sprintf; use function strtoupper; @@ -24,17 +25,13 @@ class UndisclosedPasswordTest extends TestCase { - /** @var ClientInterface */ - private $httpClient; + private ?MockObject $httpClient; - /** @var RequestInterface */ - private $httpRequest; + private MockObject $httpRequest; - /** @var ResponseInterface */ - private $httpResponse; + private MockObject $httpResponse; - /** @var UndisclosedPassword */ - private $validator; + private UndisclosedPassword $validator; /** * @inheritDoc @@ -134,9 +131,12 @@ public function testStrongUnseenPasswordsPassValidation($password): void 'HIBP_K_ANONYMITY_HASH_RANGE_LENGTH', UndisclosedPassword::class ))), - rand(0, 100000) + random_int(0, 100000) ); }); + + assert($this->httpClient instanceof MockObject); + $this->httpClient->method('sendRequest') ->willReturn($this->httpResponse); @@ -163,9 +163,12 @@ public function testBreachedPasswordsDoNotPassValidation($password): void 'HIBP_K_ANONYMITY_HASH_RANGE_LENGTH', UndisclosedPassword::class ))), - rand(0, 100000) + random_int(0, 100000) ); }); + + assert($this->httpClient instanceof MockObject); + $this->httpClient->method('sendRequest') ->willReturn($this->httpResponse); @@ -183,6 +186,8 @@ public function testBreachedPasswordsDoNotPassValidation($password): void */ public function testBreachedPasswordReturnErrorMessages($password): void { + assert($this->httpClient instanceof MockObject); + $this->httpClient->method('sendRequest') ->will($this->throwException(new Exception('foo'))); @@ -213,6 +218,9 @@ public function testValidationDegradesGracefullyWhenNoConnectionCanBeMade($passw { $clientException = $this->getMockBuilder(HttpClientException::class) ->getMock(); + + assert($this->httpClient instanceof MockObject); + $this->httpClient->method('sendRequest') ->will($this->throwException($clientException)); diff --git a/test/UuidTest.php b/test/UuidTest.php index 7b17237e..9b19757e 100644 --- a/test/UuidTest.php +++ b/test/UuidTest.php @@ -13,8 +13,7 @@ */ final class UuidTest extends TestCase { - /** @var Uuid */ - protected $validator; + protected Uuid $validator; protected function setUp(): void { diff --git a/test/ValidatorChainTest.php b/test/ValidatorChainTest.php index cbcbc234..13a0ac62 100644 --- a/test/ValidatorChainTest.php +++ b/test/ValidatorChainTest.php @@ -232,17 +232,13 @@ public function getValidatorFalse() public function testCanAttachMultipleValidatorsOfTheSameTypeAsDiscreteInstances(): void { $this->validator->attachByName('Callback', [ - 'callback' => function ($value) { - return true; - }, + 'callback' => static fn($value) => true, 'messages' => [ 'callbackValue' => 'This should not be seen in the messages', ], ]); $this->validator->attachByName('Callback', [ - 'callback' => function ($value) { - return false; - }, + 'callback' => static fn($value) => false, 'messages' => [ 'callbackValue' => 'Second callback trapped', ], diff --git a/test/ValidatorPluginManagerFactoryTest.php b/test/ValidatorPluginManagerFactoryTest.php index 29f3faf5..81e971ed 100644 --- a/test/ValidatorPluginManagerFactoryTest.php +++ b/test/ValidatorPluginManagerFactoryTest.php @@ -73,9 +73,7 @@ public function testConfiguresValidatorServicesWhenFound(): void 'test' => Digits::class, ], 'factories' => [ - 'test-too' => function ($container) use ($validator): ValidatorInterface { - return $validator; - }, + 'test-too' => static fn($container): ValidatorInterface => $validator, ], ], ]; diff --git a/test/ValidatorPluginManagerTest.php b/test/ValidatorPluginManagerTest.php index bd66b118..be4ae0f5 100644 --- a/test/ValidatorPluginManagerTest.php +++ b/test/ValidatorPluginManagerTest.php @@ -67,9 +67,7 @@ public function testRegisteringInvalidValidatorRaisesException(): void try { /** @psalm-suppress InvalidArgument */ $this->validators->setService('test', $this); - } catch (InvalidServiceException $e) { - $this->assertStringContainsString(ValidatorInterface::class, $e->getMessage()); - } catch (RuntimeException $e) { + } catch (InvalidServiceException | RuntimeException $e) { $this->assertStringContainsString(ValidatorInterface::class, $e->getMessage()); } catch (Exception $e) { $this->fail(sprintf( @@ -84,9 +82,7 @@ public function testLoadingInvalidValidatorRaisesException(): void $this->validators->setInvokableClass('test', static::class); try { $this->validators->get('test'); - } catch (InvalidServiceException $e) { - $this->assertStringContainsString(ValidatorInterface::class, $e->getMessage()); - } catch (RuntimeException $e) { + } catch (InvalidServiceException | RuntimeException $e) { $this->assertStringContainsString(ValidatorInterface::class, $e->getMessage()); } catch (Exception $e) { $this->fail(sprintf(