Skip to content

Commit

Permalink
Merge pull request #144 from samsonasik/apply-php74
Browse files Browse the repository at this point in the history
Apply PHP 7.4 syntax and typed property
  • Loading branch information
Ocramius committed Jul 27, 2022
2 parents 7baf6c1 + 08347af commit 6d61b6c
Show file tree
Hide file tree
Showing 24 changed files with 68 additions and 87 deletions.
4 changes: 1 addition & 3 deletions src/Between.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/CreditCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/DateStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/File/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Laminas\Validator\File;

use Countable;
use Laminas\Validator\AbstractValidator;
use Laminas\Validator\Exception;
use Psr\Http\Message\UploadedFileInterface;
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/IsCountable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 2 additions & 6 deletions src/UndisclosedPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions test/BetweenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
11 changes: 5 additions & 6 deletions test/CallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand All @@ -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));
}
Expand Down
6 changes: 3 additions & 3 deletions test/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/ExplodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}),
Expand Down
8 changes: 4 additions & 4 deletions test/File/CountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/File/FilesSizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions test/File/MimeTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

use function basename;
use function current;
use function dirname;
use function extension_loaded;
use function getenv;
use function is_array;
Expand Down Expand Up @@ -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,
];
Expand Down
2 changes: 1 addition & 1 deletion test/File/SizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions test/File/WordCountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions test/IsCountableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

use function json_encode;

use const JSON_THROW_ON_ERROR;

class IsCountableTest extends TestCase
{
/**
Expand Down Expand Up @@ -66,15 +68,15 @@ 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());
}

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());
}

Expand Down
3 changes: 1 addition & 2 deletions test/TestAsset/CustomTraversable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

class CustomTraversable implements Iterator
{
/** @var array */
private $data;
private array $data;

public function __construct(array $data)
{
Expand Down
6 changes: 2 additions & 4 deletions test/TestAsset/Db/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ class Join implements Iterator, Countable

/**
* Current iterator position.
*
* @var int
*/
private $position = 0;
private int $position = 0;

/**
* JOIN specifications
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 4 additions & 10 deletions test/TestAsset/Uri/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 6d61b6c

Please sign in to comment.