Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ composer.phar

# PHP CS Fixer
/.php-cs-fixer.cache
/.php-cs-fixer.php

# po4a .mo files
*.mo
13 changes: 13 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,29 @@
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;

$finder = (new Finder())->in([
__DIR__ . '/config',
__DIR__ . '/src',
__DIR__ . '/tests',
]);

return (new Config())
->setRiskyAllowed(true)
->setParallelConfig(ParallelConfigFactory::detect())
->setRules([
'@PER-CS3.0' => true,
'no_unused_imports' => true,
'ordered_class_elements' => true,
'class_attributes_separation' => ['elements' => ['method' => 'one']],
'declare_strict_types' => true,
'native_function_invocation' => true,
'native_constant_invocation' => true,
'fully_qualified_strict_types' => [
'import_symbols' => true
],
'global_namespace_import' => [
'import_classes' => true,
'import_constants' => true,
'import_functions' => true,
],
])
->setFinder($finder);
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 2.5.2 under development

- no changes in this release.
- Enh #787: Explicitly import classes, functions, and constants in "use" section (@mspirkov)

## 2.5.1 December 12, 2025

Expand Down
4 changes: 2 additions & 2 deletions config/di.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
'yii.validator.categorySource' => [
'definition' => static function () use ($params): CategorySource {
$reader = class_exists(MessageSource::class)
? new MessageSource(dirname(__DIR__) . '/messages')
? new MessageSource(\dirname(__DIR__) . '/messages')
: new IdMessageReader(); // @codeCoverageIgnore

$formatter = extension_loaded('intl')
$formatter = \extension_loaded('intl')
? new IntlMessageFormatter()
: new SimpleMessageFormatter();

Expand Down
2 changes: 2 additions & 0 deletions src/Debug/ValidatorCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Yiisoft\Yii\Debug\Collector\CollectorTrait;
use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface;

use function count;

final class ValidatorCollector implements SummaryCollectorInterface
{
use CollectorTrait;
Expand All @@ -26,7 +28,7 @@
return;
}

if ($rules instanceof Traversable) {

Check warning on line 31 in src/Debug/ValidatorCollector.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "InstanceOf_": @@ @@ if (!$this->isActive()) { return; } - if ($rules instanceof Traversable) { + if (!$rules instanceof Traversable) { $rules = iterator_to_array($rules); } $this->validations[] = ['value' => $value, 'rules' => $rules, 'result' => $result->isValid(), 'errors' => $result->getErrors()];
$rules = iterator_to_array($rules);
}

Expand All @@ -46,7 +48,7 @@
}

$count = count($this->validations);
$countValid = count(array_filter($this->validations, fn(array $data): bool => (bool) $data['result']));

Check warning on line 51 in src/Debug/ValidatorCollector.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "CastBool": @@ @@ return []; } $count = count($this->validations); - $countValid = count(array_filter($this->validations, fn(array $data): bool => (bool) $data['result'])); + $countValid = count(array_filter($this->validations, fn(array $data): bool => $data['result'])); $countInvalid = $count - $countValid; return ['total' => $count, 'valid' => $countValid, 'invalid' => $countInvalid]; }
$countInvalid = $count - $countValid;

return [
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/InvalidCallbackReturnTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Yiisoft\Validator\Rule\Callback;
use Yiisoft\Validator\Rule\CallbackHandler;

use function sprintf;

/**
* An exception used by the handler ({@see CallbackHandler}) of {@see Callback} rule for the cases when returned value
* is not a {@see Result} instance.
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/RuleHandlerInterfaceNotImplementedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Yiisoft\Validator\RuleHandlerInterface;
use Yiisoft\Validator\RuleHandlerResolverInterface;

use function sprintf;

/**
* An exception used by {@see RuleHandlerResolverInterface} implementations (e. g., {@see RuleHandlerContainer}) for
* the case when a retrieved value is not an object or an object that does not implement {@see RuleHandlerInterface}.
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/RuleHandlerNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Throwable;
use Yiisoft\Validator\RuleHandlerResolver\RuleHandlerContainer;

use function sprintf;

/**
* An exception used by {@see RuleHandlerContainer} for the case when a given class name was not found in the container.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/UnexpectedRuleException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use InvalidArgumentException;
use Throwable;

use function sprintf;

/**
* An exception used by rule handlers to guarantee that passed rule have desired type. Every handler's validation code
* must start with this check. An example for `MyRule` and `MyRuleHandler`:
Expand Down
1 change: 1 addition & 0 deletions src/Helper/ObjectParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use function is_int;
use function is_object;
use function is_string;
use function sprintf;

/**
* A helper class used to parse rules from PHP attributes (attached to class properties and class itself) and data from
Expand Down Expand Up @@ -320,7 +321,7 @@
$data = [];
foreach ($this->getReflectionProperties() as $name => $property) {
if (!$property->isInitialized($this->source)) {
continue;

Check warning on line 324 in src/Helper/ObjectParser.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "Continue_": @@ @@ $data = []; foreach ($this->getReflectionProperties() as $name => $property) { if (!$property->isInitialized($this->source)) { - continue; + break; } /** @var mixed */ $data[$name] = $property->getValue($this->source);
}
/** @var mixed */
$data[$name] = $property->getValue($this->source);
Expand Down Expand Up @@ -381,7 +382,7 @@
{
if ($this->hasCacheItem('reflectionSource')) {
/** @var ReflectionClass|ReflectionObject */
return $this->getCacheItem('reflectionSource');

Check warning on line 385 in src/Helper/ObjectParser.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ private function getReflectionSource(): ReflectionObject|ReflectionClass { if ($this->hasCacheItem('reflectionSource')) { - /** @var ReflectionClass|ReflectionObject */ - return $this->getCacheItem('reflectionSource'); + } $reflectionSource = is_object($this->source) ? new ReflectionObject($this->source) : new ReflectionClass($this->source); $this->setCacheItem('reflectionSource', $reflectionSource);
}

$reflectionSource = is_object($this->source)
Expand Down Expand Up @@ -451,7 +452,7 @@
string $name,
): bool {
if (!$this->useCache()) {
return false;

Check warning on line 455 in src/Helper/ObjectParser.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ ): bool { if (!$this->useCache()) { - return false; + } if (!array_key_exists($this->cacheKey, self::$cache)) { return false;
}

if (!array_key_exists($this->cacheKey, self::$cache)) {
Expand Down Expand Up @@ -488,7 +489,7 @@
mixed $value,
): void {
if (!$this->useCache()) {
return;

Check warning on line 492 in src/Helper/ObjectParser.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ ): void { if (!$this->useCache()) { - return; + } /** @psalm-suppress PossiblyNullArrayOffset, MixedAssignment */ self::$cache[$this->cacheKey][$name] = $value;
}

/** @psalm-suppress PossiblyNullArrayOffset, MixedAssignment */
Expand Down
1 change: 1 addition & 0 deletions src/Helper/RulesDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use function is_int;
use function is_string;
use function sprintf;

/**
* RulesDumper allows to get an array of rule names and corresponding settings from a set of rules.
Expand Down
2 changes: 2 additions & 0 deletions src/Helper/RulesNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use function is_callable;
use function is_int;
use function is_string;
use function is_object;
use function sprintf;

/**
* A helper class used to normalize different types of data to the iterable with rule instances ({@see RuleInterface}).
Expand Down
1 change: 1 addition & 0 deletions src/Helper/RulesNormalizerIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Yiisoft\Validator\SkipOnEmptyInterface;

use function is_callable;
use function sprintf;

/**
* An iterator for a set of rules, performs normalization for every individual rule unifying other provided types and
Expand Down
1 change: 1 addition & 0 deletions src/Rule/AbstractCompare.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Yiisoft\Validator\WhenInterface;

use function in_array;
use function is_scalar;

/**
* Abstract base for all the comparison validation rules.
Expand Down
2 changes: 2 additions & 0 deletions src/Rule/BooleanValueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Yiisoft\Validator\RuleInterface;
use Yiisoft\Validator\ValidationContext;

use function is_scalar;

/**
* A handler for {@see BooleanValue} rule.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Rule/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Yiisoft\Validator\SkipOnErrorInterface;
use Yiisoft\Validator\WhenInterface;

use function sprintf;

/**
* Defines validation options to validating the value using a callback.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Rule/CompareHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use Yiisoft\Validator\ValidationContext;

use function gettype;
use function is_scalar;

use const PHP_FLOAT_EPSILON;

/**
* Compares the specified value with "target" value provided directly or within a property.
Expand Down
4 changes: 2 additions & 2 deletions src/Rule/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ final class Count implements
* @param int|null $max Maximum number of items. null means no maximum number limit. Can't be combined with
* {@see $exactly}. See {@see $greaterThanMaxMessage} for the customized message for a value with too many items.
* @param string $incorrectInputMessage Error message used when the value is neither an array nor an object
* implementing {@see \Countable} interface.
* implementing {@see Countable} interface.
*
* You may use the following placeholders in the message:
*
Expand Down Expand Up @@ -117,7 +117,7 @@ public function getName(): string
}

/**
* Get error message used when the value is neither an array nor an object implementing {@see \Countable} interface.
* Get error message used when the value is neither an array nor an object implementing {@see Countable} interface.
*
* @return string Error message.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Rule/Date/BaseDateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use Yiisoft\Validator\RuleInterface;
use Yiisoft\Validator\ValidationContext;

use function is_int;
use function is_string;

/**
* @see Date
* @see DateTime
Expand Down Expand Up @@ -64,7 +67,7 @@
'Property' => $context->getCapitalizedTranslatedProperty(),
],
);
return $result;

Check warning on line 70 in src/Rule/Date/BaseDateHandler.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ $date = $this->prepareValue($value, $rule, $timeZone, false); if ($date === null) { $result->addError($rule->getIncorrectInputMessage() ?? $this->incorrectInputMessage, ['property' => $context->getTranslatedProperty(), 'Property' => $context->getCapitalizedTranslatedProperty()]); - return $result; + } $min = $this->prepareValue($rule->getMin(), $rule, $timeZone, true); if ($min !== null && $date < $min) {
}

$min = $this->prepareValue($rule->getMin(), $rule, $timeZone, true);
Expand All @@ -78,7 +81,7 @@
'limit' => $this->formatDate($min, $rule, $timeZone),
],
);
return $result;

Check warning on line 84 in src/Rule/Date/BaseDateHandler.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ $min = $this->prepareValue($rule->getMin(), $rule, $timeZone, true); if ($min !== null && $date < $min) { $result->addError($rule->getTooEarlyMessage() ?? $this->tooEarlyMessage, ['property' => $context->getTranslatedProperty(), 'Property' => $context->getCapitalizedTranslatedProperty(), 'value' => $this->formatDate($date, $rule, $timeZone), 'limit' => $this->formatDate($min, $rule, $timeZone)]); - return $result; + } $max = $this->prepareValue($rule->getMax(), $rule, $timeZone, true); if ($max !== null && $date > $max) {
}

$max = $this->prepareValue($rule->getMax(), $rule, $timeZone, true);
Expand Down
2 changes: 2 additions & 0 deletions src/Rule/Image/ImageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use function is_string;

use const UPLOAD_ERR_OK;

/**
* Validates that a value is an image and optionally checks its dimensions.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Rule/InEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Yiisoft\Validator\SkipOnErrorInterface;
use Yiisoft\Validator\WhenInterface;

use function sprintf;

/**
* Defines validation options to check that the value is one of the values (or names) contained in an enum of the
* specified class.
Expand Down
2 changes: 2 additions & 0 deletions src/Rule/JsonHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use function function_exists;
use function is_string;

use const JSON_ERROR_NONE;

/**
* A handler for {@see Json} rule. Validates that the value is a valid JSON string.
*
Expand Down
1 change: 1 addition & 0 deletions src/Rule/Nested.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use function ltrim;
use function rtrim;
use function sprintf;
use function is_callable;

/**
* Used to define rules for validation of nested structures:
Expand Down
1 change: 1 addition & 0 deletions src/Rule/NumberHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Yiisoft\Validator\ValidationContext;

use function is_bool;
use function is_scalar;

/**
* Validates that the value is a number.
Expand Down
2 changes: 2 additions & 0 deletions src/Rule/TrueValueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Yiisoft\Validator\RuleInterface;
use Yiisoft\Validator\ValidationContext;

use function is_scalar;

/**
* A handler for {@see TrueValue} rule.
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Rule/UniqueIterableHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

use function count;
use function gettype;
use function is_scalar;

use const SORT_REGULAR;

/**
* A handler for {@see UniqueIterable} rule. Validates uniqueness of each element of an iterable.
Expand Down
2 changes: 2 additions & 0 deletions tests/DataSet/ObjectDataSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
use Yiisoft\Validator\Tests\Support\Data\Charts\Chart;
use Yiisoft\Validator\Validator;

use function sprintf;

final class ObjectDataSetTest extends TestCase
{
public static function propertyVisibilityDataProvider(): array
Expand Down
4 changes: 4 additions & 0 deletions tests/Rule/CompareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
use Yiisoft\Validator\Tests\Support\Data\CompareObject;

use function is_string;
use function sprintf;

use const PHP_FLOAT_EPSILON;
use const PHP_INT_MAX;

final class CompareTest extends RuleTestCase
{
Expand Down
3 changes: 3 additions & 0 deletions tests/Rule/Image/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait;
use Yiisoft\Validator\Validator;

use const UPLOAD_ERR_NO_FILE;
use const UPLOAD_ERR_OK;

final class ImageTest extends RuleTestCase
{
use DifferentRuleInHandlerTestTrait;
Expand Down
4 changes: 3 additions & 1 deletion tests/Rule/Nested/NestedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@
use Yiisoft\Validator\Tests\Support\RulesProvider\SimpleRulesProvider;
use Yiisoft\Validator\ValidationContext;
use Yiisoft\Validator\Validator;
use ArrayIterator;

use function array_slice;
use function in_array;

final class NestedTest extends RuleTestCase
{
Expand Down Expand Up @@ -1538,7 +1540,7 @@ public function testWithNonArrayButIterableOfObjects(): void
#[Each(new Nested())]
public iterable $collection;
};
$collection = new \ArrayIterator([$classA]);
$collection = new ArrayIterator([$classA]);
$classB->collection = $collection;
$result = (new Validator())->validate($classB);

Expand Down
2 changes: 2 additions & 0 deletions tests/Rule/NumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait;
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait;

use const PHP_INT_MAX;

final class NumberTest extends RuleTestCase
{
use RuleWithOptionsTestTrait;
Expand Down
3 changes: 2 additions & 1 deletion tests/Support/Data/IteratorWithBooleanKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
use ReturnTypeWillChange;
use Yiisoft\Validator\Rule\Length;
use Yiisoft\Validator\RuleInterface;
use Iterator;

class IteratorWithBooleanKey implements \Iterator
class IteratorWithBooleanKey implements Iterator
{
private int $position = 0;
private array $array;
Expand Down
3 changes: 3 additions & 0 deletions tests/Support/Rule/PiHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

use function abs;

use const M_PI;
use const PHP_FLOAT_EPSILON;

final class PiHandler implements RuleHandlerInterface
{
public function validate(mixed $value, RuleInterface $rule, ValidationContext $context): Result
Expand Down
Loading