Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change type of $skipOnEmpty argument in rules' constructors from mixed to bool|callable|null #698

Merged
merged 23 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
490e2a1
Change type of `$skipOnEmpty` argument in rules' constructors from `m…
arogachev Apr 12, 2024
de1aca9
Revert "Revert "Change type of `$escape` argument in `Error::getValue…
arogachev Apr 12, 2024
ae83f36
Merge branch 'master' into 660-change-skip-on-empty-type
vjik Apr 13, 2024
242c014
Merge remote-tracking branch 'origin/master'
arogachev Apr 15, 2024
a05b750
Apply change for remaining rules
arogachev Apr 15, 2024
dc14006
Merge remote-tracking branch 'origin/660-change-skip-on-empty-type' i…
arogachev Apr 15, 2024
da3e51a
Apply fixes from StyleCI
StyleCIBot Apr 15, 2024
edc7e5b
Merge branch 'refs/heads/master' into 660-change-skip-on-empty-type
arogachev Apr 15, 2024
48aaff5
Merge remote-tracking branch 'origin/660-change-skip-on-empty-type' i…
arogachev Apr 15, 2024
52dc5a5
Update src/Rule/Trait/SkipOnEmptyTrait.php
arogachev Apr 29, 2024
b5fdcaa
Fix PHPDoc [skip ci]
arogachev Apr 29, 2024
bf18566
Update src/Rule/Trait/SkipOnEmptyTrait.php
arogachev Apr 29, 2024
71d11ea
More specific type [skip ci]
arogachev Apr 29, 2024
8d586b1
Merge remote-tracking branch 'origin/660-change-skip-on-empty-type' i…
arogachev Apr 29, 2024
90648ba
More review fixes
arogachev Apr 29, 2024
26057c8
Apply fixes from StyleCI
StyleCIBot Apr 29, 2024
cbc1958
Update upgrade instructions [review fix]
arogachev Apr 29, 2024
90e928a
Update UPGRADE.md
vjik Apr 30, 2024
aa68b85
Update UPGRADE.md
vjik Apr 30, 2024
6e0a603
Update UPGRADE.md
vjik Apr 30, 2024
6d89182
Update src/Rule/Trait/SkipOnEmptyTrait.php
vjik Apr 30, 2024
ca1aa71
Update src/Rule/Trait/SkipOnEmptyTrait.php
vjik Apr 30, 2024
a6352a4
Update UPGRADE.md
vjik Apr 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
(@arogachev)
- Chg #679: Change type of `$rule` argument in `RuleHandlerInterface::validate()` from `object` to `RuleInterface`
(@arogachev)
- Chg #660: Change type of `$skipOnEmpty` argument in rules' constructors from `mixed` to `bool|callable|null`
- Chg #613: Change type of `$escape` argument in `Error::getValuePath()` from `bool|string|null` to `string|null`
(@arogachev)

Expand Down
31 changes: 31 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,34 @@ for both A and B.

* The type of `$escape` argument in `Yiisoft\Validator\Error::getValuePath()` changed from `bool|string|null` to
`string|null`. If you used `bool`, replace `false` with `null` and `true` with dot (`.`).

* For custom rules using `Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait`, apply the following changes for `$skipOnEmpty`
property in the constructor:

- Turn in it into argument (remove `private` visibility).
- Change type from `mixed` to more specific `bool|callable|null`
- Add manual initialization of property value.

For example:

```php
public function __construct(
// ...
private mixed $skipOnEmpty = null,
// ...
) {
// ...
}
```

Change to:

```php
public function __construct(
// ...
bool|callable|null $skipOnEmpty = null,
// ...
) {
$this->skipOnEmpty = $skipOnEmpty;
}
```
4 changes: 3 additions & 1 deletion src/Rule/AbstractCompare.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function __construct(
private string|null $message = null,
private string $type = CompareType::NUMBER,
private string $operator = '==',
private mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private Closure|null $when = null,
) {
Expand All @@ -169,6 +169,8 @@ public function __construct(

throw new InvalidArgumentException($message);
}

$this->skipOnEmpty = $skipOnEmpty;
}

public function getName(): string
Expand Down
3 changes: 2 additions & 1 deletion src/Rule/AbstractNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function __construct(
private string $lessThanMinMessage,
private string $greaterThanMaxMessage,
string $pattern,
private mixed $skipOnEmpty,
bool|callable|null $skipOnEmpty,
private bool $skipOnError,
private Closure|null $when,
) {
Expand All @@ -113,6 +113,7 @@ public function __construct(
}

$this->pattern = $pattern;
$this->skipOnEmpty = $skipOnEmpty;
}

public function getName(): string
Expand Down
4 changes: 3 additions & 1 deletion src/Rule/AtLeast.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ public function __construct(
private string $incorrectInputMessage = '{Attribute} must be an array or an object.',
private string $message = 'At least {min, number} {min, plural, one{attribute} other{attributes}} from this ' .
'list must be filled: {attributes}.',
private mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private Closure|null $when = null
) {
if ($min > count($this->attributes)) {
throw new InvalidArgumentException('$min must be no greater than amount of $attributes.');
}

$this->skipOnEmpty = $skipOnEmpty;
}

public function getName(): string
Expand Down
3 changes: 2 additions & 1 deletion src/Rule/BooleanValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ public function __construct(
private bool $strict = false,
private string $incorrectInputMessage = 'The allowed types are integer, float, string, boolean. {type} given.',
private string $message = '{Attribute} must be either "{true}" or "{false}".',
private mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private Closure|null $when = null,
) {
$this->skipOnEmpty = $skipOnEmpty;
}

public function getName(): string
Expand Down
4 changes: 3 additions & 1 deletion src/Rule/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ final class Callback implements
public function __construct(
private mixed $callback = null,
private string|null $method = null,
private mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private Closure|null $when = null,
) {
Expand All @@ -69,6 +69,8 @@ public function __construct(
if ($this->callback !== null && $this->method !== null) {
throw new InvalidArgumentException('"$callback" and "$method" are mutually exclusive.');
}

$this->skipOnEmpty = $skipOnEmpty;
}

public function getName(): string
Expand Down
9 changes: 1 addition & 8 deletions src/Rule/Composite.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,6 @@ class Composite implements
*/
protected iterable $rules = [];

/**
* @var bool|callable|null Whether to skip this rule group if the validated value is empty / not passed. See
* {@see SkipOnEmptyInterface}.
* @psalm-var SkipOnEmptyValue
*/
private mixed $skipOnEmpty = null;

/**
* @var bool Whether to skip this rule group if any of the previous rules gave an error. See
* {@see SkipOnErrorInterface}.
Expand Down Expand Up @@ -133,8 +126,8 @@ public function __construct(
bool $skipOnError = false,
Closure|null $when = null,
) {
$this->rules = RulesNormalizer::normalizeList($rules);
$this->skipOnEmpty = $skipOnEmpty;
$this->rules = RulesNormalizer::normalizeList($rules);
$this->skipOnError = $skipOnError;
$this->when = $when;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Rule/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function __construct(
'other{items}}.',
string $notExactlyMessage = '{Attribute} must contain exactly {exactly, number} {exactly, plural, one{item} ' .
'other{items}}.',
private mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private Closure|null $when = null,
) {
Expand All @@ -107,6 +107,7 @@ public function __construct(
$greaterThanMaxMessage,
$notExactlyMessage
);
$this->skipOnEmpty = $skipOnEmpty;
}

public function getName(): string
Expand Down
3 changes: 2 additions & 1 deletion src/Rule/Date/BaseDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ public function __construct(
private ?string $incorrectInputMessage,
private ?string $tooEarlyMessage,
private ?string $tooLateMessage,
private mixed $skipOnEmpty,
bool|callable|null $skipOnEmpty,
private bool $skipOnError,
private Closure|null $when,
) {
$this->skipOnEmpty = $skipOnEmpty;
}

public function getFormat(): ?string
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Date/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function __construct(
?string $incorrectInputMessage = null,
?string $tooEarlyMessage = null,
?string $tooLateMessage = null,
mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
bool $skipOnError = false,
Closure|null $when = null,
) {
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Date/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function __construct(
?string $incorrectInputMessage = null,
?string $tooEarlyMessage = null,
?string $tooLateMessage = null,
mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
bool $skipOnError = false,
Closure|null $when = null,
) {
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Date/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function __construct(
?string $incorrectInputMessage = null,
?string $tooEarlyMessage = null,
?string $tooLateMessage = null,
mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
bool $skipOnError = false,
Closure|null $when = null,
) {
Expand Down
3 changes: 2 additions & 1 deletion src/Rule/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function __construct(
string $format = 'Y-m-d',
private string $incorrectInputMessage = '{Attribute} must be a date.',
private string $message = '{Attribute} is not a valid date.',
private mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private ?Closure $when = null,
) {
Expand All @@ -99,6 +99,7 @@ public function __construct(
}

$this->format = $format;
$this->skipOnEmpty = $skipOnEmpty;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Rule/Each.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ public function __construct(
callable|iterable|object|string $rules = [],
private string $incorrectInputMessage = '{Attribute} must be array or iterable.',
private string $incorrectInputKeyMessage = 'Every iterable key must have an integer or a string type.',
private mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private Closure|null $when = null,
) {
$this->rules = RulesNormalizer::normalize($rules);
$this->skipOnEmpty = $skipOnEmpty;
}

public function getName(): string
Expand Down
4 changes: 3 additions & 1 deletion src/Rule/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function __construct(
private bool $enableIdn = false,
private string $incorrectInputMessage = '{Attribute} must be a string.',
private string $message = '{Attribute} is not a valid email address.',
private mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private Closure|null $when = null,
) {
Expand Down Expand Up @@ -124,6 +124,8 @@ public function __construct(
throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.');
// @codeCoverageIgnoreEnd
}

$this->skipOnEmpty = $skipOnEmpty;
}

public function getName(): string
Expand Down
4 changes: 3 additions & 1 deletion src/Rule/Image/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function __construct(
private string $tooLargeWidthMessage = 'The width cannot be larger than {limit, number} {limit, plural, one{pixel} other{pixels}}.',
private string $tooLargeHeightMessage = 'The height cannot be larger than {limit, number} {limit, plural, one{pixel} other{pixels}}.',
private string $invalidAspectRatioMessage = 'The aspect ratio must be {aspectRatioWidth, number}:{aspectRatioHeight, number} with margin {aspectRatioMargin, number}%.',
private mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private Closure|null $when = null,
) {
Expand All @@ -141,6 +141,8 @@ public function __construct(
if ($this->height !== null && ($this->minHeight !== null || $this->maxHeight !== null)) {
throw new InvalidArgumentException('Exact width and min / max height can\'t be specified together.');
}

$this->skipOnEmpty = $skipOnEmpty;
}

public function getWidth(): ?int
Expand Down
3 changes: 2 additions & 1 deletion src/Rule/In.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ public function __construct(
private bool $strict = false,
private bool $not = false,
private string $message = '{Attribute} is not in the list of acceptable values.',
private mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private Closure|null $when = null,
) {
$this->skipOnEmpty = $skipOnEmpty;
}

public function getName(): string
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Integer.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function __construct(
string $lessThanMinMessage = self::DEFAULT_LESS_THAN_MIN_MESSAGE,
string $greaterThanMaxMessage = self::DEFAULT_GREATER_THAN_MAX_MESSAGE,
string $pattern = '/^\s*[+-]?\d+\s*$/',
mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
bool $skipOnError = false,
Closure|null $when = null,
) {
Expand Down
3 changes: 2 additions & 1 deletion src/Rule/Ip.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public function __construct(
private string $hasSubnetMessage = '{Attribute} must not be a subnet.',
private string $notInRangeMessage = '{Attribute} is not in the allowed range.',
private array $ranges = [],
private mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private Closure|null $when = null,
) {
Expand All @@ -217,6 +217,7 @@ public function __construct(
}

$this->ranges = $this->prepareRanges($ranges);
$this->skipOnEmpty = $skipOnEmpty;
}

public function getName(): string
Expand Down
3 changes: 2 additions & 1 deletion src/Rule/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ final class Json implements DumpedRuleInterface, SkipOnErrorInterface, WhenInter
public function __construct(
private string $incorrectInputMessage = '{Attribute} must be a string.',
private string $message = '{Attribute} is not JSON.',
private mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private Closure|null $when = null,
) {
$this->skipOnEmpty = $skipOnEmpty;
}

public function getName(): string
Expand Down
3 changes: 2 additions & 1 deletion src/Rule/Length.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function __construct(
string $notExactlyMessage = '{Attribute} must contain exactly {exactly, number} {exactly, plural, ' .
'one{character} other{characters}}.',
private string $encoding = 'UTF-8',
private mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private Closure|null $when = null
) {
Expand All @@ -109,6 +109,7 @@ public function __construct(
$greaterThanMaxMessage,
$notExactlyMessage
);
$this->skipOnEmpty = $skipOnEmpty;
}

public function getName(): string
Expand Down
3 changes: 2 additions & 1 deletion src/Rule/Nested.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,11 @@ public function __construct(
private string $noPropertyPathMessage = 'Property "{path}" is not found.',
private bool $handleEachShortcut = true,
private bool $propagateOptions = false,
private mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private Closure|null $when = null,
) {
$this->skipOnEmpty = $skipOnEmpty;
$this->prepareRules($rules);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function __construct(
string $lessThanMinMessage = '{Attribute} must be no less than {min}.',
string $greaterThanMaxMessage = '{Attribute} must be no greater than {max}.',
string $pattern = '/^\s*[-+]?\d*\.?\d+([eE][-+]?\d+)?\s*$/',
mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
bool $skipOnError = false,
Closure|null $when = null,
) {
Expand Down
3 changes: 2 additions & 1 deletion src/Rule/OneOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ public function __construct(
private array $attributes,
private string $incorrectInputMessage = '{Attribute} must be an array or an object.',
private string $message = 'Exactly 1 attribute from this list must be filled: {attributes}.',
private mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private Closure|null $when = null
) {
$this->skipOnEmpty = $skipOnEmpty;
}

public function getName(): string
Expand Down
3 changes: 2 additions & 1 deletion src/Rule/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function __construct(
private bool $not = false,
private string $incorrectInputMessage = '{Attribute} must be a string.',
private string $message = '{Attribute} is invalid.',
private mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private Closure|null $when = null,
) {
Expand All @@ -80,6 +80,7 @@ public function __construct(
}

$this->pattern = $pattern;
$this->skipOnEmpty = $skipOnEmpty;
}

public function getName(): string
Expand Down
Loading
Loading