diff --git a/.gitignore b/.gitignore index 17dcf1d2c..eca67cb16 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,7 @@ composer.phar # PHP CS Fixer /.php-cs-fixer.cache +/.php-cs-fixer.php # po4a .mo files *.mo diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index c8091220b..7571f3048 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -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); diff --git a/CHANGELOG.md b/CHANGELOG.md index bf4f07aeb..983283a7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/config/di.php b/config/di.php index ccaa6ee6f..7ec3d6575 100644 --- a/config/di.php +++ b/config/di.php @@ -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(); diff --git a/src/Debug/ValidatorCollector.php b/src/Debug/ValidatorCollector.php index af4b3febd..303e787c2 100644 --- a/src/Debug/ValidatorCollector.php +++ b/src/Debug/ValidatorCollector.php @@ -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; diff --git a/src/Exception/InvalidCallbackReturnTypeException.php b/src/Exception/InvalidCallbackReturnTypeException.php index fc90a26f6..bedd4056b 100644 --- a/src/Exception/InvalidCallbackReturnTypeException.php +++ b/src/Exception/InvalidCallbackReturnTypeException.php @@ -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. diff --git a/src/Exception/RuleHandlerInterfaceNotImplementedException.php b/src/Exception/RuleHandlerInterfaceNotImplementedException.php index 2f843c6d7..9c25a751a 100644 --- a/src/Exception/RuleHandlerInterfaceNotImplementedException.php +++ b/src/Exception/RuleHandlerInterfaceNotImplementedException.php @@ -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}. diff --git a/src/Exception/RuleHandlerNotFoundException.php b/src/Exception/RuleHandlerNotFoundException.php index 6013c24a1..3d7e114c5 100644 --- a/src/Exception/RuleHandlerNotFoundException.php +++ b/src/Exception/RuleHandlerNotFoundException.php @@ -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. */ diff --git a/src/Exception/UnexpectedRuleException.php b/src/Exception/UnexpectedRuleException.php index ba1300a78..88ed3be7a 100644 --- a/src/Exception/UnexpectedRuleException.php +++ b/src/Exception/UnexpectedRuleException.php @@ -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`: diff --git a/src/Helper/ObjectParser.php b/src/Helper/ObjectParser.php index 15d778bad..cf841c9f8 100644 --- a/src/Helper/ObjectParser.php +++ b/src/Helper/ObjectParser.php @@ -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 diff --git a/src/Helper/RulesDumper.php b/src/Helper/RulesDumper.php index df534cbdd..af9c649ba 100644 --- a/src/Helper/RulesDumper.php +++ b/src/Helper/RulesDumper.php @@ -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. diff --git a/src/Helper/RulesNormalizer.php b/src/Helper/RulesNormalizer.php index 966765434..5c79c3b0f 100644 --- a/src/Helper/RulesNormalizer.php +++ b/src/Helper/RulesNormalizer.php @@ -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}). diff --git a/src/Helper/RulesNormalizerIterator.php b/src/Helper/RulesNormalizerIterator.php index c4d2324ac..678e6c6e7 100644 --- a/src/Helper/RulesNormalizerIterator.php +++ b/src/Helper/RulesNormalizerIterator.php @@ -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 diff --git a/src/Rule/AbstractCompare.php b/src/Rule/AbstractCompare.php index 037e96b6c..7fdbcf6e3 100644 --- a/src/Rule/AbstractCompare.php +++ b/src/Rule/AbstractCompare.php @@ -17,6 +17,7 @@ use Yiisoft\Validator\WhenInterface; use function in_array; +use function is_scalar; /** * Abstract base for all the comparison validation rules. diff --git a/src/Rule/BooleanValueHandler.php b/src/Rule/BooleanValueHandler.php index bd23a92e2..c0735ebfa 100644 --- a/src/Rule/BooleanValueHandler.php +++ b/src/Rule/BooleanValueHandler.php @@ -10,6 +10,8 @@ use Yiisoft\Validator\RuleInterface; use Yiisoft\Validator\ValidationContext; +use function is_scalar; + /** * A handler for {@see BooleanValue} rule. */ diff --git a/src/Rule/Callback.php b/src/Rule/Callback.php index 28857e79c..ee1d8ac39 100644 --- a/src/Rule/Callback.php +++ b/src/Rule/Callback.php @@ -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. * diff --git a/src/Rule/CompareHandler.php b/src/Rule/CompareHandler.php index 11559668b..a010f582d 100644 --- a/src/Rule/CompareHandler.php +++ b/src/Rule/CompareHandler.php @@ -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. diff --git a/src/Rule/Count.php b/src/Rule/Count.php index 57bd440ab..93bdcb9bd 100644 --- a/src/Rule/Count.php +++ b/src/Rule/Count.php @@ -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: * @@ -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. * diff --git a/src/Rule/Date/BaseDateHandler.php b/src/Rule/Date/BaseDateHandler.php index f8ad75e56..03dc33dea 100644 --- a/src/Rule/Date/BaseDateHandler.php +++ b/src/Rule/Date/BaseDateHandler.php @@ -15,6 +15,9 @@ use Yiisoft\Validator\RuleInterface; use Yiisoft\Validator\ValidationContext; +use function is_int; +use function is_string; + /** * @see Date * @see DateTime diff --git a/src/Rule/Image/ImageHandler.php b/src/Rule/Image/ImageHandler.php index a82251bc7..6248f1777 100644 --- a/src/Rule/Image/ImageHandler.php +++ b/src/Rule/Image/ImageHandler.php @@ -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. * diff --git a/src/Rule/InEnum.php b/src/Rule/InEnum.php index 993edeae4..1577782c3 100644 --- a/src/Rule/InEnum.php +++ b/src/Rule/InEnum.php @@ -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. diff --git a/src/Rule/JsonHandler.php b/src/Rule/JsonHandler.php index 354339830..6e3c880a5 100644 --- a/src/Rule/JsonHandler.php +++ b/src/Rule/JsonHandler.php @@ -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. * diff --git a/src/Rule/Nested.php b/src/Rule/Nested.php index e72c4b33a..bfe62aefa 100644 --- a/src/Rule/Nested.php +++ b/src/Rule/Nested.php @@ -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: diff --git a/src/Rule/NumberHandler.php b/src/Rule/NumberHandler.php index e83ecd72c..38399d94f 100644 --- a/src/Rule/NumberHandler.php +++ b/src/Rule/NumberHandler.php @@ -12,6 +12,7 @@ use Yiisoft\Validator\ValidationContext; use function is_bool; +use function is_scalar; /** * Validates that the value is a number. diff --git a/src/Rule/TrueValueHandler.php b/src/Rule/TrueValueHandler.php index b3bde212b..274dc1003 100644 --- a/src/Rule/TrueValueHandler.php +++ b/src/Rule/TrueValueHandler.php @@ -10,6 +10,8 @@ use Yiisoft\Validator\RuleInterface; use Yiisoft\Validator\ValidationContext; +use function is_scalar; + /** * A handler for {@see TrueValue} rule. */ diff --git a/src/Rule/UniqueIterableHandler.php b/src/Rule/UniqueIterableHandler.php index 929524088..a06cb7a32 100644 --- a/src/Rule/UniqueIterableHandler.php +++ b/src/Rule/UniqueIterableHandler.php @@ -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. diff --git a/tests/DataSet/ObjectDataSetTest.php b/tests/DataSet/ObjectDataSetTest.php index f648158b0..91c4076fa 100644 --- a/tests/DataSet/ObjectDataSetTest.php +++ b/tests/DataSet/ObjectDataSetTest.php @@ -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 diff --git a/tests/Rule/CompareTest.php b/tests/Rule/CompareTest.php index d86dd2135..bc501f23c 100644 --- a/tests/Rule/CompareTest.php +++ b/tests/Rule/CompareTest.php @@ -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 { diff --git a/tests/Rule/Image/ImageTest.php b/tests/Rule/Image/ImageTest.php index c094bea21..1a93eff25 100644 --- a/tests/Rule/Image/ImageTest.php +++ b/tests/Rule/Image/ImageTest.php @@ -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; diff --git a/tests/Rule/Nested/NestedTest.php b/tests/Rule/Nested/NestedTest.php index 64572c796..419a27cf6 100644 --- a/tests/Rule/Nested/NestedTest.php +++ b/tests/Rule/Nested/NestedTest.php @@ -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 { @@ -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); diff --git a/tests/Rule/NumberTest.php b/tests/Rule/NumberTest.php index 495602192..ade27cac4 100644 --- a/tests/Rule/NumberTest.php +++ b/tests/Rule/NumberTest.php @@ -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; diff --git a/tests/Support/Data/IteratorWithBooleanKey.php b/tests/Support/Data/IteratorWithBooleanKey.php index db21028ff..5e3cb9fe7 100644 --- a/tests/Support/Data/IteratorWithBooleanKey.php +++ b/tests/Support/Data/IteratorWithBooleanKey.php @@ -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; diff --git a/tests/Support/Rule/PiHandler.php b/tests/Support/Rule/PiHandler.php index 841a4f9c8..126226868 100644 --- a/tests/Support/Rule/PiHandler.php +++ b/tests/Support/Rule/PiHandler.php @@ -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