From b754b0ca7a07032263d1c4879e68a31b9a452ec7 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 12 Dec 2025 10:54:24 +0300 Subject: [PATCH] Replace StyleCI to PHP CS Fixer --- .../workflows/{rector.yml => rector-cs.yml} | 16 +- .gitignore | 5 +- .php-cs-fixer.dist.php | 22 +++ .styleci.yml | 85 --------- composer.json | 4 +- src/CountableLimitInterface.php | 6 +- src/DataSet/ArrayDataSet.php | 3 +- src/DataSet/ObjectDataSet.php | 8 +- src/DataSet/SingleValueDataSet.php | 3 +- src/Debug/ValidatorCollector.php | 2 +- src/Debug/ValidatorInterfaceProxy.php | 5 +- src/EmptyCondition/WhenEmpty.php | 3 +- src/Error.php | 2 +- ...andlerInterfaceNotImplementedException.php | 2 +- src/Exception/UnexpectedRuleException.php | 2 +- src/Helper/MessageProcessor.php | 5 +- src/Helper/ObjectParser.php | 10 +- src/Helper/RulesNormalizer.php | 2 +- src/Helper/RulesNormalizerIterator.php | 4 +- src/Label.php | 3 +- .../ArrayPropertyTranslator.php | 3 +- .../TranslatorPropertyTranslator.php | 3 +- src/Result.php | 2 +- src/Rule/AbstractCompare.php | 18 +- src/Rule/AbstractNumber.php | 6 +- src/Rule/AnyRule.php | 2 +- src/Rule/BooleanValue.php | 6 +- src/Rule/Callback.php | 12 +- src/Rule/Compare.php | 4 +- src/Rule/CompareHandler.php | 16 +- src/Rule/Composite.php | 4 +- src/Rule/Count.php | 26 +-- src/Rule/Date/BaseDate.php | 2 +- src/Rule/Date/BaseDateHandler.php | 17 +- src/Rule/Date/Date.php | 2 +- src/Rule/Date/DateTime.php | 2 +- src/Rule/Date/Time.php | 2 +- src/Rule/Each.php | 6 +- src/Rule/Email.php | 2 +- src/Rule/EmailHandler.php | 2 +- src/Rule/Equal.php | 4 +- src/Rule/FilledAtLeast.php | 2 +- src/Rule/FilledOnlyOneOf.php | 2 +- src/Rule/GreaterThan.php | 6 +- src/Rule/GreaterThanOrEqual.php | 6 +- src/Rule/Image/CompositeImageInfoProvider.php | 2 +- src/Rule/Image/Image.php | 2 +- src/Rule/Image/ImageAspectRatio.php | 3 +- src/Rule/Image/ImageHandler.php | 4 +- src/Rule/Image/ImageInfo.php | 3 +- src/Rule/In.php | 2 +- src/Rule/InEnum.php | 4 +- src/Rule/Integer.php | 2 +- src/Rule/Ip.php | 2 +- src/Rule/IpHandler.php | 18 +- src/Rule/Json.php | 2 +- src/Rule/Length.php | 22 +-- src/Rule/LessThan.php | 6 +- src/Rule/LessThanOrEqual.php | 6 +- src/Rule/Nested.php | 178 +++++++++--------- src/Rule/NestedHandler.php | 4 +- src/Rule/NotEqual.php | 6 +- src/Rule/Number.php | 2 +- src/Rule/Regex.php | 2 +- src/Rule/RegexHandler.php | 4 +- src/Rule/Required.php | 4 +- src/Rule/RequiredHandler.php | 2 +- src/Rule/StopOnError.php | 2 +- src/Rule/StringValue.php | 2 +- src/Rule/Subset.php | 2 +- src/Rule/Trait/CountableLimitHandlerTrait.php | 2 +- src/Rule/Trait/CountableLimitTrait.php | 124 ++++++------ src/Rule/Trait/SkipOnEmptyTrait.php | 2 +- src/Rule/Trait/WhenTrait.php | 4 +- src/Rule/TrueValue.php | 6 +- src/Rule/Type/BooleanType.php | 2 +- src/Rule/Type/FloatType.php | 2 +- src/Rule/Type/IntegerType.php | 2 +- src/Rule/Type/StringType.php | 2 +- src/Rule/UniqueIterable.php | 6 +- src/Rule/Url.php | 2 +- src/Rule/UrlHandler.php | 4 +- src/Rule/Uuid.php | 2 +- .../RuleHandlerContainer.php | 3 +- src/ValidationContext.php | 5 +- src/Validator.php | 18 +- src/WhenInterface.php | 4 +- tests/BaseConfigTestCase.php | 4 +- tests/DataSet/DataSetUsageTest.php | 10 +- tests/DataSet/ObjectDataSetTest.php | 29 ++- tests/Debug/ValidatorInterfaceProxyTest.php | 4 +- tests/Helper/ObjectParserTest.php | 10 +- tests/Helper/RulesDumperTest.php | 8 +- tests/Helper/RulesNormalizerTest.php | 6 +- tests/Helper/SkipOnEmptyNormalizerTest.php | 2 +- tests/MessagesTest.php | 8 +- tests/ResultTest.php | 16 +- tests/Rule/AnyRuleTest.php | 16 +- tests/Rule/Base/CountableLimitTestTrait.php | 10 +- .../Base/DifferentRuleInHandlerTestTrait.php | 2 +- tests/Rule/Base/RuleTestCase.php | 10 +- .../Rule/Base/RuleWithProvidedRulesTrait.php | 3 +- tests/Rule/Base/SkipOnErrorTestTrait.php | 2 +- tests/Rule/Base/WhenTestTrait.php | 2 +- tests/Rule/BooleanValueTest.php | 12 +- tests/Rule/CallbackTest.php | 27 ++- tests/Rule/CompareTest.php | 114 +++++------ tests/Rule/CompositeTest.php | 38 ++-- tests/Rule/CountTest.php | 20 +- tests/Rule/CountableLimitHandlerTraitTest.php | 12 +- tests/Rule/Date/DateTest.php | 6 +- tests/Rule/Date/DateTimeTest.php | 4 +- tests/Rule/Date/TimeTest.php | 4 +- tests/Rule/EachTest.php | 44 ++--- tests/Rule/EmailTest.php | 16 +- tests/Rule/EqualTest.php | 14 +- tests/Rule/FilledAtLeastTest.php | 26 +-- tests/Rule/FilledOnlyOneOfTest.php | 18 +- tests/Rule/GreaterThanOrEqualTest.php | 14 +- tests/Rule/GreaterThanTest.php | 14 +- tests/Rule/Image/ImageTest.php | 16 +- tests/Rule/Image/StubImageInfoProvider.php | 3 +- tests/Rule/InEnumTest.php | 2 +- tests/Rule/InTest.php | 2 +- tests/Rule/IpTest.php | 4 +- tests/Rule/JsonTest.php | 2 +- tests/Rule/LengthTest.php | 2 +- tests/Rule/LessThanOrEqualTest.php | 14 +- tests/Rule/LessThanTest.php | 14 +- .../NestedPropertyTranslator/MainForm.php | 3 +- .../NestedPropertyTranslatorTest.php | 2 +- tests/Rule/Nested/NestedTest.php | 103 +++++----- tests/Rule/NotEqualTest.php | 14 +- tests/Rule/NumberTest.php | 2 +- tests/Rule/RegexTest.php | 2 +- tests/Rule/RequiredTest.php | 4 +- tests/Rule/StopOnErrorTest.php | 30 +-- tests/Rule/StringValueTest.php | 15 +- tests/Rule/SubsetTest.php | 2 +- tests/Rule/TrueValueTest.php | 12 +- tests/Rule/Type/BooleanTypeTest.php | 11 +- tests/Rule/Type/FloatTypeTest.php | 11 +- tests/Rule/Type/IntegerTypeTest.php | 11 +- tests/Rule/Type/StringTypeTest.php | 13 +- tests/Rule/UniqueIterableTest.php | 48 +++-- tests/Rule/UrlTest.php | 2 +- tests/Rule/UuidTest.php | 12 +- .../RuleHandlerContainerTest.php | 2 +- .../SimpleRuleHandlerContainerTest.php | 2 +- .../AttributesRulesProviderTest.php | 15 +- tests/Support/Data/AtLeastDto.php | 3 +- tests/Support/Data/CallbackDto.php | 3 +- tests/Support/Data/CompareObject.php | 3 +- tests/Support/Data/CountDto.php | 3 +- tests/Support/Data/EachDto.php | 3 +- tests/Support/Data/FilledOnlyOneOfDto.php | 3 +- .../NestedObjectWithPostValidationHook.php | 2 +- ...condNestedObjectWithPostValidationHook.php | 4 +- .../Data/ObjectForIterableCollection.php | 3 +- .../Support/Data/ObjectWithDynamicDataSet.php | 4 +- tests/Support/Data/SimpleForm.php | 3 +- .../Support/DataSet/RulesProvidedDataSet.php | 4 +- tests/Support/Rule/NotRuleAttribute.php | 4 +- tests/Support/Rule/RuleWithCustomHandler.php | 3 +- .../Support/Rule/StubRule/StubDumpedRule.php | 4 +- .../RulesProvider/SimpleRulesProvider.php | 3 +- tests/Support/StringableObject.php | 3 +- .../WithoutIntl/ConfigTest.php | 2 +- tests/ValidatorTest.php | 81 ++++---- 169 files changed, 876 insertions(+), 987 deletions(-) rename .github/workflows/{rector.yml => rector-cs.yml} (61%) create mode 100644 .php-cs-fixer.dist.php delete mode 100644 .styleci.yml diff --git a/.github/workflows/rector.yml b/.github/workflows/rector-cs.yml similarity index 61% rename from .github/workflows/rector.yml rename to .github/workflows/rector-cs.yml index 20f7d8993..69a1310cf 100644 --- a/.github/workflows/rector.yml +++ b/.github/workflows/rector-cs.yml @@ -1,3 +1,5 @@ +name: Rector + PHP CS Fixer + on: pull_request_target: paths-ignore: @@ -9,16 +11,18 @@ on: - 'infection.json.dist' - 'psalm.xml' -name: rector +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true jobs: rector: - uses: yiisoft/actions/.github/workflows/rector.yml@master + uses: yiisoft/actions/.github/workflows/rector-cs.yml@master secrets: token: ${{ secrets.YIISOFT_GITHUB_TOKEN }} with: repository: ${{ github.event.pull_request.head.repo.full_name }} - os: >- - ['ubuntu-latest'] - php: >- - ['8.5'] + php: '8.1' diff --git a/.gitignore b/.gitignore index 185b9ea46..17dcf1d2c 100644 --- a/.gitignore +++ b/.gitignore @@ -27,5 +27,8 @@ composer.phar /phpunit.xml /.phpunit.cache +# PHP CS Fixer +/.php-cs-fixer.cache + # po4a .mo files -*.mo \ No newline at end of file +*.mo diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 000000000..c8091220b --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,22 @@ +in([ + __DIR__ . '/src', + __DIR__ . '/tests', +]); + +return (new Config()) + ->setParallelConfig(ParallelConfigFactory::detect()) + ->setRules([ + '@PER-CS3.0' => true, + 'no_unused_imports' => true, + 'ordered_class_elements' => true, + 'class_attributes_separation' => ['elements' => ['method' => 'one']], + ]) + ->setFinder($finder); diff --git a/.styleci.yml b/.styleci.yml deleted file mode 100644 index 1ab379b46..000000000 --- a/.styleci.yml +++ /dev/null @@ -1,85 +0,0 @@ -preset: psr12 -risky: true - -version: 8.1 - -finder: - exclude: - - docs - - vendor - -enabled: - - alpha_ordered_traits - - array_indentation - - array_push - - combine_consecutive_issets - - combine_consecutive_unsets - - combine_nested_dirname - - declare_strict_types - - dir_constant - - fully_qualified_strict_types - - function_to_constant - - hash_to_slash_comment - - is_null - - logical_operators - - magic_constant_casing - - magic_method_casing - - method_separation - - modernize_types_casting - - native_function_casing - - native_function_type_declaration_casing - - no_alias_functions - - no_empty_comment - - no_empty_phpdoc - - no_empty_statement - - no_extra_block_blank_lines - - no_short_bool_cast - - no_superfluous_elseif - - no_unneeded_control_parentheses - - no_unneeded_curly_braces - - no_unneeded_final_method - - no_unset_cast - - no_unused_imports - - no_unused_lambda_imports - - no_useless_else - - no_useless_return - - normalize_index_brace - - php_unit_dedicate_assert - - php_unit_dedicate_assert_internal_type - - php_unit_expectation - - php_unit_mock - - php_unit_mock_short_will_return - - php_unit_namespaced - - php_unit_no_expectation_annotation - - phpdoc_no_empty_return - - phpdoc_no_useless_inheritdoc - - phpdoc_order - - phpdoc_property - - phpdoc_scalar - - phpdoc_singular_inheritdoc - - phpdoc_trim - - phpdoc_trim_consecutive_blank_line_separation - - phpdoc_type_to_var - - phpdoc_types - - phpdoc_types_order - - print_to_echo - - regular_callable_call - - return_assignment - - self_accessor - - self_static_accessor - - set_type_to_cast - - short_array_syntax - - short_list_syntax - - simplified_if_return - - single_quote - - standardize_not_equals - - ternary_to_null_coalescing - - trailing_comma_in_multiline_array - - unalign_double_arrow - - unalign_equals - - empty_loop_body_braces - - integer_literal_case - - union_type_without_spaces - -disabled: - - function_declaration diff --git a/composer.json b/composer.json index 6ef770288..522ee9c00 100644 --- a/composer.json +++ b/composer.json @@ -39,6 +39,7 @@ }, "require-dev": { "bamarni/composer-bin-plugin": "^1.8.3", + "friendsofphp/php-cs-fixer": "^3.91", "jetbrains/phpstorm-attributes": "^1.2", "maglnet/composer-require-checker": "^4.7.1", "phpbench/phpbench": "^1.4.1", @@ -93,6 +94,7 @@ "rector": "vendor/bin/rector process", "infection": "vendor/bin/infection", "dry-run": "vendor/bin/rector process --dry-run", - "psalm": "vendor/bin/psalm --config=psalm.xml" + "psalm": "vendor/bin/psalm --config=psalm.xml", + "cs-fix": "php-cs-fixer fix" } } diff --git a/src/CountableLimitInterface.php b/src/CountableLimitInterface.php index 8fdb6b059..6f936d291 100644 --- a/src/CountableLimitInterface.php +++ b/src/CountableLimitInterface.php @@ -20,7 +20,7 @@ interface CountableLimitInterface * * @see getLessThanMinMessage() for related error message. */ - public function getMin(): int|null; + public function getMin(): ?int; /** * Returns current maximum limit. @@ -29,7 +29,7 @@ public function getMin(): int|null; * * @see getGreaterThanMaxMessage() for related error message. */ - public function getMax(): int|null; + public function getMax(): ?int; /** * Returns current "exactly" value meant for strict comparison. It is a shortcut for the case when {@see getMin()} @@ -40,7 +40,7 @@ public function getMax(): int|null; * * @see getNotExactlyMessage() for related error message */ - public function getExactly(): int|null; + public function getExactly(): ?int; /** * Returns message used when a validated value is less than minimum set in {@see getMin()}. diff --git a/src/DataSet/ArrayDataSet.php b/src/DataSet/ArrayDataSet.php index c75849955..f92c8c0cb 100644 --- a/src/DataSet/ArrayDataSet.php +++ b/src/DataSet/ArrayDataSet.php @@ -27,8 +27,7 @@ public function __construct( * @var array A mapping between property names and their values. */ private array $data = [], - ) { - } + ) {} /** * Returns a property value by its name. diff --git a/src/DataSet/ObjectDataSet.php b/src/DataSet/ObjectDataSet.php index bc2646166..418782b9e 100644 --- a/src/DataSet/ObjectDataSet.php +++ b/src/DataSet/ObjectDataSet.php @@ -188,9 +188,9 @@ public function __construct( * @var object An object containing rules and data. */ private readonly object $object, - int $propertyVisibility = ReflectionProperty::IS_PRIVATE | - ReflectionProperty::IS_PROTECTED | - ReflectionProperty::IS_PUBLIC, + int $propertyVisibility = ReflectionProperty::IS_PRIVATE + | ReflectionProperty::IS_PROTECTED + | ReflectionProperty::IS_PUBLIC, bool $useCache = true, ) { $this->dataSetProvided = $this->object instanceof DataSetInterface; @@ -198,7 +198,7 @@ public function __construct( $this->parser = new ObjectParser( source: $object, propertyVisibility: $propertyVisibility, - useCache: $useCache + useCache: $useCache, ); } diff --git a/src/DataSet/SingleValueDataSet.php b/src/DataSet/SingleValueDataSet.php index 5549a7e83..d64708d1c 100644 --- a/src/DataSet/SingleValueDataSet.php +++ b/src/DataSet/SingleValueDataSet.php @@ -33,8 +33,7 @@ public function __construct( * @var mixed Single value of any (mixed) data type. */ private readonly mixed $value, - ) { - } + ) {} /** * Returns a property value by its name. {@see SingleValueDataSet} does not support properties so `null` is always diff --git a/src/Debug/ValidatorCollector.php b/src/Debug/ValidatorCollector.php index 64295540f..af4b3febd 100644 --- a/src/Debug/ValidatorCollector.php +++ b/src/Debug/ValidatorCollector.php @@ -46,7 +46,7 @@ public function getSummary(): array } $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 => (bool) $data['result'])); $countInvalid = $count - $countValid; return [ diff --git a/src/Debug/ValidatorInterfaceProxy.php b/src/Debug/ValidatorInterfaceProxy.php index 6eb805566..df5dd2f48 100644 --- a/src/Debug/ValidatorInterfaceProxy.php +++ b/src/Debug/ValidatorInterfaceProxy.php @@ -14,13 +14,12 @@ final class ValidatorInterfaceProxy implements ValidatorInterface public function __construct( private readonly ValidatorInterface $validator, private readonly ValidatorCollector $collector, - ) { - } + ) {} public function validate( mixed $data, callable|iterable|object|string|null $rules = null, - ?ValidationContext $context = null + ?ValidationContext $context = null, ): Result { $result = $this->validator->validate($data, $rules, $context); diff --git a/src/EmptyCondition/WhenEmpty.php b/src/EmptyCondition/WhenEmpty.php index 971c47fd9..95ff0d2ba 100644 --- a/src/EmptyCondition/WhenEmpty.php +++ b/src/EmptyCondition/WhenEmpty.php @@ -34,8 +34,7 @@ public function __construct( * meaning no trimming is done. */ private readonly bool $trimString = false, - ) { - } + ) {} /** * @param mixed $value The validated value. diff --git a/src/Error.php b/src/Error.php index e83f2a2ed..9e2dcd664 100644 --- a/src/Error.php +++ b/src/Error.php @@ -117,7 +117,7 @@ public function getParameters(): array * @return array A list of keys for nested structures or an empty array otherwise. * @psalm-return list */ - public function getValuePath(string|null $escape = null): array + public function getValuePath(?string $escape = null): array { if ($escape === null) { return $this->valuePath; diff --git a/src/Exception/RuleHandlerInterfaceNotImplementedException.php b/src/Exception/RuleHandlerInterfaceNotImplementedException.php index 765e6c483..2f843c6d7 100644 --- a/src/Exception/RuleHandlerInterfaceNotImplementedException.php +++ b/src/Exception/RuleHandlerInterfaceNotImplementedException.php @@ -41,7 +41,7 @@ class_exists($type) RuleHandlerInterface::class, ), $code, - $previous + $previous, ); } } diff --git a/src/Exception/UnexpectedRuleException.php b/src/Exception/UnexpectedRuleException.php index c7360e86f..ba1300a78 100644 --- a/src/Exception/UnexpectedRuleException.php +++ b/src/Exception/UnexpectedRuleException.php @@ -53,7 +53,7 @@ public function __construct( sprintf( 'Expected "%s", but "%s" given.', implode('", "', (array) $expectedClassName), - $actualObject::class + $actualObject::class, ), $code, $previous, diff --git a/src/Helper/MessageProcessor.php b/src/Helper/MessageProcessor.php index c3842b51e..0703ed0c2 100644 --- a/src/Helper/MessageProcessor.php +++ b/src/Helper/MessageProcessor.php @@ -25,8 +25,7 @@ public function __construct( private readonly string $translationCategory, private readonly MessageFormatterInterface $messageFormatter, private readonly string $messageFormatterLocale, - ) { - } + ) {} public function process(Error $error): string { @@ -34,7 +33,7 @@ public function process(Error $error): string Error::MESSAGE_TRANSLATE => $this->translator->translate( $error->getMessage(), $error->getParameters(), - $this->translationCategory + $this->translationCategory, ), Error::MESSAGE_FORMAT => $this->messageFormatter->format( $error->getMessage(), diff --git a/src/Helper/ObjectParser.php b/src/Helper/ObjectParser.php index 3c7337eae..15d778bad 100644 --- a/src/Helper/ObjectParser.php +++ b/src/Helper/ObjectParser.php @@ -142,7 +142,7 @@ final class ObjectParser /** * @var string|null A cache key. Dynamically generated on instantiation. */ - private string|null $cacheKey = null; + private ?string $cacheKey = null; /** * @throws InvalidArgumentException If a class name string provided in {@see $source} refers to a non-existing @@ -163,9 +163,9 @@ public function __construct( * * @psalm-var int-mask-of */ - private readonly int $propertyVisibility = ReflectionProperty::IS_PRIVATE | - ReflectionProperty::IS_PROTECTED | - ReflectionProperty::IS_PUBLIC, + private readonly int $propertyVisibility = ReflectionProperty::IS_PRIVATE + | ReflectionProperty::IS_PROTECTED + | ReflectionProperty::IS_PUBLIC, /** * @var bool Whether the properties with "static" modifier must be skipped. */ @@ -179,7 +179,7 @@ public function __construct( /** @var object|string $source */ if (is_string($source) && !class_exists($source)) { throw new InvalidArgumentException( - sprintf('Class "%s" not found.', $source) + sprintf('Class "%s" not found.', $source), ); } diff --git a/src/Helper/RulesNormalizer.php b/src/Helper/RulesNormalizer.php index f64b710ec..966765434 100644 --- a/src/Helper/RulesNormalizer.php +++ b/src/Helper/RulesNormalizer.php @@ -91,7 +91,7 @@ public static function normalize( sprintf( 'A property can only have an integer or a string type. %s given.', get_debug_type($property), - ) + ), ); } diff --git a/src/Helper/RulesNormalizerIterator.php b/src/Helper/RulesNormalizerIterator.php index 202de450c..c4d2324ac 100644 --- a/src/Helper/RulesNormalizerIterator.php +++ b/src/Helper/RulesNormalizerIterator.php @@ -83,8 +83,8 @@ private static function normalizeRule(mixed $rule, ?callable $defaultSkipOnEmpty sprintf( 'Rule must be either an instance of %s or a callable, %s given.', RuleInterface::class, - get_debug_type($rule) - ) + get_debug_type($rule), + ), ); } diff --git a/src/Label.php b/src/Label.php index 1ce88dd8a..3f33d5b3c 100644 --- a/src/Label.php +++ b/src/Label.php @@ -11,8 +11,7 @@ final class Label { public function __construct( private readonly string $label, - ) { - } + ) {} public function getLabel(): string { diff --git a/src/PropertyTranslator/ArrayPropertyTranslator.php b/src/PropertyTranslator/ArrayPropertyTranslator.php index 1b3507963..6f54746f5 100644 --- a/src/PropertyTranslator/ArrayPropertyTranslator.php +++ b/src/PropertyTranslator/ArrayPropertyTranslator.php @@ -19,8 +19,7 @@ final class ArrayPropertyTranslator implements PropertyTranslatorInterface */ public function __construct( private array $translations, - ) { - } + ) {} public function translate(string $property): string { diff --git a/src/PropertyTranslator/TranslatorPropertyTranslator.php b/src/PropertyTranslator/TranslatorPropertyTranslator.php index 36c10fe8c..be16e759f 100644 --- a/src/PropertyTranslator/TranslatorPropertyTranslator.php +++ b/src/PropertyTranslator/TranslatorPropertyTranslator.php @@ -17,8 +17,7 @@ final class TranslatorPropertyTranslator implements PropertyTranslatorInterface */ public function __construct( private readonly TranslatorInterface $translator, - ) { - } + ) {} public function translate(string $property): string { diff --git a/src/Result.php b/src/Result.php index 773119c94..9340d99ff 100644 --- a/src/Result.php +++ b/src/Result.php @@ -68,7 +68,7 @@ public function getErrors(): array */ public function getErrorMessages(): array { - return array_map(static fn (Error $error): string => $error->getMessage(), $this->errors); + return array_map(static fn(Error $error): string => $error->getMessage(), $this->errors); } /** diff --git a/src/Rule/AbstractCompare.php b/src/Rule/AbstractCompare.php index c8de42397..037e96b6c 100644 --- a/src/Rule/AbstractCompare.php +++ b/src/Rule/AbstractCompare.php @@ -49,17 +49,17 @@ abstract class AbstractCompare implements * * @psalm-suppress MissingClassConstType */ - protected const DEFAULT_INCORRECT_INPUT_MESSAGE = 'The allowed types for {property} are integer, float, string, ' . - 'boolean, null and object implementing \Stringable interface or \DateTimeInterface. {type} given.'; + protected const DEFAULT_INCORRECT_INPUT_MESSAGE = 'The allowed types for {property} are integer, float, string, ' + . 'boolean, null and object implementing \Stringable interface or \DateTimeInterface. {type} given.'; /** * A default for {@see $incorrectDataSetTypeMessage}. * * @psalm-suppress MissingClassConstType */ - protected const DEFAULT_INCORRECT_DATA_SET_TYPE_MESSAGE = '{Property} returned from a custom data set must have ' . - 'one of the following types: integer, float, string, boolean, null or an object implementing \Stringable ' . - 'interface or \DateTimeInterface.'; + protected const DEFAULT_INCORRECT_DATA_SET_TYPE_MESSAGE = '{Property} returned from a custom data set must have ' + . 'one of the following types: integer, float, string, boolean, null or an object implementing \Stringable ' + . 'interface or \DateTimeInterface.'; /** * List of valid types. @@ -156,15 +156,15 @@ abstract class AbstractCompare implements */ public function __construct( private mixed $targetValue = null, - private string|null $targetProperty = null, + private ?string $targetProperty = null, private string $incorrectInputMessage = self::DEFAULT_INCORRECT_INPUT_MESSAGE, private string $incorrectDataSetTypeMessage = self::DEFAULT_INCORRECT_DATA_SET_TYPE_MESSAGE, - private string|null $message = null, + private ?string $message = null, private string $type = CompareType::NUMBER, private string $operator = '==', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { if (!in_array($this->type, self::VALID_TYPES)) { $validTypesString = $this->getQuotedList(self::VALID_TYPES); @@ -208,7 +208,7 @@ public function getTargetValue(): mixed * * @see $targetProperty */ - public function getTargetProperty(): string|null + public function getTargetProperty(): ?string { return $this->targetProperty; } diff --git a/src/Rule/AbstractNumber.php b/src/Rule/AbstractNumber.php index da2cf2e7c..c3a5320f1 100644 --- a/src/Rule/AbstractNumber.php +++ b/src/Rule/AbstractNumber.php @@ -41,8 +41,8 @@ abstract class AbstractNumber implements * * @psalm-suppress MissingClassConstType */ - protected const DEFAULT_INCORRECT_INPUT_MESSAGE = 'The allowed types for {property} are integer, float and ' . - 'string. {type} given.'; + protected const DEFAULT_INCORRECT_INPUT_MESSAGE = 'The allowed types for {property} are integer, float and ' + . 'string. {type} given.'; /** * A default for {@see $lessThanMinMessage}. @@ -115,7 +115,7 @@ public function __construct( string $pattern, bool|callable|null $skipOnEmpty, private bool $skipOnError, - private Closure|null $when, + private ?Closure $when, ) { if ($pattern === '') { throw new InvalidArgumentException('Pattern can\'t be empty.'); diff --git a/src/Rule/AnyRule.php b/src/Rule/AnyRule.php index a27a222ae..1ee73f1e4 100644 --- a/src/Rule/AnyRule.php +++ b/src/Rule/AnyRule.php @@ -85,7 +85,7 @@ public function __construct( private string $message = 'At least one of the inner rules must pass the validation.', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { $this->rules = RulesNormalizer::normalizeList($rules); $this->skipOnEmpty = $skipOnEmpty; diff --git a/src/Rule/BooleanValue.php b/src/Rule/BooleanValue.php index e93475ba1..1c5e4ffb9 100644 --- a/src/Rule/BooleanValue.php +++ b/src/Rule/BooleanValue.php @@ -86,12 +86,12 @@ public function __construct( private int|float|string|bool $trueValue = '1', private int|float|string|bool $falseValue = '0', private bool $strict = false, - private string $incorrectInputMessage = 'The allowed types for {property} are integer, float, string, ' . - 'boolean. {type} given.', + private string $incorrectInputMessage = 'The allowed types for {property} are integer, float, string, ' + . 'boolean. {type} given.', private string $message = '{Property} must be either "{true}" or "{false}".', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { $this->skipOnEmpty = $skipOnEmpty; } diff --git a/src/Rule/Callback.php b/src/Rule/Callback.php index ca9267c38..28857e79c 100644 --- a/src/Rule/Callback.php +++ b/src/Rule/Callback.php @@ -57,10 +57,10 @@ final class Callback implements */ public function __construct( private mixed $callback = null, - private string|null $method = null, + private ?string $method = null, bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { if ($this->callback === null && $this->method === null) { throw new InvalidArgumentException('Either "$callback" or "$method" must be specified.'); @@ -85,7 +85,7 @@ public function getName(): string * * @see $callback */ - public function getCallback(): callable|null + public function getCallback(): ?callable { return $this->callback; } @@ -97,7 +97,7 @@ public function getCallback(): callable|null * * @see $method */ - public function getMethod(): string|null + public function getMethod(): ?string { return $this->method; } @@ -117,12 +117,12 @@ public function afterInitAttribute(object $object): void 'Method "%s" does not exist in class "%s".', $method, $object::class, - ) + ), ); } /** @psalm-suppress MixedMethodCall */ - $this->callback = Closure::bind(fn (mixed ...$args): mixed => $object->{$method}(...$args), $object, $object); + $this->callback = Closure::bind(fn(mixed ...$args): mixed => $object->{$method}(...$args), $object, $object); } public function getOptions(): array diff --git a/src/Rule/Compare.php b/src/Rule/Compare.php index 775dd0848..60431fd19 100644 --- a/src/Rule/Compare.php +++ b/src/Rule/Compare.php @@ -28,6 +28,4 @@ * @see CompareHandler */ #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] -final class Compare extends AbstractCompare -{ -} +final class Compare extends AbstractCompare {} diff --git a/src/Rule/CompareHandler.php b/src/Rule/CompareHandler.php index 2bd854a10..11559668b 100644 --- a/src/Rule/CompareHandler.php +++ b/src/Rule/CompareHandler.php @@ -105,10 +105,10 @@ private function isInputCorrect(string $type, mixed $value): bool */ private function isValueAllowedForTypeCasting(mixed $value): bool { - return $value === null || - is_scalar($value) || - $value instanceof Stringable || - $value instanceof DateTimeInterface; + return $value === null + || is_scalar($value) + || $value instanceof Stringable + || $value instanceof DateTimeInterface; } /** @@ -164,12 +164,12 @@ private function compareValues(string $type, string $operator, mixed $value, mix '!==' => !$this->checkValuesAreEqual($type, $value, $targetValue, strict: true), '>' => $value > $targetValue, /** @infection-ignore-all */ - '>=' => $this->checkValuesAreEqual($type, $value, $targetValue) || - $this->normalizeValue($type, $value) > $this->normalizeValue($type, $targetValue), + '>=' => $this->checkValuesAreEqual($type, $value, $targetValue) + || $this->normalizeValue($type, $value) > $this->normalizeValue($type, $targetValue), '<' => $value < $targetValue, /** @infection-ignore-all */ - '<=' => $this->checkValuesAreEqual($type, $value, $targetValue) || - $this->normalizeValue($type, $value) < $this->normalizeValue($type, $targetValue), + '<=' => $this->checkValuesAreEqual($type, $value, $targetValue) + || $this->normalizeValue($type, $value) < $this->normalizeValue($type, $targetValue), }; } diff --git a/src/Rule/Composite.php b/src/Rule/Composite.php index 26a5d935c..b45911bdc 100644 --- a/src/Rule/Composite.php +++ b/src/Rule/Composite.php @@ -104,7 +104,7 @@ class Composite implements * * @psalm-var WhenType */ - private Closure|null $when = null; + private ?Closure $when = null; /** * @param iterable $rules A set of rules that needs to be grouped. They will be normalized using @@ -126,7 +126,7 @@ public function __construct( iterable $rules = [], bool|callable|null $skipOnEmpty = null, bool $skipOnError = false, - Closure|null $when = null, + ?Closure $when = null, ) { $this->skipOnEmpty = $skipOnEmpty; $this->rules = RulesNormalizer::normalizeList($rules); diff --git a/src/Rule/Count.php b/src/Rule/Count.php index c0329beff..57bd440ab 100644 --- a/src/Rule/Count.php +++ b/src/Rule/Count.php @@ -85,20 +85,20 @@ final class Count implements * @psalm-param WhenType $when */ public function __construct( - int|null $exactly = null, - int|null $min = null, - int|null $max = null, - private string $incorrectInputMessage = '{Property} must be an array or implement \Countable interface. ' . - '{type} given.', - string $lessThanMinMessage = '{Property} must contain at least {min, number} {min, plural, one{item} ' . - 'other{items}}.', - string $greaterThanMaxMessage = '{Property} must contain at most {max, number} {max, plural, one{item} ' . - 'other{items}}.', - string $notExactlyMessage = '{Property} must contain exactly {exactly, number} {exactly, plural, one{item} ' . - 'other{items}}.', + ?int $exactly = null, + ?int $min = null, + ?int $max = null, + private string $incorrectInputMessage = '{Property} must be an array or implement \Countable interface. ' + . '{type} given.', + string $lessThanMinMessage = '{Property} must contain at least {min, number} {min, plural, one{item} ' + . 'other{items}}.', + string $greaterThanMaxMessage = '{Property} must contain at most {max, number} {max, plural, one{item} ' + . 'other{items}}.', + string $notExactlyMessage = '{Property} must contain exactly {exactly, number} {exactly, plural, one{item} ' + . 'other{items}}.', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { $this->initCountableLimitProperties( $min, @@ -106,7 +106,7 @@ public function __construct( $exactly, $lessThanMinMessage, $greaterThanMaxMessage, - $notExactlyMessage + $notExactlyMessage, ); $this->skipOnEmpty = $skipOnEmpty; } diff --git a/src/Rule/Date/BaseDate.php b/src/Rule/Date/BaseDate.php index 6fe4933de..d67f34deb 100644 --- a/src/Rule/Date/BaseDate.php +++ b/src/Rule/Date/BaseDate.php @@ -45,7 +45,7 @@ public function __construct( private ?string $tooLateMessage, bool|callable|null $skipOnEmpty, private bool $skipOnError, - private Closure|null $when, + private ?Closure $when, ) { $this->skipOnEmpty = $skipOnEmpty; } diff --git a/src/Rule/Date/BaseDateHandler.php b/src/Rule/Date/BaseDateHandler.php index 5c30cc989..f8ad75e56 100644 --- a/src/Rule/Date/BaseDateHandler.php +++ b/src/Rule/Date/BaseDateHandler.php @@ -40,8 +40,7 @@ public function __construct( private readonly string $incorrectInputMessage, private readonly string $tooEarlyMessage, private readonly string $tooLateMessage, - ) { - } + ) {} public function validate(mixed $value, RuleInterface $rule, ValidationContext $context): Result { @@ -63,7 +62,7 @@ public function validate(mixed $value, RuleInterface $rule, ValidationContext $c [ 'property' => $context->getTranslatedProperty(), 'Property' => $context->getCapitalizedTranslatedProperty(), - ] + ], ); return $result; } @@ -77,7 +76,7 @@ public function validate(mixed $value, RuleInterface $rule, ValidationContext $c 'Property' => $context->getCapitalizedTranslatedProperty(), 'value' => $this->formatDate($date, $rule, $timeZone), 'limit' => $this->formatDate($min, $rule, $timeZone), - ] + ], ); return $result; } @@ -91,7 +90,7 @@ public function validate(mixed $value, RuleInterface $rule, ValidationContext $c 'Property' => $context->getCapitalizedTranslatedProperty(), 'value' => $this->formatDate($date, $rule, $timeZone), 'limit' => $this->formatDate($max, $rule, $timeZone), - ] + ], ); return $result; } @@ -103,7 +102,7 @@ private function prepareValue( mixed $value, Date|DateTime|Time $rule, ?DateTimeZone $timeZone, - bool $strict + bool $strict, ): ?DateTimeInterface { $format = $rule->getFormat(); @@ -150,7 +149,7 @@ private function prepareValue( private function prepareValueWithPhpFormat( string $value, string $format, - ?DateTimeZone $timeZone + ?DateTimeZone $timeZone, ): ?DateTimeInterface { $date = DateTimeImmutable::createFromFormat($format, $value, $timeZone); if ($date === false) { @@ -213,7 +212,7 @@ private function makeFormatter( ?string $locale, int $dateType, int $timeType, - ?DateTimeZone $timeZone + ?DateTimeZone $timeZone, ): IntlDateFormatter { if ($format === null) { return new IntlDateFormatter($locale, $dateType, $timeType, $timeZone); @@ -224,7 +223,7 @@ private function makeFormatter( IntlDateFormatter::NONE, IntlDateFormatter::NONE, $timeZone, - pattern: $format + pattern: $format, ); } diff --git a/src/Rule/Date/Date.php b/src/Rule/Date/Date.php index 2e4c31837..5a5df7de8 100644 --- a/src/Rule/Date/Date.php +++ b/src/Rule/Date/Date.php @@ -132,7 +132,7 @@ public function __construct( ?string $tooLateMessage = null, bool|callable|null $skipOnEmpty = null, bool $skipOnError = false, - Closure|null $when = null, + ?Closure $when = null, ) { parent::__construct( $format, diff --git a/src/Rule/Date/DateTime.php b/src/Rule/Date/DateTime.php index 2a0e11905..92112f742 100644 --- a/src/Rule/Date/DateTime.php +++ b/src/Rule/Date/DateTime.php @@ -142,7 +142,7 @@ public function __construct( ?string $tooLateMessage = null, bool|callable|null $skipOnEmpty = null, bool $skipOnError = false, - Closure|null $when = null, + ?Closure $when = null, ) { parent::__construct( $format, diff --git a/src/Rule/Date/Time.php b/src/Rule/Date/Time.php index 8078d7bf8..8aaa7adf8 100644 --- a/src/Rule/Date/Time.php +++ b/src/Rule/Date/Time.php @@ -132,7 +132,7 @@ public function __construct( ?string $tooLateMessage = null, bool|callable|null $skipOnEmpty = null, bool $skipOnError = false, - Closure|null $when = null, + ?Closure $when = null, ) { parent::__construct( $format, diff --git a/src/Rule/Each.php b/src/Rule/Each.php index 746837226..0e4938e2d 100644 --- a/src/Rule/Each.php +++ b/src/Rule/Each.php @@ -121,11 +121,11 @@ final class Each implements public function __construct( callable|iterable|object|string $rules = [], private string $incorrectInputMessage = '{Property} must be array or iterable. {type} given.', - private string $incorrectInputKeyMessage = 'Every iterable key of {property} must have an integer or a ' . - 'string type. {type} given.', + private string $incorrectInputKeyMessage = 'Every iterable key of {property} must have an integer or a ' + . 'string type. {type} given.', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, public readonly bool $stopOnError = false, ) { $this->rules = RulesNormalizer::normalize($rules); diff --git a/src/Rule/Email.php b/src/Rule/Email.php index daa100c21..5e71d4fd5 100644 --- a/src/Rule/Email.php +++ b/src/Rule/Email.php @@ -98,7 +98,7 @@ public function __construct( private string $message = '{Property} is not a valid email address.', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { if ($pattern === '') { throw new InvalidArgumentException('Pattern can\'t be empty.'); diff --git a/src/Rule/EmailHandler.php b/src/Rule/EmailHandler.php index ed5fc80b1..d73404269 100644 --- a/src/Rule/EmailHandler.php +++ b/src/Rule/EmailHandler.php @@ -36,7 +36,7 @@ public function validate(mixed $value, RuleInterface $rule, ValidationContext $c if (!preg_match( '/^(?P(?:"?([^"]*)"?\s)?)(?:\s+)?((?P.+)@(?P[^>]+))(?P>?))/', $value, - $matches + $matches, )) { $valid = $rule->isIdnEnabled() ? (bool) preg_match($rule->getIdnEmailPattern(), $value) diff --git a/src/Rule/Equal.php b/src/Rule/Equal.php index 8e09b44ee..f7499e44b 100644 --- a/src/Rule/Equal.php +++ b/src/Rule/Equal.php @@ -91,12 +91,12 @@ public function __construct( ?string $targetProperty = null, string $incorrectInputMessage = self::DEFAULT_INCORRECT_INPUT_MESSAGE, string $incorrectDataSetTypeMessage = self::DEFAULT_INCORRECT_DATA_SET_TYPE_MESSAGE, - string|null $message = null, + ?string $message = null, string $type = CompareType::NUMBER, bool $strict = false, bool|callable|null $skipOnEmpty = false, bool $skipOnError = false, - Closure|null $when = null, + ?Closure $when = null, ) { parent::__construct( targetValue: $targetValue, diff --git a/src/Rule/FilledAtLeast.php b/src/Rule/FilledAtLeast.php index 384e89a12..9baddda8c 100644 --- a/src/Rule/FilledAtLeast.php +++ b/src/Rule/FilledAtLeast.php @@ -68,7 +68,7 @@ public function __construct( private string $message = 'At least {min, number} {min, plural, one{property} other{properties}} from this list must be filled for {property}: {properties}.', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null + private ?Closure $when = null, ) { if ($min > count($this->properties)) { throw new InvalidArgumentException('$min must be no greater than amount of $properties.'); diff --git a/src/Rule/FilledOnlyOneOf.php b/src/Rule/FilledOnlyOneOf.php index 56330e00c..969a97fb3 100644 --- a/src/Rule/FilledOnlyOneOf.php +++ b/src/Rule/FilledOnlyOneOf.php @@ -62,7 +62,7 @@ public function __construct( private string $message = 'Exactly 1 property from this list must be filled for {property}: {properties}.', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null + private ?Closure $when = null, ) { $this->skipOnEmpty = $skipOnEmpty; } diff --git a/src/Rule/GreaterThan.php b/src/Rule/GreaterThan.php index 17b3550aa..ec06be83d 100644 --- a/src/Rule/GreaterThan.php +++ b/src/Rule/GreaterThan.php @@ -86,14 +86,14 @@ final class GreaterThan extends AbstractCompare */ public function __construct( mixed $targetValue = null, - string|null $targetProperty = null, + ?string $targetProperty = null, string $incorrectInputMessage = self::DEFAULT_INCORRECT_INPUT_MESSAGE, string $incorrectDataSetTypeMessage = self::DEFAULT_INCORRECT_DATA_SET_TYPE_MESSAGE, - string|null $message = null, + ?string $message = null, string $type = CompareType::NUMBER, bool|callable|null $skipOnEmpty = false, bool $skipOnError = false, - Closure|null $when = null, + ?Closure $when = null, ) { parent::__construct( targetValue: $targetValue, diff --git a/src/Rule/GreaterThanOrEqual.php b/src/Rule/GreaterThanOrEqual.php index 02ca1197f..24924b4b0 100644 --- a/src/Rule/GreaterThanOrEqual.php +++ b/src/Rule/GreaterThanOrEqual.php @@ -87,14 +87,14 @@ final class GreaterThanOrEqual extends AbstractCompare */ public function __construct( mixed $targetValue = null, - string|null $targetProperty = null, + ?string $targetProperty = null, string $incorrectInputMessage = self::DEFAULT_INCORRECT_INPUT_MESSAGE, string $incorrectDataSetTypeMessage = self::DEFAULT_INCORRECT_DATA_SET_TYPE_MESSAGE, - string|null $message = null, + ?string $message = null, string $type = CompareType::NUMBER, bool|callable|null $skipOnEmpty = false, bool $skipOnError = false, - Closure|null $when = null, + ?Closure $when = null, ) { parent::__construct( targetValue: $targetValue, diff --git a/src/Rule/Image/CompositeImageInfoProvider.php b/src/Rule/Image/CompositeImageInfoProvider.php index 5e7cbd75e..3b5c817f0 100644 --- a/src/Rule/Image/CompositeImageInfoProvider.php +++ b/src/Rule/Image/CompositeImageInfoProvider.php @@ -12,7 +12,7 @@ final class CompositeImageInfoProvider implements ImageInfoProviderInterface private readonly array $providers; public function __construct( - ImageInfoProviderInterface ...$providers + ImageInfoProviderInterface ...$providers, ) { $this->providers = $providers; } diff --git a/src/Rule/Image/Image.php b/src/Rule/Image/Image.php index 92806c4d7..fcf46a36a 100644 --- a/src/Rule/Image/Image.php +++ b/src/Rule/Image/Image.php @@ -132,7 +132,7 @@ public function __construct( private string $invalidAspectRatioMessage = 'The aspect ratio of {property} must be {aspectRatioWidth, number}:{aspectRatioHeight, number} with margin {aspectRatioMargin, number}%.', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { if ($this->width !== null && ($this->minWidth !== null || $this->maxWidth !== null)) { throw new InvalidArgumentException('Exact width and min / max width can\'t be specified together.'); diff --git a/src/Rule/Image/ImageAspectRatio.php b/src/Rule/Image/ImageAspectRatio.php index 9e0df1995..f236e3d18 100644 --- a/src/Rule/Image/ImageAspectRatio.php +++ b/src/Rule/Image/ImageAspectRatio.php @@ -25,8 +25,7 @@ public function __construct( private readonly int $width, private readonly int $height, private readonly float $margin = 0, - ) { - } + ) {} public function getWidth(): int { diff --git a/src/Rule/Image/ImageHandler.php b/src/Rule/Image/ImageHandler.php index 451fa515c..a82251bc7 100644 --- a/src/Rule/Image/ImageHandler.php +++ b/src/Rule/Image/ImageHandler.php @@ -171,8 +171,8 @@ private function validateAspectRatio( $absoluteMargin = $rule->getAspectRatio()->getMargin() / 100; if ( - ($validatedAspectRatio < $expectedAspectRatio - $absoluteMargin) || - ($validatedAspectRatio > $expectedAspectRatio + $absoluteMargin) + ($validatedAspectRatio < $expectedAspectRatio - $absoluteMargin) + || ($validatedAspectRatio > $expectedAspectRatio + $absoluteMargin) ) { $result->addError( $rule->getInvalidAspectRatioMessage(), diff --git a/src/Rule/Image/ImageInfo.php b/src/Rule/Image/ImageInfo.php index ee08bed91..5f1c9dbe3 100644 --- a/src/Rule/Image/ImageInfo.php +++ b/src/Rule/Image/ImageInfo.php @@ -9,8 +9,7 @@ final class ImageInfo public function __construct( private readonly int $width, private readonly int $height, - ) { - } + ) {} public function getWidth(): int { diff --git a/src/Rule/In.php b/src/Rule/In.php index 3d7100005..57558ace0 100644 --- a/src/Rule/In.php +++ b/src/Rule/In.php @@ -76,7 +76,7 @@ public function __construct( private string $message = '{Property} is not in the list of acceptable values.', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { $this->skipOnEmpty = $skipOnEmpty; } diff --git a/src/Rule/InEnum.php b/src/Rule/InEnum.php index 8f09dc9b7..993edeae4 100644 --- a/src/Rule/InEnum.php +++ b/src/Rule/InEnum.php @@ -75,13 +75,13 @@ public function __construct( private string $message = '{Property} is not in the list of acceptable values.', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { $this->skipOnEmpty = $skipOnEmpty; if (!is_subclass_of($this->class, UnitEnum::class)) { throw new InvalidArgumentException( - sprintf('Class should be an enum class string, %s provided.', get_debug_type($this->class)) + sprintf('Class should be an enum class string, %s provided.', get_debug_type($this->class)), ); } } diff --git a/src/Rule/Integer.php b/src/Rule/Integer.php index 1e2191c0c..3e4a50bed 100644 --- a/src/Rule/Integer.php +++ b/src/Rule/Integer.php @@ -80,7 +80,7 @@ public function __construct( string $pattern = '/^\s*[+-]?\d+\s*$/', bool|callable|null $skipOnEmpty = null, bool $skipOnError = false, - Closure|null $when = null, + ?Closure $when = null, ) { parent::__construct( min: $min, diff --git a/src/Rule/Ip.php b/src/Rule/Ip.php index 5d4ab210e..3fe885292 100644 --- a/src/Rule/Ip.php +++ b/src/Rule/Ip.php @@ -169,7 +169,7 @@ public function __construct( array $ranges = [], bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { if (!$this->allowIpv4 && !$this->allowIpv6) { throw new InvalidArgumentException('Both IPv4 and IPv6 checks can not be disabled at the same time.'); diff --git a/src/Rule/IpHandler.php b/src/Rule/IpHandler.php index f8a6963d8..8fe4fa7f3 100644 --- a/src/Rule/IpHandler.php +++ b/src/Rule/IpHandler.php @@ -74,9 +74,9 @@ public function validate(mixed $value, RuleInterface $rule, ValidationContext $c */ private static function getIpParsePattern(): string { - return '/^(?' . - IpRanges::NEGATION_CHARACTER . - ')?(?(?(?:' . IpHelper::IPV4_PATTERN . ')|(?:' . IpHelper::IPV6_PATTERN . '))(?:\/(?-?\d+))?)$/'; + return '/^(?' + . IpRanges::NEGATION_CHARACTER + . ')?(?(?(?:' . IpHelper::IPV4_PATTERN . ')|(?:' . IpHelper::IPV6_PATTERN . '))(?:\/(?-?\d+))?)$/'; } /** @@ -95,8 +95,8 @@ private static function validateValueParts( ?string $cidr, bool $negation, string $value, - ValidationContext $context - ): Result|null { + ValidationContext $context, + ): ?Result { if ($cidr === null && $rule->isSubnetRequired()) { return self::getGenericErrorResult($rule->getNoSubnetMessage(), $context, $value); } @@ -126,8 +126,8 @@ private static function validateVersion( Ip $rule, int $ipVersion, string $value, - ValidationContext $context - ): Result|null { + ValidationContext $context, + ): ?Result { if ($ipVersion === IpHelper::IPV6 && !$rule->isIpv6Allowed()) { return self::getGenericErrorResult($rule->getIpv6NotAllowedMessage(), $context, $value); } @@ -155,8 +155,8 @@ private static function validateCidr( ?string $cidr, string $ipCidr, string $value, - ValidationContext $context - ): Result|null { + ValidationContext $context, + ): ?Result { if ($cidr !== null) { try { IpHelper::getCidrBits($ipCidr); diff --git a/src/Rule/Json.php b/src/Rule/Json.php index 1059e4db0..eca57eccb 100644 --- a/src/Rule/Json.php +++ b/src/Rule/Json.php @@ -60,7 +60,7 @@ public function __construct( private string $message = '{Property} is not a valid JSON.', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { $this->skipOnEmpty = $skipOnEmpty; } diff --git a/src/Rule/Length.php b/src/Rule/Length.php index 249992ee8..1c0155c80 100644 --- a/src/Rule/Length.php +++ b/src/Rule/Length.php @@ -86,20 +86,20 @@ final class Length implements * @psalm-param WhenType $when */ public function __construct( - int|null $exactly = null, - int|null $min = null, - int|null $max = null, + ?int $exactly = null, + ?int $min = null, + ?int $max = null, private string $incorrectInputMessage = '{Property} must be a string. {type} given.', - string $lessThanMinMessage = '{Property} must contain at least {min, number} {min, plural, one{character} ' . - 'other{characters}}.', - string $greaterThanMaxMessage = '{Property} must contain at most {max, number} {max, plural, one{character} ' . - 'other{characters}}.', - string $notExactlyMessage = '{Property} must contain exactly {exactly, number} {exactly, plural, ' . - 'one{character} other{characters}}.', + string $lessThanMinMessage = '{Property} must contain at least {min, number} {min, plural, one{character} ' + . 'other{characters}}.', + string $greaterThanMaxMessage = '{Property} must contain at most {max, number} {max, plural, one{character} ' + . 'other{characters}}.', + string $notExactlyMessage = '{Property} must contain exactly {exactly, number} {exactly, plural, ' + . 'one{character} other{characters}}.', private string $encoding = 'UTF-8', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null + private ?Closure $when = null, ) { $this->initCountableLimitProperties( $min, @@ -107,7 +107,7 @@ public function __construct( $exactly, $lessThanMinMessage, $greaterThanMaxMessage, - $notExactlyMessage + $notExactlyMessage, ); $this->skipOnEmpty = $skipOnEmpty; } diff --git a/src/Rule/LessThan.php b/src/Rule/LessThan.php index 56caf54b3..47e8ddc51 100644 --- a/src/Rule/LessThan.php +++ b/src/Rule/LessThan.php @@ -86,14 +86,14 @@ final class LessThan extends AbstractCompare */ public function __construct( mixed $targetValue = null, - string|null $targetProperty = null, + ?string $targetProperty = null, string $incorrectInputMessage = self::DEFAULT_INCORRECT_INPUT_MESSAGE, string $incorrectDataSetTypeMessage = self::DEFAULT_INCORRECT_DATA_SET_TYPE_MESSAGE, - string|null $message = null, + ?string $message = null, string $type = CompareType::NUMBER, bool|callable|null $skipOnEmpty = false, bool $skipOnError = false, - Closure|null $when = null, + ?Closure $when = null, ) { parent::__construct( targetValue: $targetValue, diff --git a/src/Rule/LessThanOrEqual.php b/src/Rule/LessThanOrEqual.php index 8788fe027..a3641df20 100644 --- a/src/Rule/LessThanOrEqual.php +++ b/src/Rule/LessThanOrEqual.php @@ -86,14 +86,14 @@ final class LessThanOrEqual extends AbstractCompare */ public function __construct( mixed $targetValue = null, - string|null $targetProperty = null, + ?string $targetProperty = null, string $incorrectInputMessage = self::DEFAULT_INCORRECT_INPUT_MESSAGE, string $incorrectDataSetTypeMessage = self::DEFAULT_INCORRECT_DATA_SET_TYPE_MESSAGE, - string|null $message = null, + ?string $message = null, string $type = CompareType::NUMBER, bool|callable|null $skipOnEmpty = false, bool $skipOnError = false, - Closure|null $when = null, + ?Closure $when = null, ) { parent::__construct( targetValue: $targetValue, diff --git a/src/Rule/Nested.php b/src/Rule/Nested.php index fe1e5db25..e72c4b33a 100644 --- a/src/Rule/Nested.php +++ b/src/Rule/Nested.php @@ -128,7 +128,7 @@ final class Nested implements * * @psalm-var NormalizedNestedRulesArray|null */ - private array|null $rules; + private ?array $rules; /** * @param iterable|object|string|null $rules Rules for validating nested structure. The following types are @@ -214,10 +214,10 @@ public function __construct( private int $rulesSourceClassPropertyVisibility = ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PUBLIC, - private string $noRulesWithNoObjectMessage = 'Nested rule without rules requires {property} to be an object. ' . - '{type} given.', - private string $incorrectDataSetTypeMessage = 'An object data set data for {property} can only have an array ' . - 'type. {type} given.', + private string $noRulesWithNoObjectMessage = 'Nested rule without rules requires {property} to be an object. ' + . '{type} given.', + private string $incorrectDataSetTypeMessage = 'An object data set data for {property} can only have an array ' + . 'type. {type} given.', private string $incorrectInputMessage = '{Property} must be an array or an object. {type} given.', private bool $requirePropertyPath = false, private string $noPropertyPathMessage = 'Property "{path}" is not found in {property}.', @@ -225,7 +225,7 @@ public function __construct( private bool $propagateOptions = false, bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { $this->skipOnEmpty = $skipOnEmpty; $this->prepareRules($rules); @@ -243,7 +243,7 @@ public function getName(): string * * @psalm-return NormalizedNestedRulesArray */ - public function getRules(): array|null + public function getRules(): ?array { return $this->rules; } @@ -327,6 +327,84 @@ public function getNoPropertyPathMessage(): string return $this->noPropertyPathMessage; } + public function propagateOptions(): void + { + if ($this->rules === null) { + return; + } + + $rules = []; + foreach ($this->rules as $attributeRulesIndex => $attributeRules) { + $rules[$attributeRulesIndex] = is_iterable($attributeRules) + ? PropagateOptionsHelper::propagate($this, $attributeRules) + : PropagateOptionsHelper::propagateToRule($this, $attributeRules); + } + + $this->rules = $rules; + } + + public function afterInitAttribute(object $object): void + { + if ($this->rules === null) { + return; + } + + foreach ($this->rules as $rules) { + if (is_array($rules)) { + foreach ($rules as $rule) { + if ($rule instanceof AfterInitAttributeEventInterface) { + $rule->afterInitAttribute($object); + } + } + } else { + if ($rules instanceof AfterInitAttributeEventInterface) { + $rules->afterInitAttribute($object); + } + } + } + } + + #[ArrayShape([ + 'requirePropertyPath' => 'bool', + 'noRulesWithNoObjectMessage' => 'array', + 'incorrectDataSetTypeMessage' => 'array', + 'incorrectInputMessage' => 'array', + 'noPropertyPathMessage' => 'array', + 'skipOnEmpty' => 'bool', + 'skipOnError' => 'bool', + 'rules' => 'array|null', + ])] + public function getOptions(): array + { + return [ + 'noRulesWithNoObjectMessage' => [ + 'template' => $this->noRulesWithNoObjectMessage, + 'parameters' => [], + ], + 'incorrectDataSetTypeMessage' => [ + 'template' => $this->incorrectDataSetTypeMessage, + 'parameters' => [], + ], + 'incorrectInputMessage' => [ + 'template' => $this->incorrectInputMessage, + 'parameters' => [], + ], + 'noPropertyPathMessage' => [ + 'template' => $this->getNoPropertyPathMessage(), + 'parameters' => [], + ], + 'requirePropertyPath' => $this->isPropertyPathRequired(), + 'skipOnEmpty' => $this->getSkipOnEmptyOption(), + 'skipOnError' => $this->skipOnError, + 'rules' => $this->rules === null ? null : RulesDumper::asArray($this->rules), + ]; + } + + public function getHandler(): string + { + return NestedHandler::class; + } + /** * Prepares raw rules passed in the constructor for usage in handler. As a result, {@see $rules} property will * contain ready to use rules. @@ -351,8 +429,8 @@ private function prepareRules(iterable|object|string|null $source): void $rules = $source; } else { throw new InvalidArgumentException( - 'The $rules argument passed to Nested rule can be either: a null, an object implementing ' . - 'RulesProviderInterface, a class string or an iterable.' + 'The $rules argument passed to Nested rule can be either: a null, an object implementing ' + . 'RulesProviderInterface, a class string or an iterable.', ); } @@ -461,7 +539,7 @@ private static function ensureArrayHasRules(iterable &$rules): void $message = sprintf( 'Every rule must be an instance of %s or a callable, %s given.', RuleInterface::class, - get_debug_type($rule) + get_debug_type($rule), ); throw new InvalidArgumentException($message); @@ -490,7 +568,7 @@ private function handleEachShortcut(array &$rules): void $parts = StringHelper::parsePath( (string) $valuePath, delimiter: self::EACH_SHORTCUT, - preserveDelimiterEscaping: true + preserveDelimiterEscaping: true, ); if (count($parts) === 1) { continue; @@ -536,82 +614,4 @@ private function handleEachShortcut(array &$rules): void } } } - - public function propagateOptions(): void - { - if ($this->rules === null) { - return; - } - - $rules = []; - foreach ($this->rules as $attributeRulesIndex => $attributeRules) { - $rules[$attributeRulesIndex] = is_iterable($attributeRules) - ? PropagateOptionsHelper::propagate($this, $attributeRules) - : PropagateOptionsHelper::propagateToRule($this, $attributeRules); - } - - $this->rules = $rules; - } - - public function afterInitAttribute(object $object): void - { - if ($this->rules === null) { - return; - } - - foreach ($this->rules as $rules) { - if (is_array($rules)) { - foreach ($rules as $rule) { - if ($rule instanceof AfterInitAttributeEventInterface) { - $rule->afterInitAttribute($object); - } - } - } else { - if ($rules instanceof AfterInitAttributeEventInterface) { - $rules->afterInitAttribute($object); - } - } - } - } - - #[ArrayShape([ - 'requirePropertyPath' => 'bool', - 'noRulesWithNoObjectMessage' => 'array', - 'incorrectDataSetTypeMessage' => 'array', - 'incorrectInputMessage' => 'array', - 'noPropertyPathMessage' => 'array', - 'skipOnEmpty' => 'bool', - 'skipOnError' => 'bool', - 'rules' => 'array|null', - ])] - public function getOptions(): array - { - return [ - 'noRulesWithNoObjectMessage' => [ - 'template' => $this->noRulesWithNoObjectMessage, - 'parameters' => [], - ], - 'incorrectDataSetTypeMessage' => [ - 'template' => $this->incorrectDataSetTypeMessage, - 'parameters' => [], - ], - 'incorrectInputMessage' => [ - 'template' => $this->incorrectInputMessage, - 'parameters' => [], - ], - 'noPropertyPathMessage' => [ - 'template' => $this->getNoPropertyPathMessage(), - 'parameters' => [], - ], - 'requirePropertyPath' => $this->isPropertyPathRequired(), - 'skipOnEmpty' => $this->getSkipOnEmptyOption(), - 'skipOnError' => $this->skipOnError, - 'rules' => $this->rules === null ? null : RulesDumper::asArray($this->rules), - ]; - } - - public function getHandler(): string - { - return NestedHandler::class; - } } diff --git a/src/Rule/NestedHandler.php b/src/Rule/NestedHandler.php index 0d7bd406a..2c6242a94 100644 --- a/src/Rule/NestedHandler.php +++ b/src/Rule/NestedHandler.php @@ -95,7 +95,7 @@ public function validate(mixed $value, RuleInterface $rule, ValidationContext $c $property = end($valuePathList); $itemResult = $context->validate( ArrayHelper::keyExists($data, $valuePathList) ? [$property => $validatedValue] : [], - [$property => $rules] + [$property => $rules], ); } @@ -111,7 +111,7 @@ public function validate(mixed $value, RuleInterface $rule, ValidationContext $c $compoundResult->addErrorWithoutPostProcessing( $error->getMessage(), $error->getParameters(), - $valuePathList + $valuePathList, ); } } diff --git a/src/Rule/NotEqual.php b/src/Rule/NotEqual.php index d55d02b3e..dbf1e37bf 100644 --- a/src/Rule/NotEqual.php +++ b/src/Rule/NotEqual.php @@ -88,15 +88,15 @@ final class NotEqual extends AbstractCompare */ public function __construct( mixed $targetValue = null, - string|null $targetProperty = null, + ?string $targetProperty = null, string $incorrectInputMessage = self::DEFAULT_INCORRECT_INPUT_MESSAGE, string $incorrectDataSetTypeMessage = self::DEFAULT_INCORRECT_DATA_SET_TYPE_MESSAGE, - string|null $message = null, + ?string $message = null, string $type = CompareType::NUMBER, bool $strict = false, bool|callable|null $skipOnEmpty = false, bool $skipOnError = false, - Closure|null $when = null, + ?Closure $when = null, ) { parent::__construct( targetValue: $targetValue, diff --git a/src/Rule/Number.php b/src/Rule/Number.php index f0823603f..14f89287d 100644 --- a/src/Rule/Number.php +++ b/src/Rule/Number.php @@ -80,7 +80,7 @@ public function __construct( string $pattern = '/^\s*[-+]?\d*\.?\d+([eE][-+]?\d+)?\s*$/', bool|callable|null $skipOnEmpty = null, bool $skipOnError = false, - Closure|null $when = null, + ?Closure $when = null, ) { parent::__construct( min: $min, diff --git a/src/Rule/Regex.php b/src/Rule/Regex.php index d71fd01c1..3f0101c19 100644 --- a/src/Rule/Regex.php +++ b/src/Rule/Regex.php @@ -73,7 +73,7 @@ public function __construct( private string $message = '{Property} is invalid.', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { if ($pattern === '') { throw new InvalidArgumentException('Pattern can\'t be empty.'); diff --git a/src/Rule/RegexHandler.php b/src/Rule/RegexHandler.php index 383ac6e5c..182f94fcb 100644 --- a/src/Rule/RegexHandler.php +++ b/src/Rule/RegexHandler.php @@ -35,8 +35,8 @@ public function validate(mixed $value, RuleInterface $rule, ValidationContext $c } if ( - (!$rule->isNot() && !preg_match($rule->getPattern(), $value)) || - ($rule->isNot() && preg_match($rule->getPattern(), $value)) + (!$rule->isNot() && !preg_match($rule->getPattern(), $value)) + || ($rule->isNot() && preg_match($rule->getPattern(), $value)) ) { $result->addError($rule->getMessage(), [ 'property' => $context->getTranslatedProperty(), diff --git a/src/Rule/Required.php b/src/Rule/Required.php index 774b11651..42f9df2ac 100644 --- a/src/Rule/Required.php +++ b/src/Rule/Required.php @@ -78,9 +78,9 @@ final class Required implements DumpedRuleInterface, SkipOnErrorInterface, WhenI public function __construct( private string $message = '{Property} cannot be blank.', private string $notPassedMessage = '{Property} not passed.', - callable|null $emptyCondition = null, + ?callable $emptyCondition = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { $this->emptyCondition = $emptyCondition; } diff --git a/src/Rule/RequiredHandler.php b/src/Rule/RequiredHandler.php index 07996c3a9..624db2113 100644 --- a/src/Rule/RequiredHandler.php +++ b/src/Rule/RequiredHandler.php @@ -43,7 +43,7 @@ final class RequiredHandler implements RuleHandlerInterface * @psalm-param EmptyConditionType|null $defaultEmptyCondition */ public function __construct( - callable|null $defaultEmptyCondition = null, + ?callable $defaultEmptyCondition = null, ) { $this->defaultEmptyCondition = $defaultEmptyCondition ?? new WhenEmpty(trimString: true); } diff --git a/src/Rule/StopOnError.php b/src/Rule/StopOnError.php index 7f0b6ce50..e46d1d791 100644 --- a/src/Rule/StopOnError.php +++ b/src/Rule/StopOnError.php @@ -87,7 +87,7 @@ public function __construct( iterable $rules, bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { $this->rules = RulesNormalizer::normalizeList($rules); $this->skipOnEmpty = $skipOnEmpty; diff --git a/src/Rule/StringValue.php b/src/Rule/StringValue.php index 49dbcb1ae..0908c9620 100644 --- a/src/Rule/StringValue.php +++ b/src/Rule/StringValue.php @@ -57,7 +57,7 @@ public function __construct( private string $message = '{Property} must be a string.', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { $this->skipOnEmpty = $skipOnEmpty; } diff --git a/src/Rule/Subset.php b/src/Rule/Subset.php index 7b649e0dc..4a248c54d 100644 --- a/src/Rule/Subset.php +++ b/src/Rule/Subset.php @@ -78,7 +78,7 @@ public function __construct( private string $message = '{Property} is not a subset of acceptable values.', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { $this->skipOnEmpty = $skipOnEmpty; } diff --git a/src/Rule/Trait/CountableLimitHandlerTrait.php b/src/Rule/Trait/CountableLimitHandlerTrait.php index 1ebda3213..8717b334a 100644 --- a/src/Rule/Trait/CountableLimitHandlerTrait.php +++ b/src/Rule/Trait/CountableLimitHandlerTrait.php @@ -30,7 +30,7 @@ private function validateCountableLimits( CountableLimitInterface|RuleInterface $rule, ValidationContext $context, int $number, - Result $result + Result $result, ): void { if (!$rule instanceof CountableLimitInterface || !$rule instanceof RuleInterface) { throw new InvalidArgumentException('$rule must implement both LimitInterface and RuleInterface.'); diff --git a/src/Rule/Trait/CountableLimitTrait.php b/src/Rule/Trait/CountableLimitTrait.php index adc91565d..2b1eb7eae 100644 --- a/src/Rule/Trait/CountableLimitTrait.php +++ b/src/Rule/Trait/CountableLimitTrait.php @@ -56,13 +56,13 @@ trait CountableLimitTrait * * @see $lessThanMinMessage for related error message. */ - private int|null $min = null; + private ?int $min = null; /** * @var int|null Maximum limit. Can't be combined with {@see $exactly}. * * @see $greaterThanMaxMessage for related error message. */ - private int|null $max = null; + private ?int $max = null; /** * @var int|null "Exactly" number. A shortcut / replacement for the case when {@see $min} and {@see $max} have the * same not-null value. Mutually exclusive with both {@see $min} and {@see $max}. `null` means no strict comparison @@ -70,7 +70,7 @@ trait CountableLimitTrait * * @see $notExactlyMessage for related error message. */ - private int|null $exactly = null; + private ?int $exactly = null; /** * @var string Validation error message used when a validated value is less than minimum set in {@see $min}. */ @@ -85,68 +85,12 @@ trait CountableLimitTrait */ private string $notExactlyMessage; - /** - * Initializes countable limit related properties and runs checks for required, mutually exclusive properties and - * their allowed values (including dependency on each other). - * - * @param int|null $min Minimum limit ({@see $min}). - * @param int|null $max Maximum limit ({@see $max}). - * @param int|null $exactly "Exactly" number ({@see $exactly}). - * @param string $lessThanMinMessage "Less than minimum" validation error message ({@see $lessThanMinMessage}). - * @param string $greaterThanMinMessage "Greater than maximum" validation error message - * ({@see $greaterThanMinMessage}). - * @param string $notExactlyMessage "Not exactly" validation error message ({@see $notExactlyMessage}). - */ - private function initCountableLimitProperties( - int|null $min, - int|null $max, - int|null $exactly, - string $lessThanMinMessage, - string $greaterThanMinMessage, - string $notExactlyMessage - ): void { - $this->min = $min; - $this->max = $max; - $this->exactly = $exactly; - $this->lessThanMinMessage = $lessThanMinMessage; - $this->greaterThanMaxMessage = $greaterThanMinMessage; - $this->notExactlyMessage = $notExactlyMessage; - - if ($this->min === null && $this->max === null && $this->exactly === null) { - throw new InvalidArgumentException( - 'At least one of these properties must be specified: $min, $max, $exactly.' - ); - } - - if (($this->min !== null || $this->max !== null) && $this->exactly !== null) { - throw new InvalidArgumentException('$exactly is mutually exclusive with $min and $max.'); - } - - if ( - ($this->min !== null && $this->min < 0) || - ($this->max !== null && $this->max < 0) || - ($this->exactly !== null && $this->exactly < 0) - ) { - throw new InvalidArgumentException('Only positive or zero values are allowed.'); - } - - if ($this->min !== null && $this->max !== null) { - if ($this->min > $this->max) { - throw new InvalidArgumentException('$min must be lower than $max.'); - } - - if ($this->min === $this->max) { - throw new InvalidArgumentException('Use $exactly instead.'); - } - } - } - /** * A getter for {@see $min} property. * * @return int|null A number representing minimum boundary. `null` means no lower bound. */ - public function getMin(): int|null + public function getMin(): ?int { return $this->min; } @@ -156,7 +100,7 @@ public function getMin(): int|null * * @return int|null A number representing maximum boundary. `null` means no upper bound. */ - public function getMax(): int|null + public function getMax(): ?int { return $this->max; } @@ -167,7 +111,7 @@ public function getMax(): int|null * @return int|null A number representing "exactly" value. `null` means no strict comparison so lower / upper limits / * both must be set. */ - public function getExactly(): int|null + public function getExactly(): ?int { return $this->exactly; } @@ -202,6 +146,62 @@ public function getNotExactlyMessage(): string return $this->notExactlyMessage; } + /** + * Initializes countable limit related properties and runs checks for required, mutually exclusive properties and + * their allowed values (including dependency on each other). + * + * @param int|null $min Minimum limit ({@see $min}). + * @param int|null $max Maximum limit ({@see $max}). + * @param int|null $exactly "Exactly" number ({@see $exactly}). + * @param string $lessThanMinMessage "Less than minimum" validation error message ({@see $lessThanMinMessage}). + * @param string $greaterThanMinMessage "Greater than maximum" validation error message + * ({@see $greaterThanMinMessage}). + * @param string $notExactlyMessage "Not exactly" validation error message ({@see $notExactlyMessage}). + */ + private function initCountableLimitProperties( + ?int $min, + ?int $max, + ?int $exactly, + string $lessThanMinMessage, + string $greaterThanMinMessage, + string $notExactlyMessage, + ): void { + $this->min = $min; + $this->max = $max; + $this->exactly = $exactly; + $this->lessThanMinMessage = $lessThanMinMessage; + $this->greaterThanMaxMessage = $greaterThanMinMessage; + $this->notExactlyMessage = $notExactlyMessage; + + if ($this->min === null && $this->max === null && $this->exactly === null) { + throw new InvalidArgumentException( + 'At least one of these properties must be specified: $min, $max, $exactly.', + ); + } + + if (($this->min !== null || $this->max !== null) && $this->exactly !== null) { + throw new InvalidArgumentException('$exactly is mutually exclusive with $min and $max.'); + } + + if ( + ($this->min !== null && $this->min < 0) + || ($this->max !== null && $this->max < 0) + || ($this->exactly !== null && $this->exactly < 0) + ) { + throw new InvalidArgumentException('Only positive or zero values are allowed.'); + } + + if ($this->min !== null && $this->max !== null) { + if ($this->min > $this->max) { + throw new InvalidArgumentException('$min must be lower than $max.'); + } + + if ($this->min === $this->max) { + throw new InvalidArgumentException('Use $exactly instead.'); + } + } + } + /** * Limit related options intended to be merged with other rule options. * diff --git a/src/Rule/Trait/SkipOnEmptyTrait.php b/src/Rule/Trait/SkipOnEmptyTrait.php index 989b74e51..0aa1d3769 100644 --- a/src/Rule/Trait/SkipOnEmptyTrait.php +++ b/src/Rule/Trait/SkipOnEmptyTrait.php @@ -73,7 +73,7 @@ public function getSkipOnEmpty(): bool|callable|null * - `false` - do not skip an empty value. * - `null` - unable to determine because the callable was initially passed. */ - private function getSkipOnEmptyOption(): bool|null + private function getSkipOnEmptyOption(): ?bool { if (is_bool($this->skipOnEmpty)) { return $this->skipOnEmpty; diff --git a/src/Rule/Trait/WhenTrait.php b/src/Rule/Trait/WhenTrait.php index 748d7628b..2dbb9a88c 100644 --- a/src/Rule/Trait/WhenTrait.php +++ b/src/Rule/Trait/WhenTrait.php @@ -36,7 +36,7 @@ trait WhenTrait * * @return $this The new instance with a changed value. */ - public function when(Closure|null $value): static + public function when(?Closure $value): static { $new = clone $this; $new->when = $value; @@ -53,7 +53,7 @@ public function when(Closure|null $value): static * - `null` - always apply the validation. * - `callable` - apply the validation depending on a return value: `true` - apply, `false` - do not apply. */ - public function getWhen(): Closure|null + public function getWhen(): ?Closure { return $this->when; } diff --git a/src/Rule/TrueValue.php b/src/Rule/TrueValue.php index 781e9745c..621ce5275 100644 --- a/src/Rule/TrueValue.php +++ b/src/Rule/TrueValue.php @@ -78,12 +78,12 @@ final class TrueValue implements DumpedRuleInterface, SkipOnErrorInterface, When public function __construct( private int|float|string|bool $trueValue = '1', private bool $strict = false, - private string $incorrectInputMessage = 'The allowed types for {property} are integer, float, string, ' . - 'boolean. {type} given.', + private string $incorrectInputMessage = 'The allowed types for {property} are integer, float, string, ' + . 'boolean. {type} given.', private string $message = '{Property} must be "{true}".', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { $this->skipOnEmpty = $skipOnEmpty; } diff --git a/src/Rule/Type/BooleanType.php b/src/Rule/Type/BooleanType.php index 1ce1fc294..20fb5100a 100644 --- a/src/Rule/Type/BooleanType.php +++ b/src/Rule/Type/BooleanType.php @@ -53,7 +53,7 @@ public function __construct( private string $message = '{Property} must be a boolean.', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { $this->skipOnEmpty = $skipOnEmpty; } diff --git a/src/Rule/Type/FloatType.php b/src/Rule/Type/FloatType.php index 73c7ec281..3efd57431 100644 --- a/src/Rule/Type/FloatType.php +++ b/src/Rule/Type/FloatType.php @@ -52,7 +52,7 @@ public function __construct( private string $message = '{Property} must be a float.', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { $this->skipOnEmpty = $skipOnEmpty; } diff --git a/src/Rule/Type/IntegerType.php b/src/Rule/Type/IntegerType.php index cb3df8f3f..b26160f38 100644 --- a/src/Rule/Type/IntegerType.php +++ b/src/Rule/Type/IntegerType.php @@ -52,7 +52,7 @@ public function __construct( private string $message = '{Property} must be an integer.', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { $this->skipOnEmpty = $skipOnEmpty; } diff --git a/src/Rule/Type/StringType.php b/src/Rule/Type/StringType.php index 43981faee..6972d5f8f 100644 --- a/src/Rule/Type/StringType.php +++ b/src/Rule/Type/StringType.php @@ -51,7 +51,7 @@ public function __construct( private string $message = '{Property} must be a string. {type} given.', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { $this->skipOnEmpty = $skipOnEmpty; } diff --git a/src/Rule/UniqueIterable.php b/src/Rule/UniqueIterable.php index 16b1a3754..8aceae520 100644 --- a/src/Rule/UniqueIterable.php +++ b/src/Rule/UniqueIterable.php @@ -74,13 +74,13 @@ final class UniqueIterable implements */ public function __construct( private string $incorrectInputMessage = '{Property} must be array or iterable.', - private string $incorrectItemValueMessage = 'The allowed types for iterable\'s item values of {property} are ' . - 'integer, float, string, boolean and object implementing \Stringable or \DateTimeInterface.', + private string $incorrectItemValueMessage = 'The allowed types for iterable\'s item values of {property} are ' + . 'integer, float, string, boolean and object implementing \Stringable or \DateTimeInterface.', private string $differentTypesMessage = 'All iterable items of {property} must have the same type.', private string $message = 'Every iterable\'s item of {property} must be unique.', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { $this->skipOnEmpty = $skipOnEmpty; } diff --git a/src/Rule/Url.php b/src/Rule/Url.php index a60a67cd0..d7d474af4 100644 --- a/src/Rule/Url.php +++ b/src/Rule/Url.php @@ -85,7 +85,7 @@ public function __construct( private string $message = '{Property} is not a valid URL.', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { $pattern = $this->preparePattern($pattern); if ($pattern === '') { diff --git a/src/Rule/UrlHandler.php b/src/Rule/UrlHandler.php index 0c09d6864..405ac8946 100644 --- a/src/Rule/UrlHandler.php +++ b/src/Rule/UrlHandler.php @@ -96,8 +96,8 @@ private function convertIdn(string $value): string */ return preg_replace_callback( '/:\/\/([^\/]+)/', - fn ($matches) => '://' . $this->idnToAscii($matches[1]), - $value + fn($matches) => '://' . $this->idnToAscii($matches[1]), + $value, ); } } diff --git a/src/Rule/Uuid.php b/src/Rule/Uuid.php index 7b408fdb3..05bbc36ca 100644 --- a/src/Rule/Uuid.php +++ b/src/Rule/Uuid.php @@ -49,7 +49,7 @@ public function __construct( private string $message = 'The value of {property} is not a valid UUID.', bool|callable|null $skipOnEmpty = null, private bool $skipOnError = false, - private Closure|null $when = null, + private ?Closure $when = null, ) { $this->skipOnEmpty = $skipOnEmpty; } diff --git a/src/RuleHandlerResolver/RuleHandlerContainer.php b/src/RuleHandlerResolver/RuleHandlerContainer.php index b817dbc5e..78363a05d 100644 --- a/src/RuleHandlerResolver/RuleHandlerContainer.php +++ b/src/RuleHandlerResolver/RuleHandlerContainer.php @@ -37,8 +37,7 @@ public function __construct( * @var ContainerInterface An instance of dependency injection container. */ private readonly ContainerInterface $container, - ) { - } + ) {} /** * Resolves a rule handler name to a corresponding rule handler instance. The actual resolving is delegated to diff --git a/src/ValidationContext.php b/src/ValidationContext.php index c29851cd1..c3e1200e1 100644 --- a/src/ValidationContext.php +++ b/src/ValidationContext.php @@ -80,8 +80,7 @@ final class ValidationContext public function __construct( private array $parameters = [], private ?PropertyTranslatorInterface $propertyTranslator = null, - ) { - } + ) {} /** * Set context data if it is not set yet. @@ -128,7 +127,7 @@ public function setPropertyTranslator(?PropertyTranslatorInterface $propertyTran return $this; } - public function setPropertyLabel(string|null $label): self + public function setPropertyLabel(?string $label): self { $this->propertyLabel = $label; return $this; diff --git a/src/Validator.php b/src/Validator.php index a9f0437e5..c33a28e6c 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -120,7 +120,7 @@ public function withDefaultSkipOnEmptyCondition(bool|callable|null $value): stat public function validate( mixed $data, callable|iterable|object|string|null $rules = null, - ?ValidationContext $context = null + ?ValidationContext $context = null, ): Result { $dataSet = DataSetNormalizer::normalize($data); $originalData = $dataSet instanceof DataWrapperInterface ? $dataSet->getSource() : $data; @@ -128,11 +128,11 @@ public function validate( $rules = RulesNormalizer::normalize( $rules, $dataSet, - $this->defaultSkipOnEmptyCondition + $this->defaultSkipOnEmptyCondition, ); - $defaultPropertyTranslator = - ($dataSet instanceof PropertyTranslatorProviderInterface ? $dataSet->getPropertyTranslator() : null) + $defaultPropertyTranslator + = ($dataSet instanceof PropertyTranslatorProviderInterface ? $dataSet->getPropertyTranslator() : null) ?? $this->defaultPropertyTranslator; $context ??= new ValidationContext(); @@ -167,7 +167,7 @@ public function validate( $result->addErrorWithoutPostProcessing( $this->messageProcessor->process($error), $error->getParameters(), - $error->getValuePath() + $error->getValuePath(), ); } } @@ -222,12 +222,12 @@ private function validateInternal(mixed $value, iterable $rules, ValidationConte Error::MESSAGE_FORMAT => $compoundResult->addErrorWithFormatOnly( $error->getMessage(), $error->getParameters(), - $valuePath + $valuePath, ), default => $compoundResult->addErrorWithoutPostProcessing( $error->getMessage(), $error->getParameters(), - $valuePath + $valuePath, ), }; } @@ -253,8 +253,8 @@ private function validateInternal(mixed $value, iterable $rules, ValidationConte private function shouldSkipRule(RuleInterface $rule, mixed $value, ValidationContext $context): bool { if ( - $rule instanceof SkipOnEmptyInterface && - (SkipOnEmptyNormalizer::normalize($rule->getSkipOnEmpty()))($value, $context->isPropertyMissing()) + $rule instanceof SkipOnEmptyInterface + && (SkipOnEmptyNormalizer::normalize($rule->getSkipOnEmpty()))($value, $context->isPropertyMissing()) ) { return true; } diff --git a/src/WhenInterface.php b/src/WhenInterface.php index e0c0e48ce..936a940e3 100644 --- a/src/WhenInterface.php +++ b/src/WhenInterface.php @@ -43,7 +43,7 @@ interface WhenInterface * * @see ValidationContext for publicly available methods and properties for building condition. */ - public function when(Closure|null $value): static; + public function when(?Closure $value): static; /** * Returns current "when" value. @@ -53,5 +53,5 @@ public function when(Closure|null $value): static; * - `null` - always apply the validation. * - `callable` - apply the validation depending on a return value: `true` - apply, `false` - do not apply. */ - public function getWhen(): Closure|null; + public function getWhen(): ?Closure; } diff --git a/tests/BaseConfigTestCase.php b/tests/BaseConfigTestCase.php index 128b1585e..027e5b8b7 100644 --- a/tests/BaseConfigTestCase.php +++ b/tests/BaseConfigTestCase.php @@ -12,14 +12,14 @@ abstract class BaseConfigTestCase extends TestCase { - final protected function createContainer(array|null $params = null): Container + final protected function createContainer(?array $params = null): Container { $config = ContainerConfig::create()->withDefinitions($this->getContainerDefinitions($params)); return new Container($config); } - final protected function getContainerDefinitions(array|null $params): array + final protected function getContainerDefinitions(?array $params): array { if ($params === null) { $params = $this->getParams(); diff --git a/tests/DataSet/DataSetUsageTest.php b/tests/DataSet/DataSetUsageTest.php index 505e1b277..e8c91b3dd 100644 --- a/tests/DataSet/DataSetUsageTest.php +++ b/tests/DataSet/DataSetUsageTest.php @@ -105,7 +105,7 @@ static function ($value): Result { return $result; }, ], - ] + ], ); $result = (new Validator())->validate($dataObject); @@ -129,7 +129,7 @@ public function testExplicitRulesHavePriority(): void 'age' => [ new Number(max: 25), ], - ] + ], ); $validator = new Validator(); $result = $validator->validate( @@ -138,7 +138,7 @@ public function testExplicitRulesHavePriority(): void 'username' => [ new Length(max: 10), ], - ] + ], ); $this->assertTrue($result->isValid()); @@ -159,12 +159,12 @@ public function testEmptyExplicitRulesHavePriority(): void 'age' => [ new Number(max: 25), ], - ] + ], ); $validator = new Validator(); $result = $validator->validate( $dataSet, - [] + [], ); $this->assertTrue($result->isValid()); diff --git a/tests/DataSet/ObjectDataSetTest.php b/tests/DataSet/ObjectDataSetTest.php index 6e5603588..f648158b0 100644 --- a/tests/DataSet/ObjectDataSetTest.php +++ b/tests/DataSet/ObjectDataSetTest.php @@ -69,7 +69,7 @@ public static function propertyVisibilityDataProvider(): array [ new ObjectDataSet( new ObjectWithDifferentPropertyVisibility(), - ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED + ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED, ), ['name' => '', 'age' => 17], ['name' => '', 'age' => 17, 'number' => null, 'non-exist' => null], @@ -83,7 +83,7 @@ public function testPropertyVisibility( ObjectDataSet $initialDataSet, array $expectedData, array $expectedPropertyValuesMap, - array $expectedRulesKeys + array $expectedRulesKeys, ): void { $dataSets = [ $initialDataSet, @@ -234,25 +234,24 @@ public static function dataCollectRules(): array { return [ [ - new class () { - }, + new class {}, [], ], [ - new class () { + new class { private $property1; }, [], ], [ - new class () { + new class { #[NotRuleAttribute] private $property1; }, [], ], [ - new class () { + new class { #[Required()] private $property1; }, @@ -263,7 +262,7 @@ public static function dataCollectRules(): array ], ], [ - new class () { + new class { use TitleTrait; }, [ @@ -273,7 +272,7 @@ public static function dataCollectRules(): array ], ], [ - new class () { + new class { #[Required()] #[Length(max: 255, skipOnEmpty: true)] private $property1; @@ -293,7 +292,7 @@ public static function dataCollectRules(): array ], ], [ - new class () { + new class { #[Each([ new Required(), new Length(max: 255, skipOnEmpty: true), @@ -312,7 +311,7 @@ public static function dataCollectRules(): array ], ], [ - new class () { + new class { #[Nested([ new Required(), new Length(max: 255, skipOnEmpty: true), @@ -339,7 +338,7 @@ public static function dataCollectRules(): array ], ], [ - new class () { + new class { #[Length(max: 255, skipOnEmpty: true)] #[Length(max: 255, skipOnEmpty: false)] private $property1; @@ -352,7 +351,7 @@ public static function dataCollectRules(): array ], ], [ - new class () { + new class { #[Nested([ new Required(), new Length(max: 255, skipOnEmpty: true), @@ -423,7 +422,7 @@ public function testValidateWithWrongCallbackMethod(): void 'Method "%s" does not exist in class "%s".', 'validateName', $object::class, - ) + ), ); $dataSet->getRules(); } @@ -480,7 +479,7 @@ public static function objectWithLabelsProvider(): array [$dataSet, $expectedResult], [$dataSet, $expectedResult], // Not a duplicate. Used to test caching. [ - new ObjectDataSet(new class () { + new ObjectDataSet(new class { #[Required] #[Label('Test label')] public string $property; diff --git a/tests/Debug/ValidatorInterfaceProxyTest.php b/tests/Debug/ValidatorInterfaceProxyTest.php index a203c603f..c2e079807 100644 --- a/tests/Debug/ValidatorInterfaceProxyTest.php +++ b/tests/Debug/ValidatorInterfaceProxyTest.php @@ -52,7 +52,7 @@ protected function checkCollectedData(array $data): void 'errors' => [], ], ], - $data + $data, ); } @@ -60,7 +60,7 @@ protected function checkIndexData(array $data): void { $this->assertEquals( ['total' => 2, 'valid' => 1, 'invalid' => 1], - $data + $data, ); } } diff --git a/tests/Helper/ObjectParserTest.php b/tests/Helper/ObjectParserTest.php index 2c047165c..7415a169f 100644 --- a/tests/Helper/ObjectParserTest.php +++ b/tests/Helper/ObjectParserTest.php @@ -32,7 +32,7 @@ public static function dataSkipStaticProperties(): array return [ [ ['a' => 4, 'b' => 2], - new class () { + new class { public int $a = 4; public static int $b = 2; }, @@ -40,7 +40,7 @@ public static function dataSkipStaticProperties(): array ], [ ['a' => 4, 'c' => 'hello'], - new class () { + new class { public int $a = 4; public static int $b = 2; public string $c = 'hello'; @@ -60,7 +60,7 @@ public function testSkipStaticProperties(array $expectedData, object $object, bo public function testSkipStaticPropertiesDefault(): void { - $object = new class () { + $object = new class { public int $a = 4; public static int $b = 2; }; @@ -83,7 +83,7 @@ public function testDataWithClassString(): void public function testGetRulesExpectingAttributeInheritance(): void { - $object = new class () { + $object = new class { #[CustomUrlRuleSet] public string $url; }; @@ -181,7 +181,7 @@ public function testDisabledCache(): void public function testUninitializedProperty(): void { - $parser = new ObjectParser(new class () { + $parser = new ObjectParser(new class { public int $a = 4; public int $uninitialized; }); diff --git a/tests/Helper/RulesDumperTest.php b/tests/Helper/RulesDumperTest.php index b6d7cc11f..ddb22d52a 100644 --- a/tests/Helper/RulesDumperTest.php +++ b/tests/Helper/RulesDumperTest.php @@ -28,9 +28,9 @@ public static function asArrayDataProvider(): array lessThanMinMessage: 'Value must be greater than 10.', greaterThanMaxMessage: 'Value must be no greater than 100.', skipOnEmpty: true, - skipOnError: true + skipOnError: true, ), - (fn () => yield from [$rule, $rule])(), + (fn() => yield from [$rule, $rule])(), ], ], [ @@ -40,8 +40,8 @@ public static function asArrayDataProvider(): array 'min' => 10, 'max' => 100, 'incorrectInputMessage' => [ - 'template' => 'The allowed types for {property} are integer, float and string. ' . - '{type} given.', + 'template' => 'The allowed types for {property} are integer, float and string. ' + . '{type} given.', 'parameters' => [], ], 'notNumberMessage' => [ diff --git a/tests/Helper/RulesNormalizerTest.php b/tests/Helper/RulesNormalizerTest.php index dc9f9d977..156871b15 100644 --- a/tests/Helper/RulesNormalizerTest.php +++ b/tests/Helper/RulesNormalizerTest.php @@ -61,7 +61,7 @@ public static function dataNormalize(): array public function testNormalizeWithArrayResult( array $expected, callable|iterable|object|string|null $rules = null, - mixed $data = null + mixed $data = null, ): void { $rules = RulesNormalizer::normalize($rules, $data); @@ -85,7 +85,7 @@ public static function dataNormalizeList(): array ], [ [Callback::class], - static fn () => new Result(), + static fn() => new Result(), ], [ [Number::class], @@ -93,7 +93,7 @@ public static function dataNormalizeList(): array ], [ [Number::class, Callback::class], - [new Number(), static fn () => new Result()], + [new Number(), static fn() => new Result()], ], ]; } diff --git a/tests/Helper/SkipOnEmptyNormalizerTest.php b/tests/Helper/SkipOnEmptyNormalizerTest.php index 8612b91a5..5d67d2a8a 100644 --- a/tests/Helper/SkipOnEmptyNormalizerTest.php +++ b/tests/Helper/SkipOnEmptyNormalizerTest.php @@ -19,7 +19,7 @@ public static function normalizeData(): array [null, NeverEmpty::class], [false, NeverEmpty::class], [true, WhenEmpty::class], - [static fn (mixed $value, bool $isPropertyMissing): bool => true, Closure::class], + [static fn(mixed $value, bool $isPropertyMissing): bool => true, Closure::class], ]; } diff --git a/tests/MessagesTest.php b/tests/MessagesTest.php index d06692c3c..ab12efa89 100644 --- a/tests/MessagesTest.php +++ b/tests/MessagesTest.php @@ -36,7 +36,7 @@ public function testBase(): void $result = $validator->validate( 'hello', - new Regex('~\d+~') + new Regex('~\d+~'), ); $this->assertSame( @@ -76,7 +76,7 @@ public function testErrorWithoutPostProcessing(): void Validator::DEFAULT_TRANSLATION_CATEGORY, new MessageSource($this->getMessagesPath()), new SimpleMessageFormatter(), - ) + ), ); $validator = new Validator(translator: $translator); @@ -98,9 +98,9 @@ public function testErrorWithFormatOnly(): void Validator::DEFAULT_TRANSLATION_CATEGORY, new MessageSource($this->getMessagesPath()), new SimpleMessageFormatter(), - ) + ), ); - $messageFormatter = new class () implements MessageFormatterInterface { + $messageFormatter = new class implements MessageFormatterInterface { public function format(string $message, array $parameters, string $locale): string { $result = $message . '!'; diff --git a/tests/ResultTest.php b/tests/ResultTest.php index 15e08c644..52a300bf0 100644 --- a/tests/ResultTest.php +++ b/tests/ResultTest.php @@ -64,7 +64,7 @@ public function testGetErrors(): void { $this->assertEquals( [new Error('error1'), new Error('error2', [], ['path', 2])], - $this->createErrorResult()->getErrors() + $this->createErrorResult()->getErrors(), ); } @@ -83,7 +83,7 @@ public function testGetErrorMessagesIndexedByPath(): void 'attribute4.subattribute4\.1.subattribute4*2' => ['error4.1'], 'attribute4.subattribute4\.3.subattribute4*4' => ['error4.2'], ], - $this->createAttributeErrorResult()->getErrorMessagesIndexedByPath() + $this->createAttributeErrorResult()->getErrorMessagesIndexedByPath(), ); } @@ -118,7 +118,7 @@ public function testGetErrorMessagesIndexedByAttribute(): void '' => ['error3.1', 'error3.2'], 'attribute4' => ['error4.1', 'error4.2'], ], - $this->createAttributeErrorResult()->getErrorMessagesIndexedByProperty() + $this->createAttributeErrorResult()->getErrorMessagesIndexedByProperty(), ); } @@ -164,7 +164,7 @@ public function testGetAttributeErrors(): void new Error('error2.3', [], ['attribute2', 'nested']), new Error('error2.4', [], ['attribute2', 'nested']), ], - $result->getPropertyErrors('attribute2') + $result->getPropertyErrors('attribute2'), ); $this->assertEquals([new Error('error3.1'), new Error('error3.2')], $result->getPropertyErrors('')); $this->assertEquals( @@ -172,7 +172,7 @@ public function testGetAttributeErrors(): void new Error('error4.1', [], ['attribute4', 'subattribute4.1', 'subattribute4*2']), new Error('error4.2', [], ['attribute4', 'subattribute4.3', 'subattribute4*4']), ], - $result->getPropertyErrors('attribute4') + $result->getPropertyErrors('attribute4'), ); } @@ -183,7 +183,7 @@ public function testGetAttributeErrorMessages(): void $this->assertEquals([], $result->getPropertyErrorMessages('attribute1')); $this->assertEquals( ['error2.1', 'error2.2', 'error2.3', 'error2.4'], - $result->getPropertyErrorMessages('attribute2') + $result->getPropertyErrorMessages('attribute2'), ); $this->assertEquals(['error3.1', 'error3.2'], $result->getPropertyErrorMessages('')); $this->assertEquals(['error4.1', 'error4.2'], $result->getPropertyErrorMessages('attribute4')); @@ -213,7 +213,7 @@ public function testGetAttributeErrorMessagesIndexedByPath(): void $this->assertEquals([], $result->getPropertyErrorMessagesIndexedByPath('attribute1')); $this->assertEquals( ['' => ['error2.1', 'error2.2'], 'nested' => ['error2.3', 'error2.4']], - $result->getPropertyErrorMessagesIndexedByPath('attribute2') + $result->getPropertyErrorMessagesIndexedByPath('attribute2'), ); $this->assertEquals(['' => ['error3.1', 'error3.2']], $result->getPropertyErrorMessagesIndexedByPath('')); $this->assertEquals([ @@ -252,7 +252,7 @@ public function testDataKeysWithDots(): void 'user\.age' => ['Too young.'], 'meta.tag' => ['Too short.'], ], - $result->getErrorMessagesIndexedByPath() + $result->getErrorMessagesIndexedByPath(), ); } diff --git a/tests/Rule/AnyRuleTest.php b/tests/Rule/AnyRuleTest.php index 9223f0102..3f6c49a86 100644 --- a/tests/Rule/AnyRuleTest.php +++ b/tests/Rule/AnyRuleTest.php @@ -99,7 +99,7 @@ public static function dataValidationPassed(): array 'right away' => [1, new AnyRule([new IntegerType(), new FloatType()])], 'later' => [1.5, new AnyRule([new IntegerType(), new FloatType()])], 'using as attribute' => [ - new class () { + new class { #[AnyRule([new IntegerType(), new FloatType()])] private int|float $sum = 1.5; }, @@ -115,7 +115,7 @@ public static function dataValidationFailed(): array return [ 'none' => ['1', new AnyRule([new IntegerType(), new FloatType()]), ['' => [$message]]], 'using as attribute' => [ - new class () { + new class { #[AnyRule([new IntegerType(), new FloatType()])] private string $sum = '1.5'; }, @@ -135,18 +135,13 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal( new AnyRule([new IntegerType(), new FloatType()]), new AnyRule([new IntegerType(), new FloatType()], when: $when), ); } - protected function getDifferentRuleInHandlerItems(): array - { - return [AnyRule::class, AnyRuleHandler::class]; - } - public function testAfterInitAttribute(): void { $object = new stdClass(); @@ -157,4 +152,9 @@ public function testAfterInitAttribute(): void $this->assertSame($object, $innerRule1->getObject()); $this->assertSame($object, $innerRule2->getObject()); } + + protected function getDifferentRuleInHandlerItems(): array + { + return [AnyRule::class, AnyRuleHandler::class]; + } } diff --git a/tests/Rule/Base/CountableLimitTestTrait.php b/tests/Rule/Base/CountableLimitTestTrait.php index 01951b8b7..0c4160904 100644 --- a/tests/Rule/Base/CountableLimitTestTrait.php +++ b/tests/Rule/Base/CountableLimitTestTrait.php @@ -9,11 +9,6 @@ trait CountableLimitTestTrait { - /** - * @return class-string - */ - abstract protected function getRuleClass(): string; - public static function dataInitWithMinAndMaxAndExactly(): array { return [ @@ -93,4 +88,9 @@ public function testInitWithMinGreaterThanMax(): void $this->expectExceptionMessage('$min must be lower than $max.'); new $ruleClass(min: 2, max: 1); } + + /** + * @return class-string + */ + abstract protected function getRuleClass(): string; } diff --git a/tests/Rule/Base/DifferentRuleInHandlerTestTrait.php b/tests/Rule/Base/DifferentRuleInHandlerTestTrait.php index 6904fd349..e1d4b24bc 100644 --- a/tests/Rule/Base/DifferentRuleInHandlerTestTrait.php +++ b/tests/Rule/Base/DifferentRuleInHandlerTestTrait.php @@ -19,7 +19,7 @@ public function testDifferentRuleInHandler(): void $this->expectException(UnexpectedRuleException::class); $this->expectExceptionMessage( - 'Expected "' . $ruleClassName . '", but "' . RuleWithCustomHandler::class . '" given.' + 'Expected "' . $ruleClassName . '", but "' . RuleWithCustomHandler::class . '" given.', ); $validator->validate([], [$rule]); } diff --git a/tests/Rule/Base/RuleTestCase.php b/tests/Rule/Base/RuleTestCase.php index af9c44098..2980f5d02 100644 --- a/tests/Rule/Base/RuleTestCase.php +++ b/tests/Rule/Base/RuleTestCase.php @@ -19,10 +19,10 @@ abstract public static function dataValidationPassed(): array; public function testValidationPassed( mixed $data, array|RuleInterface|null $rules = null, - ?array $ruleHandlers = null + ?array $ruleHandlers = null, ): void { $validator = new Validator( - ruleHandlerResolver: $ruleHandlers === null ? null : new SimpleRuleHandlerContainer($ruleHandlers) + ruleHandlerResolver: $ruleHandlers === null ? null : new SimpleRuleHandlerContainer($ruleHandlers), ); $result = $validator->validate($data, $rules); @@ -36,10 +36,10 @@ public function testValidationFailed( mixed $data, array|RuleInterface|null $rules, array $errorMessagesIndexedByPath, - ?array $ruleHandlers = null + ?array $ruleHandlers = null, ): void { $validator = new Validator( - ruleHandlerResolver: $ruleHandlers === null ? null : new SimpleRuleHandlerContainer($ruleHandlers) + ruleHandlerResolver: $ruleHandlers === null ? null : new SimpleRuleHandlerContainer($ruleHandlers), ); $result = $validator->validate($data, $rules); @@ -52,7 +52,7 @@ private function getErrorMessages(Result $result): array return array_map( static fn(array $errors) => array_map( static fn(string $error) => str_replace(' ', ' ', $error), - $errors + $errors, ), $result->getErrorMessagesIndexedByPath(), ); diff --git a/tests/Rule/Base/RuleWithProvidedRulesTrait.php b/tests/Rule/Base/RuleWithProvidedRulesTrait.php index 50f2403bc..313d0f9ba 100644 --- a/tests/Rule/Base/RuleWithProvidedRulesTrait.php +++ b/tests/Rule/Base/RuleWithProvidedRulesTrait.php @@ -18,8 +18,7 @@ private function testGetOptionsWithNotRuleInternal($ruleClassName): void { $rule = new $ruleClassName([ new Required(), - new class () { - }, + new class {}, new Number(min: 1), ]); $this->assertInstanceOf(DumpedRuleInterface::class, $rule); diff --git a/tests/Rule/Base/SkipOnErrorTestTrait.php b/tests/Rule/Base/SkipOnErrorTestTrait.php index 4a37e6e66..aafa0e4b3 100644 --- a/tests/Rule/Base/SkipOnErrorTestTrait.php +++ b/tests/Rule/Base/SkipOnErrorTestTrait.php @@ -12,7 +12,7 @@ abstract public function testSkipOnError(): void; private function testSkipOnErrorInternal( SkipOnErrorInterface $nonSkippingRule, - SkipOnErrorInterface $skippingRule + SkipOnErrorInterface $skippingRule, ): void { $this->assertFalse($nonSkippingRule->shouldSkipOnError()); diff --git a/tests/Rule/Base/WhenTestTrait.php b/tests/Rule/Base/WhenTestTrait.php index 237bd64b5..ba25353c6 100644 --- a/tests/Rule/Base/WhenTestTrait.php +++ b/tests/Rule/Base/WhenTestTrait.php @@ -15,7 +15,7 @@ private function testWhenInternal(WhenInterface $ruleWithoutWhen, WhenInterface { $this->assertNull($ruleWithoutWhen->getWhen()); - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $new = $ruleWithoutWhen->when($when); $this->assertNull($ruleWithoutWhen->getWhen()); $this->assertInstanceOf(Closure::class, $new->getWhen()); diff --git a/tests/Rule/BooleanValueTest.php b/tests/Rule/BooleanValueTest.php index 546bb7a52..00bc1d13e 100644 --- a/tests/Rule/BooleanValueTest.php +++ b/tests/Rule/BooleanValueTest.php @@ -35,8 +35,8 @@ public static function dataOptions(): array 'falseValue' => '0', 'strict' => false, 'incorrectInputMessage' => [ - 'template' => 'The allowed types for {property} are integer, float, string, boolean. {type} ' . - 'given.', + 'template' => 'The allowed types for {property} are integer, float, string, boolean. {type} ' + . 'given.', 'parameters' => [ 'true' => '1', 'false' => '0', @@ -61,7 +61,7 @@ public static function dataOptions(): array incorrectInputMessage: 'Custom message 1.', message: 'Custom message 2.', skipOnEmpty: true, - skipOnError: true + skipOnError: true, ), [ 'trueValue' => 'YES', @@ -92,8 +92,8 @@ public static function dataOptions(): array 'falseValue' => false, 'strict' => true, 'incorrectInputMessage' => [ - 'template' => 'The allowed types for {property} are integer, float, string, boolean. {type} ' . - 'given.', + 'template' => 'The allowed types for {property} are integer, float, string, boolean. {type} ' + . 'given.', 'parameters' => [ 'true' => 'true', 'false' => 'false', @@ -248,7 +248,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new BooleanValue(), new BooleanValue(when: $when)); } diff --git a/tests/Rule/CallbackTest.php b/tests/Rule/CallbackTest.php index 6262a446e..fcd1d31ed 100644 --- a/tests/Rule/CallbackTest.php +++ b/tests/Rule/CallbackTest.php @@ -40,12 +40,12 @@ public function testInitWithBothCallbackAndMethodException(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('"$callback" and "$method" are mutually exclusive.'); - new Callback(callback: static fn (): Result => new Result(), method: 'test'); + new Callback(callback: static fn(): Result => new Result(), method: 'test'); } public function testGetName(): void { - $rule = new Callback(callback: static fn (): Result => new Result()); + $rule = new Callback(callback: static fn(): Result => new Result()); $this->assertSame(Callback::class, $rule->getName()); } @@ -57,7 +57,7 @@ public function testGetMethod(): void public function testAfterInitAttributeWithNoMethod(): void { - $rule = new Callback(callback: static fn (): Result => new Result()); + $rule = new Callback(callback: static fn(): Result => new Result()); $callback = $rule->getCallback(); $this->assertIsCallable($callback); $this->assertNull($rule->getMethod()); @@ -73,7 +73,7 @@ public static function dataOptions(): array return [ [ new Callback( - static fn (mixed $value, object $rule, ValidationContext $context): Result => new Result(), + static fn(mixed $value, object $rule, ValidationContext $context): Result => new Result(), ), [ 'method' => null, @@ -83,7 +83,7 @@ public static function dataOptions(): array ], [ new Callback( - static fn (mixed $value, object $rule, ValidationContext $context): Result => new Result(), + static fn(mixed $value, object $rule, ValidationContext $context): Result => new Result(), skipOnEmpty: true, ), [ @@ -159,8 +159,7 @@ public function __construct( #[Callback(method: 'validateName')] #[Callback(method: 'staticValidateName')] private $age, - ) { - } + ) {} private function validateName(mixed $value, RuleInterface $rule, ValidationContext $context): Result { @@ -177,7 +176,7 @@ private function validateName(mixed $value, RuleInterface $rule, ValidationConte private static function staticValidateName( mixed $value, RuleInterface $rule, - ValidationContext $context + ValidationContext $context, ): Result { if ($value !== $context->getDataSet()->getPropertyValue('age')) { throw new RuntimeException('Method scope was not bound to the object.'); @@ -202,7 +201,7 @@ private static function staticValidateName( public function testThrowExceptionWithInvalidReturn(): void { - $callback = static fn (mixed $value, object $rule, ValidationContext $context): string => 'invalid return'; + $callback = static fn(mixed $value, object $rule, ValidationContext $context): string => 'invalid return'; $rule = new Callback($callback); $validator = new Validator(); @@ -225,17 +224,17 @@ public function testValidateUsingMethodOutsideAttributeScope(): void public function testSkipOnError(): void { $this->testSkipOnErrorInternal( - new Callback(callback: static fn (): Result => new Result()), - new Callback(callback: static fn (): Result => new Result(), skipOnError: true), + new Callback(callback: static fn(): Result => new Result()), + new Callback(callback: static fn(): Result => new Result(), skipOnError: true), ); } public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal( - new Callback(callback: static fn (): Result => new Result()), - new Callback(callback: static fn (): Result => new Result(), when: $when), + new Callback(callback: static fn(): Result => new Result()), + new Callback(callback: static fn(): Result => new Result(), when: $when), ); } diff --git a/tests/Rule/CompareTest.php b/tests/Rule/CompareTest.php index 170b24810..d86dd2135 100644 --- a/tests/Rule/CompareTest.php +++ b/tests/Rule/CompareTest.php @@ -41,8 +41,8 @@ public function testInitWithWrongType(): void public function testInitWithWrongOperator(): void { $this->expectException(InvalidArgumentException::class); - $message = 'Operator "=" is not supported. The valid operators are: "==", "===", "!=", "!==", ">", ">=", ' . - '"<", "<=".'; + $message = 'Operator "=" is not supported. The valid operators are: "==", "===", "!=", "!==", ">", ">=", ' + . '"<", "<=".'; $this->expectExceptionMessage($message); new Compare(1, operator: '='); @@ -63,8 +63,8 @@ public static function dataOptions(): array 'targetValue' => 1, 'targetProperty' => null, 'incorrectInputMessage' => [ - 'template' => 'The allowed types for {property} are integer, float, string, boolean, null ' . - 'and object implementing \Stringable interface or \DateTimeInterface. {type} given.', + 'template' => 'The allowed types for {property} are integer, float, string, boolean, null ' + . 'and object implementing \Stringable interface or \DateTimeInterface. {type} given.', 'parameters' => [ 'targetValue' => 1, 'targetProperty' => null, @@ -72,9 +72,9 @@ public static function dataOptions(): array ], ], 'incorrectDataSetTypeMessage' => [ - 'template' => '{Property} returned from a custom data set must have one of the following ' . - 'types: integer, float, string, boolean, null or an object implementing \Stringable ' . - 'interface or \DateTimeInterface.', + 'template' => '{Property} returned from a custom data set must have one of the following ' + . 'types: integer, float, string, boolean, null or an object implementing \Stringable ' + . 'interface or \DateTimeInterface.', 'parameters' => [ 'targetValue' => 1, 'targetProperty' => null, @@ -106,7 +106,7 @@ public static function dataOptions(): array operator: '>=', skipOnEmpty: true, skipOnError: true, - when: static fn (): bool => true, + when: static fn(): bool => true, ), [ 'targetProperty' => 'test', @@ -143,8 +143,8 @@ public static function dataOptions(): array 'targetValue' => 1, 'targetProperty' => 'test', 'incorrectInputMessage' => [ - 'template' => 'The allowed types for {property} are integer, float, string, boolean, null ' . - 'and object implementing \Stringable interface or \DateTimeInterface. {type} given.', + 'template' => 'The allowed types for {property} are integer, float, string, boolean, null ' + . 'and object implementing \Stringable interface or \DateTimeInterface. {type} given.', 'parameters' => [ 'targetValue' => 1, 'targetProperty' => 'test', @@ -152,9 +152,9 @@ public static function dataOptions(): array ], ], 'incorrectDataSetTypeMessage' => [ - 'template' => '{Property} returned from a custom data set must have one of the following ' . - 'types: integer, float, string, boolean, null or an object implementing \Stringable ' . - 'interface or \DateTimeInterface.', + 'template' => '{Property} returned from a custom data set must have one of the following ' + . 'types: integer, float, string, boolean, null or an object implementing \Stringable ' + . 'interface or \DateTimeInterface.', 'parameters' => [ 'targetValue' => 1, 'targetProperty' => 'test', @@ -180,25 +180,25 @@ public static function dataOptions(): array public static function dataValidationPassed(): array { - $targetStringableFloat = new class () implements Stringable { + $targetStringableFloat = new class implements Stringable { public function __toString(): string { return '100.5'; } }; - $stringableFloat = new class () implements Stringable { + $stringableFloat = new class implements Stringable { public function __toString(): string { return '100.50'; } }; - $targetStringableUuid = new class () implements Stringable { + $targetStringableUuid = new class implements Stringable { public function __toString(): string { return '3b98a689-7d49-48bb-8741-7e27f220b69a'; } }; - $stringableUuid = new class () implements Stringable { + $stringableUuid = new class implements Stringable { public function __toString(): string { return 'd62f2b3f-707f-451a-8819-046ff8436a4f'; @@ -377,7 +377,7 @@ public function __toString(): string public static function dataValidationPassedWithDifferentTypes(): array { - $customDataSet = new class () implements DataSetInterface { + $customDataSet = new class implements DataSetInterface { public function getPropertyValue(string $property): mixed { return 100; @@ -524,7 +524,7 @@ public function hasProperty(string $property): bool ['number' => new Compare(targetProperty: 'property', operator: '<=')], ], 'target property: object property, target property value: integer, property value: integer with the same value, type: number, operator: ==' => [ - new class () { + new class { public int $property = 100; public int $number = 100; }, @@ -544,14 +544,14 @@ public function hasProperty(string $property): bool public function testValidationPassed( mixed $data, array|RuleInterface|null $rules = null, - ?array $ruleHandlers = null + ?array $ruleHandlers = null, ): void { parent::testValidationPassed($data, $rules, $ruleHandlers); } public static function dataValidationFailed(): array { - $incorrectDataSet = new class () implements DataWrapperInterface { + $incorrectDataSet = new class implements DataWrapperInterface { public function getPropertyValue(string $property): mixed { return $property === 'test' ? new stdClass() : false; @@ -572,25 +572,25 @@ public function hasProperty(string $property): bool return false; } }; - $targetStringableFloat = new class () implements Stringable { + $targetStringableFloat = new class implements Stringable { public function __toString(): string { return '100.5'; } }; - $stringableFloat = new class () implements Stringable { + $stringableFloat = new class implements Stringable { public function __toString(): string { return '100.50'; } }; - $targetStringableUuid = new class () implements Stringable { + $targetStringableUuid = new class implements Stringable { public function __toString(): string { return '3b98a689-7d49-48bb-8741-7e27f220b69a'; } }; - $stringableUuid = new class () implements Stringable { + $stringableUuid = new class implements Stringable { public function __toString(): string { return 'd62f2b3f-707f-451a-8819-046ff8436a4f'; @@ -611,8 +611,8 @@ public function __toString(): string [new Compare(false)], [ '' => [ - 'The allowed types for value are integer, float, string, boolean, null and object ' . - 'implementing \Stringable interface or \DateTimeInterface. array given.', + 'The allowed types for value are integer, float, string, boolean, null and object ' + . 'implementing \Stringable interface or \DateTimeInterface. array given.', ], ], ], @@ -639,8 +639,8 @@ public function __toString(): string [new Compare(targetProperty: 'test')], [ '' => [ - 'Value returned from a custom data set must have one of the following types: integer, float, ' . - 'string, boolean, null or an object implementing \Stringable interface or \DateTimeInterface.', + 'Value returned from a custom data set must have one of the following types: integer, float, ' + . 'string, boolean, null or an object implementing \Stringable interface or \DateTimeInterface.', ], ], ], @@ -669,8 +669,8 @@ public function __toString(): string [ 'data' => new Compare( targetProperty: 'test', - incorrectDataSetTypeMessage: 'Property - {property}, capitalized property - {Property}, ' . - 'Type - {type}.', + incorrectDataSetTypeMessage: 'Property - {property}, capitalized property - {Property}, ' + . 'Type - {type}.', ), ], ['data' => ['Property - data, capitalized property - Data, Type - stdClass.']], @@ -684,14 +684,14 @@ public function __toString(): string [ new Compare( 100, - message: 'Property - {property}, target value - {targetValue}, target property - ' . - '{targetProperty}, target value or property - {targetValueOrProperty}, value - {value}.', + message: 'Property - {property}, target value - {targetValue}, target property - ' + . '{targetProperty}, target value or property - {targetValueOrProperty}, value - {value}.', ), ], [ '' => [ - 'Property - value, target value - 100, target property - , target value or property - 100, ' . - 'value - 101.', + 'Property - value, target value - 100, target property - , target value or property - 100, ' + . 'value - 101.', ], ], ], @@ -700,16 +700,16 @@ public function __toString(): string [ 'number' => new Compare( targetProperty: 'property', - message: 'Property - {property}, target value - {targetValue}, target property - ' . - '{targetProperty}, target property value - {targetPropertyValue}, target value or ' . - 'property - {targetValueOrProperty}, value - {value}.', + message: 'Property - {property}, target value - {targetValue}, target property - ' + . '{targetProperty}, target property value - {targetPropertyValue}, target value or ' + . 'property - {targetValueOrProperty}, value - {value}.', operator: '===', ), ], [ 'number' => [ - 'Property - number, target value - , target property - property, target property value ' . - '- 100, target value or property - property, value - 101.', + 'Property - number, target value - , target property - property, target property value ' + . '- 100, target value or property - property, value - 101.', ], ], ], @@ -719,16 +719,16 @@ public function __toString(): string [ 'number' => new Compare( targetProperty: 'property', - message: 'Property - {Property}, target value - {targetValue}, target property - ' . - '{TargetProperty}, target property value - {targetPropertyValue}, target value or ' . - 'property - {TargetValueOrProperty}, value - {value}.', + message: 'Property - {Property}, target value - {targetValue}, target property - ' + . '{TargetProperty}, target property value - {targetPropertyValue}, target value or ' + . 'property - {TargetValueOrProperty}, value - {value}.', operator: '===', ), ], [ 'number' => [ - 'Property - Number, target value - , target property - Property, target property value ' . - '- 100, target value or property - Property, value - 101.', + 'Property - Number, target value - , target property - Property, target property value ' + . '- 100, target value or property - Property, value - 101.', ], ], ], @@ -1002,11 +1002,22 @@ public function testValidationFailed( mixed $data, array|RuleInterface|null $rules, array $errorMessagesIndexedByPath, - ?array $ruleHandlers = null + ?array $ruleHandlers = null, ): void { parent::testValidationFailed($data, $rules, $errorMessagesIndexedByPath, $ruleHandlers); } + public function testSkipOnError(): void + { + $this->testSkipOnErrorInternal(new Compare(), new Compare(skipOnError: true)); + } + + public function testWhen(): void + { + $when = static fn(mixed $value): bool => $value !== null; + $this->testWhenInternal(new Compare(), new Compare(when: $when)); + } + private static function extendDataWithDifferentTypes(array $initialData): array { $dynamicData = []; @@ -1048,15 +1059,4 @@ private static function extendDataWithDifferentTypes(array $initialData): array return array_merge($initialData, $dynamicData); } - - public function testSkipOnError(): void - { - $this->testSkipOnErrorInternal(new Compare(), new Compare(skipOnError: true)); - } - - public function testWhen(): void - { - $when = static fn (mixed $value): bool => $value !== null; - $this->testWhenInternal(new Compare(), new Compare(when: $when)); - } } diff --git a/tests/Rule/CompositeTest.php b/tests/Rule/CompositeTest.php index 6f7fc1079..017f0525c 100644 --- a/tests/Rule/CompositeTest.php +++ b/tests/Rule/CompositeTest.php @@ -53,8 +53,8 @@ public static function dataOptions(): array 'min' => null, 'max' => 13, 'incorrectInputMessage' => [ - 'template' => 'The allowed types for {property} are integer, float and string. ' . - '{type} given.', + 'template' => 'The allowed types for {property} are integer, float and string. ' + . '{type} given.', 'parameters' => [], ], 'notNumberMessage' => [ @@ -78,8 +78,8 @@ public static function dataOptions(): array 'min' => null, 'max' => 14, 'incorrectInputMessage' => [ - 'template' => 'The allowed types for {property} are integer, float and string. ' . - '{type} given.', + 'template' => 'The allowed types for {property} are integer, float and string. ' + . '{type} given.', 'parameters' => [], ], 'notNumberMessage' => [ @@ -115,8 +115,8 @@ public static function dataOptions(): array 'min' => null, 'max' => 13, 'incorrectInputMessage' => [ - 'template' => 'The allowed types for {property} are integer, float and string. ' . - '{type} given.', + 'template' => 'The allowed types for {property} are integer, float and string. ' + . '{type} given.', 'parameters' => [], ], 'notNumberMessage' => [ @@ -147,7 +147,7 @@ public static function dataOptions(): array ], 'callable' => [ new Composite([ - static fn () => (new Result())->addError('Bad value.'), + static fn() => (new Result())->addError('Bad value.'), ]), [ 'skipOnEmpty' => false, @@ -163,7 +163,7 @@ public static function dataOptions(): array ], ], 'inheritance' => [ - new class () extends Composite { + new class extends Composite { public function getRules(): array { return [ @@ -216,17 +216,15 @@ public static function dataValidationPassed(): array new Number(max: 13), new Number(max: 14), ], - when: fn () => false, + when: fn() => false, ), ], ], 'override constructor' => [ 20, [ - new class () extends Composite { - public function __construct() - { - } + new class extends Composite { + public function __construct() {} }, ], ], @@ -256,8 +254,8 @@ public static function dataValidationFailed(): array 20, [ new Composite([ - static fn () => (new Result())->addError('Bad value.'), - static fn () => (new Result())->addError('Very bad value.'), + static fn() => (new Result())->addError('Bad value.'), + static fn() => (new Result())->addError('Very bad value.'), ]), ], [ @@ -272,7 +270,7 @@ public static function dataValidationFailed(): array [ new Composite( [new Number(max: 13), new Number(min: 21)], - when: fn () => true, + when: fn() => true, ), ], [ @@ -312,7 +310,7 @@ public static function dataValidationFailed(): array [ new Composite( [new Number(max: 13, greaterThanMaxMessage: 'Custom error')], - when: fn () => true, + when: fn() => true, ), ], ['' => ['Custom error']], @@ -320,7 +318,7 @@ public static function dataValidationFailed(): array 'override rules' => [ null, [ - new class () extends Composite { + new class extends Composite { public function getRules(): array { return [new Required()]; @@ -347,7 +345,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new Composite([]), new Composite([], when: $when)); } @@ -360,7 +358,7 @@ public function testWithCallbackAttribute(): void '' => ['Invalid A.'], 'b' => ['Invalid B.'], ], - $result->getErrorMessagesIndexedByProperty() + $result->getErrorMessagesIndexedByProperty(), ); } diff --git a/tests/Rule/CountTest.php b/tests/Rule/CountTest.php index d53b4a4bd..529f55375 100644 --- a/tests/Rule/CountTest.php +++ b/tests/Rule/CountTest.php @@ -42,18 +42,18 @@ public static function dataOptions(): array 'max' => 5, 'exactly' => null, 'lessThanMinMessage' => [ - 'template' => '{Property} must contain at least {min, number} {min, plural, one{item} ' . - 'other{items}}.', + 'template' => '{Property} must contain at least {min, number} {min, plural, one{item} ' + . 'other{items}}.', 'parameters' => ['min' => 1], ], 'greaterThanMaxMessage' => [ - 'template' => '{Property} must contain at most {max, number} {max, plural, one{item} ' . - 'other{items}}.', + 'template' => '{Property} must contain at most {max, number} {max, plural, one{item} ' + . 'other{items}}.', 'parameters' => ['max' => 5], ], 'notExactlyMessage' => [ - 'template' => '{Property} must contain exactly {exactly, number} {exactly, plural, one{item} ' . - 'other{items}}.', + 'template' => '{Property} must contain exactly {exactly, number} {exactly, plural, one{item} ' + . 'other{items}}.', 'parameters' => ['exactly' => null], ], 'incorrectInputMessage' => [ @@ -116,14 +116,14 @@ public static function dataValidationPassed(): array ], [ new SingleValueDataSet( - new class () implements Countable { + new class implements Countable { protected int $myCount = 3; public function count(): int { return $this->myCount; } - } + }, ), [new Count(min: 3)], ], @@ -196,7 +196,7 @@ public static function dataValidationFailed(): array // https://www.php.net/manual/ru/class.countable.php 'value: class with min count returned from count method but not implenting Countable interface, min: 3' => [ [ - new class () { + new class { protected int $myCount = 3; public function count(): int @@ -302,7 +302,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new Count(min: 3), new Count(min: 3, when: $when)); } diff --git a/tests/Rule/CountableLimitHandlerTraitTest.php b/tests/Rule/CountableLimitHandlerTraitTest.php index 8cbc411c5..37ef0f27b 100644 --- a/tests/Rule/CountableLimitHandlerTraitTest.php +++ b/tests/Rule/CountableLimitHandlerTraitTest.php @@ -21,18 +21,18 @@ public static function validateLimitsWithWrongRuleData(): array { return [ [ - new class () implements CountableLimitInterface { - public function getMin(): int|null + new class implements CountableLimitInterface { + public function getMin(): ?int { return null; } - public function getMax(): int|null + public function getMax(): ?int { return null; } - public function getExactly(): int|null + public function getExactly(): ?int { return 1; } @@ -62,7 +62,7 @@ public function getNotExactlyMessage(): string #[DataProvider('validateLimitsWithWrongRuleData')] public function testValidateLimitsWithWrongRule(CountableLimitInterface|RuleInterface $rule): void { - $handler = new class () implements RuleHandlerInterface { + $handler = new class implements RuleHandlerInterface { use CountableLimitHandlerTrait; public function validate(mixed $value, RuleInterface $rule, ValidationContext $context): Result @@ -74,7 +74,7 @@ public function checkValidateLimits( CountableLimitInterface|RuleInterface $rule, ValidationContext $context, int $number, - Result $result + Result $result, ): void { $this->validateCountableLimits($rule, $context, $number, $result); } diff --git a/tests/Rule/Date/DateTest.php b/tests/Rule/Date/DateTest.php index cdb51312b..8bb10937e 100644 --- a/tests/Rule/Date/DateTest.php +++ b/tests/Rule/Date/DateTest.php @@ -163,7 +163,7 @@ public function testDifferentRuleInHandlerItems(): array $this->expectException(UnexpectedRuleException::class); $this->expectExceptionMessage( - 'Expected "' . Date::class . '", "' . DateTime::class . '", "' . Time::class . '", but "' . RuleWithCustomHandler::class . '" given.' + 'Expected "' . Date::class . '", "' . DateTime::class . '", "' . Time::class . '", but "' . RuleWithCustomHandler::class . '" given.', ); $validator->validate([], $rule); } @@ -198,8 +198,8 @@ public function testWhen(): void $this->testWhenInternal( new Date(), new Date( - when: static fn(mixed $value): bool => $value !== null - ) + when: static fn(mixed $value): bool => $value !== null, + ), ); } } diff --git a/tests/Rule/Date/DateTimeTest.php b/tests/Rule/Date/DateTimeTest.php index 195e0752e..cd3883d4d 100644 --- a/tests/Rule/Date/DateTimeTest.php +++ b/tests/Rule/Date/DateTimeTest.php @@ -105,8 +105,8 @@ public function testWhen(): void $this->testWhenInternal( new Date(), new Date( - when: static fn(mixed $value): bool => $value !== null - ) + when: static fn(mixed $value): bool => $value !== null, + ), ); } } diff --git a/tests/Rule/Date/TimeTest.php b/tests/Rule/Date/TimeTest.php index 871e3a50b..56f6a858b 100644 --- a/tests/Rule/Date/TimeTest.php +++ b/tests/Rule/Date/TimeTest.php @@ -106,8 +106,8 @@ public function testWhen(): void $this->testWhenInternal( new Date(), new Date( - when: static fn(mixed $value): bool => $value !== null - ) + when: static fn(mixed $value): bool => $value !== null, + ), ); } } diff --git a/tests/Rule/EachTest.php b/tests/Rule/EachTest.php index 573fc8c49..35ccc2b8e 100644 --- a/tests/Rule/EachTest.php +++ b/tests/Rule/EachTest.php @@ -56,8 +56,8 @@ public static function dataOptions(): array 'parameters' => [], ], 'incorrectInputKeyMessage' => [ - 'template' => 'Every iterable key of {property} must have an integer or a string type. ' . - '{type} given.', + 'template' => 'Every iterable key of {property} must have an integer or a string type. ' + . '{type} given.', 'parameters' => [], ], 'skipOnEmpty' => false, @@ -70,8 +70,8 @@ public static function dataOptions(): array 'min' => null, 'max' => 13, 'incorrectInputMessage' => [ - 'template' => 'The allowed types for {property} are integer, float and string. ' . - '{type} given.', + 'template' => 'The allowed types for {property} are integer, float and string. ' + . '{type} given.', 'parameters' => [], ], 'notNumberMessage' => [ @@ -97,8 +97,8 @@ public static function dataOptions(): array 'min' => null, 'max' => 14, 'incorrectInputMessage' => [ - 'template' => 'The allowed types for {property} are integer, float and string. ' . - '{type} given.', + 'template' => 'The allowed types for {property} are integer, float and string. ' + . '{type} given.', 'parameters' => [], ], 'notNumberMessage' => [ @@ -132,8 +132,8 @@ public static function dataOptions(): array 'parameters' => [], ], 'incorrectInputKeyMessage' => [ - 'template' => 'Every iterable key of {property} must have an integer or a string type. ' . - '{type} given.', + 'template' => 'Every iterable key of {property} must have an integer or a string type. ' + . '{type} given.', 'parameters' => [], ], 'skipOnEmpty' => false, @@ -146,8 +146,8 @@ public static function dataOptions(): array 'min' => null, 'max' => 13, 'incorrectInputMessage' => [ - 'template' => 'The allowed types for {property} are integer, float and string. ' . - '{type} given.', + 'template' => 'The allowed types for {property} are integer, float and string. ' + . '{type} given.', 'parameters' => [], ], 'notNumberMessage' => [ @@ -293,7 +293,7 @@ public static function dataValidationFailed(): array ], 'single callable rule' => [ [10, 20], - [new Each(static fn (): Result => (new Result())->addError('error'))], + [new Each(static fn(): Result => (new Result())->addError('error'))], [ 0 => ['error'], 1 => ['error'], @@ -301,7 +301,7 @@ public static function dataValidationFailed(): array ], 'rules array with callable' => [ [10, 20], - [new Each([static fn (): Result => (new Result())->addError('error')])], + [new Each([static fn(): Result => (new Result())->addError('error')])], [ 0 => ['error'], 1 => ['error'], @@ -383,7 +383,7 @@ public static function dataValidationFailed(): array [[10, 20, 30], [11, 12, 13], [20, 30, 40]], new Each( new Each(new Integer(max: 15)), - stopOnError: true + stopOnError: true, ), [ '0.1' => ['Value must be no greater than 15.'], @@ -400,15 +400,10 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new Each(), new Each(when: $when)); } - protected function getDifferentRuleInHandlerItems(): array - { - return [Each::class, EachHandler::class]; - } - public static function dataContextEachKey(): array { return [ @@ -437,7 +432,7 @@ public function testContextEachKey($data, $keys): void function (mixed $value, object $rule, ValidationContext $context) use (&$indexes) { $indexes[] = $context->getParameter(Each::PARAMETER_EACH_KEY); return new Result(); - } + }, ), ), ]; @@ -455,7 +450,7 @@ public function testNestedContextEachKey(): void function (mixed $value, object $rule, ValidationContext $context) use (&$indexes) { $indexes[] = $context->getParameter(Each::PARAMETER_EACH_KEY); return new Result(); - } + }, ); (new Validator())->validate( @@ -481,7 +476,12 @@ public function testClassAttribute(): void 'a' => ['Value must be zero.'], 'c' => ['Value must be zero.'], ], - $result->getErrorMessagesIndexedByProperty() + $result->getErrorMessagesIndexedByProperty(), ); } + + protected function getDifferentRuleInHandlerItems(): array + { + return [Each::class, EachHandler::class]; + } } diff --git a/tests/Rule/EmailTest.php b/tests/Rule/EmailTest.php index 5e2a8e45a..aa6cbb246 100644 --- a/tests/Rule/EmailTest.php +++ b/tests/Rule/EmailTest.php @@ -254,10 +254,10 @@ public static function dataValidationFailed(): array $errors, ], [ - 'Short Name ', + 'Short Name ', [$ruleAllowedName], $errors, ], @@ -268,9 +268,9 @@ public static function dataValidationFailed(): array ['', [$ruleEnabledIdn], $errors], [ - 'Короткое имя <тест@это-доменное-имя.после-преобразования-в-idn.будет-содержать-больше-254-символов.' . - 'бла-бла-бла-бла-бла-бла-бла-бла.бла-бла-бла-бла-бла-бла.бла-бла-бла-бла-бла-бла.бла-бла-бла-бла-бла-' . - 'бла.com>', + 'Короткое имя <тест@это-доменное-имя.после-преобразования-в-idn.будет-содержать-больше-254-символов.' + . 'бла-бла-бла-бла-бла-бла-бла-бла.бла-бла-бла-бла-бла-бла.бла-бла-бла-бла-бла-бла.бла-бла-бла-бла-бла-' + . 'бла.com>', [$ruleEnabledIdnAndAllowedName], $errors, ], @@ -321,7 +321,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new Email(), new Email(when: $when)); } diff --git a/tests/Rule/EqualTest.php b/tests/Rule/EqualTest.php index 19b83338a..03b66c400 100644 --- a/tests/Rule/EqualTest.php +++ b/tests/Rule/EqualTest.php @@ -33,8 +33,8 @@ public static function dataOptions(): array 'targetValue' => 1, 'targetProperty' => null, 'incorrectInputMessage' => [ - 'template' => 'The allowed types for {property} are integer, float, string, boolean, null ' . - 'and object implementing \Stringable interface or \DateTimeInterface. {type} given.', + 'template' => 'The allowed types for {property} are integer, float, string, boolean, null ' + . 'and object implementing \Stringable interface or \DateTimeInterface. {type} given.', 'parameters' => [ 'targetValue' => 1, 'targetProperty' => null, @@ -42,9 +42,9 @@ public static function dataOptions(): array ], ], 'incorrectDataSetTypeMessage' => [ - 'template' => '{Property} returned from a custom data set must have one of the following ' . - 'types: integer, float, string, boolean, null or an object implementing \Stringable ' . - 'interface or \DateTimeInterface.', + 'template' => '{Property} returned from a custom data set must have one of the following ' + . 'types: integer, float, string, boolean, null or an object implementing \Stringable ' + . 'interface or \DateTimeInterface.', 'parameters' => [ 'targetValue' => 1, 'targetProperty' => null, @@ -76,7 +76,7 @@ public static function dataOptions(): array strict: true, skipOnEmpty: true, skipOnError: true, - when: static fn (): bool => true, + when: static fn(): bool => true, ), [ 'targetProperty' => 'test', @@ -131,7 +131,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new Equal(1), new Equal(1, when: $when)); } } diff --git a/tests/Rule/FilledAtLeastTest.php b/tests/Rule/FilledAtLeastTest.php index 7f23302f0..35eed0cc5 100644 --- a/tests/Rule/FilledAtLeastTest.php +++ b/tests/Rule/FilledAtLeastTest.php @@ -67,8 +67,8 @@ public static function dataOptions(): array 'parameters' => [], ], 'message' => [ - 'template' => 'At least {min, number} {min, plural, one{property} other{properties}} from ' . - 'this list must be filled for {property}: {properties}.', + 'template' => 'At least {min, number} {min, plural, one{property} other{properties}} from ' + . 'this list must be filled for {property}: {properties}.', 'parameters' => ['min' => 1], ], 'skipOnEmpty' => false, @@ -115,8 +115,8 @@ public static function dataOptions(): array 'parameters' => [], ], 'message' => [ - 'template' => 'At least {min, number} {min, plural, one{property} other{properties}} from ' . - 'this list must be filled for {property}: {properties}.', + 'template' => 'At least {min, number} {min, plural, one{property} other{properties}} from ' + . 'this list must be filled for {property}: {properties}.', 'parameters' => ['min' => 1], ], 'skipOnEmpty' => null, @@ -130,21 +130,21 @@ public static function dataValidationPassed(): array { return [ [ - new class () { + new class { public $prop1 = 1; public $prop2 = null; }, [new FilledAtLeast(['prop1', 'prop2'])], ], [ - new class () { + new class { public $prop1 = null; public $prop2 = 1; }, [new FilledAtLeast(['prop2'])], ], [ - new class () { + new class { private int $prop1 = 1; private $prop2 = null; }, @@ -159,12 +159,12 @@ public static function dataValidationPassed(): array [new FilledAtLeast(['prop2'])], ], [ - new class () { + new class { public $obj; public function __construct() { - $this->obj = new class () { + $this->obj = new class { public $prop1 = 1; public $prop2 = null; }; @@ -173,12 +173,12 @@ public function __construct() ['obj' => new FilledAtLeast(['prop1', 'prop2'])], ], [ - new class () { + new class { public $obj; public function __construct() { - $this->obj = new class () { + $this->obj = new class { public $prop1 = null; public $prop2 = 1; }; @@ -214,7 +214,7 @@ public function __construct() public static function dataValidationFailed(): array { - $class = new class () { + $class = new class { public $prop1 = 1; public $prop2 = null; }; @@ -296,7 +296,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new FilledAtLeast(['prop']), new FilledAtLeast(['prop'], when: $when)); } diff --git a/tests/Rule/FilledOnlyOneOfTest.php b/tests/Rule/FilledOnlyOneOfTest.php index 920c93076..eb059a705 100644 --- a/tests/Rule/FilledOnlyOneOfTest.php +++ b/tests/Rule/FilledOnlyOneOfTest.php @@ -100,21 +100,21 @@ public static function dataValidationPassed(): array { return [ [ - new class () { + new class { public $prop1 = 1; public $prop2 = null; }, [new FilledOnlyOneOf(['prop1', 'prop2'])], ], [ - new class () { + new class { public $prop1 = null; public $prop2 = 1; }, [new FilledOnlyOneOf(['prop1', 'prop2'])], ], [ - new class () { + new class { private int $prop1 = 1; private $prop2 = null; }, @@ -129,12 +129,12 @@ public static function dataValidationPassed(): array [new FilledOnlyOneOf(['prop1', 'prop2'])], ], [ - new class () { + new class { public $obj; public function __construct() { - $this->obj = new class () { + $this->obj = new class { public $prop1 = 1; public $prop2 = null; }; @@ -143,12 +143,12 @@ public function __construct() ['obj' => new FilledOnlyOneOf(['prop1', 'prop2'])], ], [ - new class () { + new class { public $obj; public function __construct() { - $this->obj = new class () { + $this->obj = new class { public $prop1 = null; public $prop2 = 1; }; @@ -172,7 +172,7 @@ public function __construct() public static function dataValidationFailed(): array { - $object = new class () { + $object = new class { public $prop1 = null; public $prop2 = null; }; @@ -249,7 +249,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new FilledOnlyOneOf([]), new FilledOnlyOneOf([], when: $when)); } diff --git a/tests/Rule/GreaterThanOrEqualTest.php b/tests/Rule/GreaterThanOrEqualTest.php index 0cf193121..0baf9bdd6 100644 --- a/tests/Rule/GreaterThanOrEqualTest.php +++ b/tests/Rule/GreaterThanOrEqualTest.php @@ -33,8 +33,8 @@ public static function dataOptions(): array 'targetValue' => 1, 'targetProperty' => null, 'incorrectInputMessage' => [ - 'template' => 'The allowed types for {property} are integer, float, string, boolean, null ' . - 'and object implementing \Stringable interface or \DateTimeInterface. {type} given.', + 'template' => 'The allowed types for {property} are integer, float, string, boolean, null ' + . 'and object implementing \Stringable interface or \DateTimeInterface. {type} given.', 'parameters' => [ 'targetValue' => 1, 'targetProperty' => null, @@ -42,9 +42,9 @@ public static function dataOptions(): array ], ], 'incorrectDataSetTypeMessage' => [ - 'template' => '{Property} returned from a custom data set must have one of the following ' . - 'types: integer, float, string, boolean, null or an object implementing \Stringable ' . - 'interface or \DateTimeInterface.', + 'template' => '{Property} returned from a custom data set must have one of the following ' + . 'types: integer, float, string, boolean, null or an object implementing \Stringable ' + . 'interface or \DateTimeInterface.', 'parameters' => [ 'targetValue' => 1, 'targetProperty' => null, @@ -75,7 +75,7 @@ public static function dataOptions(): array type: CompareType::ORIGINAL, skipOnEmpty: true, skipOnError: true, - when: static fn (): bool => true, + when: static fn(): bool => true, ), [ 'targetProperty' => 'test', @@ -132,7 +132,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new GreaterThanOrEqual(1), new GreaterThanOrEqual(1, when: $when)); } } diff --git a/tests/Rule/GreaterThanTest.php b/tests/Rule/GreaterThanTest.php index d4d526a99..02fbc6406 100644 --- a/tests/Rule/GreaterThanTest.php +++ b/tests/Rule/GreaterThanTest.php @@ -33,8 +33,8 @@ public static function dataOptions(): array 'targetValue' => 1, 'targetProperty' => null, 'incorrectInputMessage' => [ - 'template' => 'The allowed types for {property} are integer, float, string, boolean, null ' . - 'and object implementing \Stringable interface or \DateTimeInterface. {type} given.', + 'template' => 'The allowed types for {property} are integer, float, string, boolean, null ' + . 'and object implementing \Stringable interface or \DateTimeInterface. {type} given.', 'parameters' => [ 'targetValue' => 1, 'targetProperty' => null, @@ -42,9 +42,9 @@ public static function dataOptions(): array ], ], 'incorrectDataSetTypeMessage' => [ - 'template' => '{Property} returned from a custom data set must have one of the following ' . - 'types: integer, float, string, boolean, null or an object implementing \Stringable ' . - 'interface or \DateTimeInterface.', + 'template' => '{Property} returned from a custom data set must have one of the following ' + . 'types: integer, float, string, boolean, null or an object implementing \Stringable ' + . 'interface or \DateTimeInterface.', 'parameters' => [ 'targetValue' => 1, 'targetProperty' => null, @@ -75,7 +75,7 @@ public static function dataOptions(): array type: CompareType::ORIGINAL, skipOnEmpty: true, skipOnError: true, - when: static fn (): bool => true, + when: static fn(): bool => true, ), [ 'targetProperty' => 'test', @@ -132,7 +132,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new GreaterThan(1), new GreaterThan(1, when: $when)); } } diff --git a/tests/Rule/Image/ImageTest.php b/tests/Rule/Image/ImageTest.php index 5b934f04d..c094bea21 100644 --- a/tests/Rule/Image/ImageTest.php +++ b/tests/Rule/Image/ImageTest.php @@ -301,11 +301,6 @@ public function testValidationFailedAspectRatioWithCustomMessage(): void ); } - protected function getDifferentRuleInHandlerItems(): array - { - return [Image::class, ImageHandler::class]; - } - public static function dataOptions(): array { return [ @@ -520,8 +515,8 @@ public function testWhen(): void $this->testWhenInternal( new Image(), new Image( - when: static fn(mixed $value): bool => $value !== null - ) + when: static fn(mixed $value): bool => $value !== null, + ), ); } @@ -535,7 +530,7 @@ public function testStringValueIsNotFilePath(): void static function (int $code, string $message) use (&$errorMessage): bool { $errorMessage = $message; return true; - } + }, ); $result = $validator->validate('test', new Image()); @@ -546,4 +541,9 @@ static function (int $code, string $message) use (&$errorMessage): bool { $this->assertSame(['Value must be an image.'], $result->getErrorMessages()); $this->assertSame('mime_content_type(test): Failed to open stream: No such file or directory', $errorMessage); } + + protected function getDifferentRuleInHandlerItems(): array + { + return [Image::class, ImageHandler::class]; + } } diff --git a/tests/Rule/Image/StubImageInfoProvider.php b/tests/Rule/Image/StubImageInfoProvider.php index 81184d2be..5bd8fd6f9 100644 --- a/tests/Rule/Image/StubImageInfoProvider.php +++ b/tests/Rule/Image/StubImageInfoProvider.php @@ -11,8 +11,7 @@ final class StubImageInfoProvider implements ImageInfoProviderInterface { public function __construct( private readonly ?ImageInfo $info = null, - ) { - } + ) {} public function get(string $path): ?ImageInfo { diff --git a/tests/Rule/InEnumTest.php b/tests/Rule/InEnumTest.php index 33d6c2078..b4b739ff2 100644 --- a/tests/Rule/InEnumTest.php +++ b/tests/Rule/InEnumTest.php @@ -154,7 +154,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new InEnum(EnumStatus::class), new InEnum(EnumStatus::class, when: $when)); } diff --git a/tests/Rule/InTest.php b/tests/Rule/InTest.php index cc748660b..3d5545bbe 100644 --- a/tests/Rule/InTest.php +++ b/tests/Rule/InTest.php @@ -219,7 +219,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new In(range(1, 10)), new In(range(1, 10), when: $when)); } diff --git a/tests/Rule/IpTest.php b/tests/Rule/IpTest.php index 85ff18dac..3e3e80c97 100644 --- a/tests/Rule/IpTest.php +++ b/tests/Rule/IpTest.php @@ -141,7 +141,7 @@ public static function dataOptions(): array notInRangeMessage: 'Custom message 8.', ranges: ['private'], skipOnEmpty: true, - skipOnError: true + skipOnError: true, ), [ 'networks' => [ @@ -628,7 +628,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new Ip(), new Ip(when: $when)); } diff --git a/tests/Rule/JsonTest.php b/tests/Rule/JsonTest.php index 277e7c95f..7fcfa620d 100644 --- a/tests/Rule/JsonTest.php +++ b/tests/Rule/JsonTest.php @@ -200,7 +200,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new Json(), new Json(when: $when)); } diff --git a/tests/Rule/LengthTest.php b/tests/Rule/LengthTest.php index e232ad6de..48616f89f 100644 --- a/tests/Rule/LengthTest.php +++ b/tests/Rule/LengthTest.php @@ -288,7 +288,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new Length(min: 3), new Length(min: 3, when: $when)); } diff --git a/tests/Rule/LessThanOrEqualTest.php b/tests/Rule/LessThanOrEqualTest.php index 13afd35a9..371c327f7 100644 --- a/tests/Rule/LessThanOrEqualTest.php +++ b/tests/Rule/LessThanOrEqualTest.php @@ -33,8 +33,8 @@ public static function dataOptions(): array 'targetValue' => 1, 'targetProperty' => null, 'incorrectInputMessage' => [ - 'template' => 'The allowed types for {property} are integer, float, string, boolean, null ' . - 'and object implementing \Stringable interface or \DateTimeInterface. {type} given.', + 'template' => 'The allowed types for {property} are integer, float, string, boolean, null ' + . 'and object implementing \Stringable interface or \DateTimeInterface. {type} given.', 'parameters' => [ 'targetValue' => 1, 'targetProperty' => null, @@ -42,9 +42,9 @@ public static function dataOptions(): array ], ], 'incorrectDataSetTypeMessage' => [ - 'template' => '{Property} returned from a custom data set must have one of the following ' . - 'types: integer, float, string, boolean, null or an object implementing \Stringable ' . - 'interface or \DateTimeInterface.', + 'template' => '{Property} returned from a custom data set must have one of the following ' + . 'types: integer, float, string, boolean, null or an object implementing \Stringable ' + . 'interface or \DateTimeInterface.', 'parameters' => [ 'targetValue' => 1, 'targetProperty' => null, @@ -75,7 +75,7 @@ public static function dataOptions(): array type: CompareType::ORIGINAL, skipOnEmpty: true, skipOnError: true, - when: static fn (): bool => true, + when: static fn(): bool => true, ), [ 'targetProperty' => 'test', @@ -133,7 +133,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new LessThanOrEqual(1), new LessThanOrEqual(1, when: $when)); } } diff --git a/tests/Rule/LessThanTest.php b/tests/Rule/LessThanTest.php index 90e3bab8a..0c17bdb37 100644 --- a/tests/Rule/LessThanTest.php +++ b/tests/Rule/LessThanTest.php @@ -33,8 +33,8 @@ public static function dataOptions(): array 'targetValue' => 1, 'targetProperty' => null, 'incorrectInputMessage' => [ - 'template' => 'The allowed types for {property} are integer, float, string, boolean, null ' . - 'and object implementing \Stringable interface or \DateTimeInterface. {type} given.', + 'template' => 'The allowed types for {property} are integer, float, string, boolean, null ' + . 'and object implementing \Stringable interface or \DateTimeInterface. {type} given.', 'parameters' => [ 'targetValue' => 1, 'targetProperty' => null, @@ -42,9 +42,9 @@ public static function dataOptions(): array ], ], 'incorrectDataSetTypeMessage' => [ - 'template' => '{Property} returned from a custom data set must have one of the following ' . - 'types: integer, float, string, boolean, null or an object implementing \Stringable ' . - 'interface or \DateTimeInterface.', + 'template' => '{Property} returned from a custom data set must have one of the following ' + . 'types: integer, float, string, boolean, null or an object implementing \Stringable ' + . 'interface or \DateTimeInterface.', 'parameters' => [ 'targetValue' => 1, 'targetProperty' => null, @@ -75,7 +75,7 @@ public static function dataOptions(): array type: CompareType::ORIGINAL, skipOnEmpty: true, skipOnError: true, - when: static fn (): bool => true, + when: static fn(): bool => true, ), [ 'targetProperty' => 'test', @@ -132,7 +132,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new LessThan(1), new LessThan(1, when: $when)); } } diff --git a/tests/Rule/Nested/NestedPropertyTranslator/MainForm.php b/tests/Rule/Nested/NestedPropertyTranslator/MainForm.php index 9b63d5f32..7351e3478 100644 --- a/tests/Rule/Nested/NestedPropertyTranslator/MainForm.php +++ b/tests/Rule/Nested/NestedPropertyTranslator/MainForm.php @@ -11,6 +11,5 @@ final class MainForm public function __construct( #[Nested] public SubForm $sub, - ) { - } + ) {} } diff --git a/tests/Rule/Nested/NestedPropertyTranslator/NestedPropertyTranslatorTest.php b/tests/Rule/Nested/NestedPropertyTranslator/NestedPropertyTranslatorTest.php index f002316ef..1c98bbd85 100644 --- a/tests/Rule/Nested/NestedPropertyTranslator/NestedPropertyTranslatorTest.php +++ b/tests/Rule/Nested/NestedPropertyTranslator/NestedPropertyTranslatorTest.php @@ -20,7 +20,7 @@ public function testBase(): void assertSame( ['Телефон must contain at least 5 characters.'], - $result->getErrorMessages() + $result->getErrorMessages(), ); } } diff --git a/tests/Rule/Nested/NestedTest.php b/tests/Rule/Nested/NestedTest.php index 3c4f11305..64572c796 100644 --- a/tests/Rule/Nested/NestedTest.php +++ b/tests/Rule/Nested/NestedTest.php @@ -110,8 +110,8 @@ public static function dataOptions(): array 'parameters' => [], ], 'incorrectDataSetTypeMessage' => [ - 'template' => 'An object data set data for {property} can only have an array type. {type} ' . - 'given.', + 'template' => 'An object data set data for {property} can only have an array type. {type} ' + . 'given.', 'parameters' => [], ], 'incorrectInputMessage' => [ @@ -131,8 +131,8 @@ public static function dataOptions(): array 'min' => null, 'max' => null, 'incorrectInputMessage' => [ - 'template' => 'The allowed types for {property} are integer, float and string. ' . - '{type} given.', + 'template' => 'The allowed types for {property} are integer, float and string. ' + . '{type} given.', 'parameters' => [], ], 'notNumberMessage' => [ @@ -165,8 +165,8 @@ public static function dataOptions(): array 'parameters' => [], ], 'incorrectDataSetTypeMessage' => [ - 'template' => 'An object data set data for {property} can only have an array type. {type} ' . - 'given.', + 'template' => 'An object data set data for {property} can only have an array type. {type} ' + . 'given.', 'parameters' => [], ], 'incorrectInputMessage' => [ @@ -199,8 +199,8 @@ public static function dataOptions(): array 'parameters' => [], ], 'incorrectDataSetTypeMessage' => [ - 'template' => 'An object data set data for {property} can only have an array type. {type} ' . - 'given.', + 'template' => 'An object data set data for {property} can only have an array type. {type} ' + . 'given.', 'parameters' => [], ], 'incorrectInputMessage' => [ @@ -233,8 +233,7 @@ public function testGetOptionsWithNotRule(): void $rule = new Nested([ 'a' => new Required(), - 'b' => new class () { - }, + 'b' => new class {}, 'c' => new Number(min: 1), ]); $rule->getOptions(); @@ -257,7 +256,7 @@ public static function dataHandler(): array { return [ 'class-string-rules' => [ - new class () { + new class { #[Nested(ObjectWithDifferentPropertyVisibility::class)] private array $array = [ 'name' => 'hello', @@ -271,7 +270,7 @@ public static function dataHandler(): array ], ], 'class-string-rules-private-only' => [ - new class () { + new class { #[Nested( rules: ObjectWithDifferentPropertyVisibility::class, rulesSourceClassPropertyVisibility: ReflectionProperty::IS_PRIVATE, @@ -287,7 +286,7 @@ public static function dataHandler(): array ], ], 'rules-provider' => [ - new class () implements RulesProviderInterface { + new class implements RulesProviderInterface { private array $array = [ 'name' => 'hello', 'age' => 17, @@ -300,7 +299,7 @@ public function getRules(): iterable 'array' => new Nested( new SimpleRulesProvider([ 'age' => new Number(min: 99), - ]) + ]), ), ]; } @@ -310,7 +309,7 @@ public function getRules(): iterable ], ], 'empty-rules' => [ - new class () { + new class { #[Nested([])] private readonly ObjectWithDifferentPropertyVisibility $object; @@ -322,7 +321,7 @@ public function __construct() [], ], 'rules-from-validated-value' => [ - new class () { + new class { #[Nested] private readonly ObjectWithDifferentPropertyVisibility $object; @@ -337,7 +336,7 @@ public function __construct() ], ], 'rules-from-validated-value-only-public' => [ - new class () { + new class { #[Nested(validatedObjectPropertyVisibility: ReflectionProperty::IS_PUBLIC)] private readonly ObjectWithDifferentPropertyVisibility $object; @@ -351,7 +350,7 @@ public function __construct() ], ], 'rules-from-validated-value-only-protected' => [ - new class () { + new class { #[Nested(validatedObjectPropertyVisibility: ReflectionProperty::IS_PROTECTED)] private readonly ObjectWithDifferentPropertyVisibility $object; @@ -365,7 +364,7 @@ public function __construct() ], ], 'rules-from-validated-value-inherit-attributes' => [ - new class () { + new class { #[Nested] private readonly InheritAttributesObject $object; @@ -390,7 +389,7 @@ public function __construct() ], ], 'object' => [ - new class () { + new class { #[Nested(['number' => new Number(max: 7)])] private readonly ObjectWithDifferentPropertyVisibility $object; @@ -404,7 +403,7 @@ public function __construct() ], ], 'object-private-only' => [ - new class () { + new class { #[Nested( ['age' => new Number(min: 100, skipOnEmpty: true), 'number' => new Number(max: 7)], validatedObjectPropertyVisibility: ReflectionProperty::IS_PRIVATE, @@ -631,7 +630,7 @@ public function testNestedWithoutRulesWithObject(): void 'caption' => ['Caption must contain at least 3 characters.'], 'object.name' => ['Name must contain at least 5 characters.'], ], - $result->getErrorMessagesIndexedByPath() + $result->getErrorMessagesIndexedByPath(), ); } @@ -878,7 +877,7 @@ public function testWithOtherNestedAndEach( array $rules, array $expectedDetailedErrors, array $expectedErrorMessages, - array $expectedErrorMessagesIndexedByPath + array $expectedErrorMessagesIndexedByPath, ): void { $result = (new Validator())->validate($data, $rules); @@ -887,7 +886,7 @@ public function testWithOtherNestedAndEach( $error->getMessage(), $error->getValuePath(), ], - $result->getErrors() + $result->getErrors(), ); $this->assertSame($expectedDetailedErrors, $errorsData); @@ -897,7 +896,7 @@ public function testWithOtherNestedAndEach( public function testNestedPostValidationHook(): void { - $object = new class () implements PostValidationHookInterface { + $object = new class implements PostValidationHookInterface { #[Required] #[StringValue] #[Length(min: 6)] @@ -933,7 +932,7 @@ public function processValidationResult(Result $result): void 'nested.secondNested.secondInt', 'nested.secondNested.secondString', ], - array_keys($object->validationResult->getFirstErrorMessagesIndexedByPath()) + array_keys($object->validationResult->getFirstErrorMessagesIndexedByPath()), ); $this->assertSame( [ @@ -943,14 +942,14 @@ public function processValidationResult(Result $result): void 'secondNested.secondInt', 'secondNested.secondString', ], - array_keys($object->nested->validationResult->getFirstErrorMessagesIndexedByPath()) + array_keys($object->nested->validationResult->getFirstErrorMessagesIndexedByPath()), ); $this->assertSame( [ 'secondInt', 'secondString', ], - array_keys($object->nested->secondNested->validationResult->getFirstErrorMessagesIndexedByPath()) + array_keys($object->nested->secondNested->validationResult->getFirstErrorMessagesIndexedByPath()), ); } @@ -1041,7 +1040,7 @@ public static function dataValidationPassed(): array [0 => 'a', 1 => 'b'], [ new Nested( - new class () implements RulesProviderInterface { + new class implements RulesProviderInterface { public function getRules(): iterable { yield false => new Length(min: 1); @@ -1064,7 +1063,7 @@ public function getRules(): iterable public static function dataValidationFailed(): array { - $incorrectDataSet = new class () implements DataSetInterface { + $incorrectDataSet = new class implements DataSetInterface { public function getPropertyValue(string $property): mixed { return false; @@ -1084,7 +1083,7 @@ public function hasProperty(string $property): bool return [ // No rules with no object 'no rules with no object, array' => [ - new class () { + new class { #[Nested] public array $value = []; }, @@ -1092,7 +1091,7 @@ public function hasProperty(string $property): bool ['value' => ['Nested rule without rules requires value to be an object. array given.']], ], 'no rules with no object, boolean' => [ - new class () { + new class { #[Nested] public bool $value = false; }, @@ -1100,7 +1099,7 @@ public function hasProperty(string $property): bool ['value' => ['Nested rule without rules requires value to be an object. bool given.']], ], 'no rules with no object, integer' => [ - new class () { + new class { #[Nested] public int $value = 42; }, @@ -1108,7 +1107,7 @@ public function hasProperty(string $property): bool ['value' => ['Nested rule without rules requires value to be an object. int given.']], ], 'custom no rules with no object message' => [ - new class () { + new class { #[Nested(noRulesWithNoObjectMessage: 'Custom no rules with no object message.')] public array $value = []; }, @@ -1116,7 +1115,7 @@ public function hasProperty(string $property): bool ['value' => ['Custom no rules with no object message.']], ], 'custom no rules with no object message with parameters' => [ - new class () { + new class { #[Nested(noRulesWithNoObjectMessage: 'Property - {Property}, {property}, type - {type}.')] public array $value = []; }, @@ -1255,7 +1254,7 @@ public function hasProperty(string $property): bool ], [ new ObjectDataSet( - new class () { + new class { private int $value = 7; }, ReflectionProperty::IS_PUBLIC, @@ -1432,7 +1431,7 @@ public function testValidationFailedWithDetailedErrors(mixed $data, array $rules $error->getMessage(), $error->getValuePath(), ], - $result->getErrors() + $result->getErrors(), ); $this->assertFalse($result->isValid()); @@ -1467,8 +1466,8 @@ public function testInvalidRules(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage( - 'The $rules argument passed to Nested rule can be either: a null, an object implementing ' . - 'RulesProviderInterface, a class string or an iterable.' + 'The $rules argument passed to Nested rule can be either: a null, an object implementing ' + . 'RulesProviderInterface, a class string or an iterable.', ); new Nested(new Required()); } @@ -1499,11 +1498,6 @@ public function testWithCallbackAttribute(): void ); } - protected function getDifferentRuleInHandlerItems(): array - { - return [Nested::class, NestedHandler::class]; - } - public function testUseCallableRules() { $rules = new Nested([ @@ -1534,13 +1528,13 @@ public function testWithArrayOfObjects(): void public function testWithNonArrayButIterableOfObjects(): void { - $classA = new class () { + $classA = new class { #[Required] #[Length(min: 10)] public $id; }; - $classB = new class () { + $classB = new class { #[Each(new Nested())] public iterable $collection; }; @@ -1562,12 +1556,12 @@ public function testWithNonArrayButIterableOfObjects(): void */ public function testExampleFromIssue751() { - $classA = new class () { + $classA = new class { #[Required] public $id; }; - $classB = new class () { + $classB = new class { #[Each([new Nested()])] public $array_of_a; }; @@ -1588,10 +1582,10 @@ public function testNestedRulesWithWhenMissing(): void 'regulations' => new Nested( rules: [ 'is_one_time' => new BooleanType( - skipOnEmpty: new WhenMissing() + skipOnEmpty: new WhenMissing(), ), ], - skipOnEmpty: new WhenMissing() + skipOnEmpty: new WhenMissing(), ), ]; @@ -1648,10 +1642,10 @@ public function testNestedRulesWithWhenMissingAndNullableField(): void } return $result; }, - skipOnEmpty: new WhenMissing() + skipOnEmpty: new WhenMissing(), ), ], - skipOnEmpty: new WhenMissing() + skipOnEmpty: new WhenMissing(), ), ]; @@ -1689,4 +1683,9 @@ public function testNestedRulesWithWhenMissingAndNullableField(): void $result = (new Validator())->validate($data, $rules); $this->assertFalse($result->isValid()); } + + protected function getDifferentRuleInHandlerItems(): array + { + return [Nested::class, NestedHandler::class]; + } } diff --git a/tests/Rule/NotEqualTest.php b/tests/Rule/NotEqualTest.php index 6f03bcba4..cae247e5b 100644 --- a/tests/Rule/NotEqualTest.php +++ b/tests/Rule/NotEqualTest.php @@ -33,8 +33,8 @@ public static function dataOptions(): array 'targetValue' => 1, 'targetProperty' => null, 'incorrectInputMessage' => [ - 'template' => 'The allowed types for {property} are integer, float, string, boolean, null ' . - 'and object implementing \Stringable interface or \DateTimeInterface. {type} given.', + 'template' => 'The allowed types for {property} are integer, float, string, boolean, null ' + . 'and object implementing \Stringable interface or \DateTimeInterface. {type} given.', 'parameters' => [ 'targetValue' => 1, 'targetProperty' => null, @@ -42,9 +42,9 @@ public static function dataOptions(): array ], ], 'incorrectDataSetTypeMessage' => [ - 'template' => '{Property} returned from a custom data set must have one of the following ' . - 'types: integer, float, string, boolean, null or an object implementing \Stringable ' . - 'interface or \DateTimeInterface.', + 'template' => '{Property} returned from a custom data set must have one of the following ' + . 'types: integer, float, string, boolean, null or an object implementing \Stringable ' + . 'interface or \DateTimeInterface.', 'parameters' => [ 'targetValue' => 1, 'targetProperty' => null, @@ -76,7 +76,7 @@ public static function dataOptions(): array strict: true, skipOnEmpty: true, skipOnError: true, - when: static fn (): bool => true, + when: static fn(): bool => true, ), [ 'targetProperty' => 'test', @@ -131,7 +131,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new NotEqual(1), new NotEqual(1, when: $when)); } } diff --git a/tests/Rule/NumberTest.php b/tests/Rule/NumberTest.php index ed4fae180..495602192 100644 --- a/tests/Rule/NumberTest.php +++ b/tests/Rule/NumberTest.php @@ -312,7 +312,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new Number(), new Number(when: $when)); } } diff --git a/tests/Rule/RegexTest.php b/tests/Rule/RegexTest.php index fa389d5b1..3e41dc8c5 100644 --- a/tests/Rule/RegexTest.php +++ b/tests/Rule/RegexTest.php @@ -141,7 +141,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new Regex('//'), new Regex('//', when: $when)); } diff --git a/tests/Rule/RequiredTest.php b/tests/Rule/RequiredTest.php index 85ede621c..d4bb89290 100644 --- a/tests/Rule/RequiredTest.php +++ b/tests/Rule/RequiredTest.php @@ -41,7 +41,7 @@ public static function dataGetEmptyCondition(): array { return [ 'skip on null' => [new WhenNull(), WhenNull::class], - 'closure' => [static fn () => false, Closure::class], + 'closure' => [static fn() => false, Closure::class], ]; } @@ -94,7 +94,7 @@ public static function dataValidationFailed(): array [[], [new Required()], $singleMessageCannotBeBlank], 'custom empty callback' => [ '42', - [new Required(emptyCondition: static fn (mixed $value): bool => $value === '42')], + [new Required(emptyCondition: static fn(mixed $value): bool => $value === '42')], $singleMessageCannotBeBlank, ], 'custom error' => [null, [new Required(message: 'Custom error')], ['' => ['Custom error']]], diff --git a/tests/Rule/StopOnErrorTest.php b/tests/Rule/StopOnErrorTest.php index 2746f66c3..cf72e03ed 100644 --- a/tests/Rule/StopOnErrorTest.php +++ b/tests/Rule/StopOnErrorTest.php @@ -46,22 +46,22 @@ public static function dataOptions(): array 'max' => null, 'exactly' => null, 'lessThanMinMessage' => [ - 'template' => '{Property} must contain at least {min, number} {min, plural, ' . - 'one{character} other{characters}}.', + 'template' => '{Property} must contain at least {min, number} {min, plural, ' + . 'one{character} other{characters}}.', 'parameters' => [ 'min' => 10, ], ], 'greaterThanMaxMessage' => [ - 'template' => '{Property} must contain at most {max, number} {max, plural, ' . - 'one{character} other{characters}}.', + 'template' => '{Property} must contain at most {max, number} {max, plural, ' + . 'one{character} other{characters}}.', 'parameters' => [ 'max' => null, ], ], 'notExactlyMessage' => [ - 'template' => '{Property} must contain exactly {exactly, number} {exactly, plural, ' . - 'one{character} other{characters}}.', + 'template' => '{Property} must contain exactly {exactly, number} {exactly, plural, ' + . 'one{character} other{characters}}.', 'parameters' => [ 'exactly' => null, ], @@ -93,22 +93,22 @@ public static function dataOptions(): array 'max' => null, 'exactly' => null, 'lessThanMinMessage' => [ - 'template' => '{Property} must contain at least {min, number} {min, plural, ' . - 'one{character} other{characters}}.', + 'template' => '{Property} must contain at least {min, number} {min, plural, ' + . 'one{character} other{characters}}.', 'parameters' => [ 'min' => 10, ], ], 'greaterThanMaxMessage' => [ - 'template' => '{Property} must contain at most {max, number} {max, plural, ' . - 'one{character} other{characters}}.', + 'template' => '{Property} must contain at most {max, number} {max, plural, ' + . 'one{character} other{characters}}.', 'parameters' => [ 'max' => null, ], ], 'notExactlyMessage' => [ - 'template' => '{Property} must contain exactly {exactly, number} {exactly, plural, ' . - 'one{character} other{characters}}.', + 'template' => '{Property} must contain exactly {exactly, number} {exactly, plural, ' + . 'one{character} other{characters}}.', 'parameters' => [ 'exactly' => null, ], @@ -278,7 +278,7 @@ public static function dataValidationFailed(): array 'rules normalization, callable' => [ [], new StopOnError([ - static fn (): Result => (new Result())->addError('Custom error.'), + static fn(): Result => (new Result())->addError('Custom error.'), ]), ['' => ['Custom error.']], ], @@ -287,7 +287,7 @@ public static function dataValidationFailed(): array public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal( new StopOnError([new Length(min: 10)]), new StopOnError([new Length(min: 10)], when: $when), @@ -302,7 +302,7 @@ public function testClassAttribute(): void [ '' => ['error A'], ], - $result->getErrorMessagesIndexedByProperty() + $result->getErrorMessagesIndexedByProperty(), ); } diff --git a/tests/Rule/StringValueTest.php b/tests/Rule/StringValueTest.php index b6b4bf3a8..5e1e17fdb 100644 --- a/tests/Rule/StringValueTest.php +++ b/tests/Rule/StringValueTest.php @@ -69,10 +69,10 @@ public static function dataValidationPassed(): array 'value: null, skipOnEmpty: true' => [null, [new StringValue(skipOnEmpty: true)]], 'value: null, when: custom callable allowing everything except null' => [ null, - [new StringValue(when: static fn (mixed $value): bool => $value !== null)], + [new StringValue(when: static fn(mixed $value): bool => $value !== null)], ], 'value: object providing rules and valid data' => [ - new class () { + new class { #[StringValue] private string $name = 'test'; }, @@ -111,11 +111,11 @@ public static function dataValidationFailed(): array ], 'value: integer, when: custom callable allowing everything except null' => [ 1, - [new StringValue(when: static fn (mixed $value): bool => $value !== null)], + [new StringValue(when: static fn(mixed $value): bool => $value !== null)], ['' => [$message]], ], 'value: object providing rules and wrong data' => [ - new class () { + new class { #[StringValue] private ?string $name = null; }, @@ -138,11 +138,10 @@ public static function dataValidationFailed(): array ['data' => ['Property - data, type - bool.']], ], 'value: object providing rules, property labels and wrong data' => [ - new class () implements RulesProviderInterface, PropertyTranslatorProviderInterface { + new class implements RulesProviderInterface, PropertyTranslatorProviderInterface { public function __construct( public ?string $name = null, - ) { - } + ) {} public function getPropertyLabels(): array { @@ -178,7 +177,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new StringValue(), new StringValue(when: $when)); } diff --git a/tests/Rule/SubsetTest.php b/tests/Rule/SubsetTest.php index 8d88ac0c8..5f3323cf7 100644 --- a/tests/Rule/SubsetTest.php +++ b/tests/Rule/SubsetTest.php @@ -261,7 +261,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new Subset([]), new Subset([], when: $when)); } diff --git a/tests/Rule/TrueValueTest.php b/tests/Rule/TrueValueTest.php index 7aabef0cf..fee1759f2 100644 --- a/tests/Rule/TrueValueTest.php +++ b/tests/Rule/TrueValueTest.php @@ -34,8 +34,8 @@ public static function dataOptions(): array 'trueValue' => '1', 'strict' => false, 'incorrectInputMessage' => [ - 'template' => 'The allowed types for {property} are integer, float, string, boolean. {type} ' . - 'given.', + 'template' => 'The allowed types for {property} are integer, float, string, boolean. {type} ' + . 'given.', 'parameters' => [ 'true' => '1', ], @@ -57,7 +57,7 @@ public static function dataOptions(): array incorrectInputMessage: 'Custom incorrect input message.', message: 'Custom message.', skipOnEmpty: true, - skipOnError: true + skipOnError: true, ), [ 'trueValue' => 'YES', @@ -84,8 +84,8 @@ public static function dataOptions(): array 'trueValue' => true, 'strict' => true, 'incorrectInputMessage' => [ - 'template' => 'The allowed types for {property} are integer, float, string, boolean. {type} ' . - 'given.', + 'template' => 'The allowed types for {property} are integer, float, string, boolean. {type} ' + . 'given.', 'parameters' => [ 'true' => 'true', ], @@ -213,7 +213,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new TrueValue(), new TrueValue(when: $when)); } diff --git a/tests/Rule/Type/BooleanTypeTest.php b/tests/Rule/Type/BooleanTypeTest.php index ee12b7115..6a49764e6 100644 --- a/tests/Rule/Type/BooleanTypeTest.php +++ b/tests/Rule/Type/BooleanTypeTest.php @@ -63,7 +63,7 @@ public static function dataValidationPassed(): array 'boolean, false' => [false, new BooleanType()], 'boolean, true' => [true, new BooleanType()], 'using as attribute' => [ - new class () { + new class { #[BooleanType] private bool $active = true; }, @@ -92,11 +92,10 @@ public static function dataValidationFailed(): array ['active' => ['Property - active, type - array']], ], 'message, translated property' => [ - new class () implements RulesProviderInterface, PropertyTranslatorProviderInterface { + new class implements RulesProviderInterface, PropertyTranslatorProviderInterface { public function __construct( public ?bool $active = null, - ) { - } + ) {} public function getPropertyLabels(): array { @@ -121,7 +120,7 @@ public function getRules(): array ['active' => ['"Активен" - не булево значение.']], ], 'using as attribute' => [ - new class () { + new class { #[BooleanType] private int $active = 1; }, @@ -138,7 +137,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new BooleanType(), new BooleanType(when: $when)); } diff --git a/tests/Rule/Type/FloatTypeTest.php b/tests/Rule/Type/FloatTypeTest.php index 1ce1a126b..93a8952e5 100644 --- a/tests/Rule/Type/FloatTypeTest.php +++ b/tests/Rule/Type/FloatTypeTest.php @@ -67,7 +67,7 @@ public static function dataValidationPassed(): array 'float, capital e' => [7E-10, new FloatType()], 'float, underscores, PHP >= 7.4' => [1_234.567, new FloatType()], 'using as attribute' => [ - new class () { + new class { #[FloatType] private float $sum = 1.5; }, @@ -93,11 +93,10 @@ public static function dataValidationFailed(): array ['sum' => ['Property - sum, type - array']], ], 'message, translated property' => [ - new class () implements RulesProviderInterface, PropertyTranslatorProviderInterface { + new class implements RulesProviderInterface, PropertyTranslatorProviderInterface { public function __construct( public ?float $sum = null, - ) { - } + ) {} public function getPropertyLabels(): array { @@ -122,7 +121,7 @@ public function getRules(): array ['sum' => ['"Сумма" - невещественное число.']], ], 'using as attribute' => [ - new class () { + new class { #[FloatType] private int $sum = 1; }, @@ -139,7 +138,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new FloatType(), new FloatType(when: $when)); } diff --git a/tests/Rule/Type/IntegerTypeTest.php b/tests/Rule/Type/IntegerTypeTest.php index 453fd15ff..e943abba1 100644 --- a/tests/Rule/Type/IntegerTypeTest.php +++ b/tests/Rule/Type/IntegerTypeTest.php @@ -68,7 +68,7 @@ public static function dataValidationPassed(): array 'binary number' => [0b11111111, new IntegerType()], 'decimal number, underscores, PHP >= 7.4' => [1_234_567, new IntegerType()], 'using as attribute' => [ - new class () { + new class { #[IntegerType] private int $sum = 1; }, @@ -93,11 +93,10 @@ public static function dataValidationFailed(): array ['sum' => ['Property - sum, type - array']], ], 'message, translated property' => [ - new class () implements RulesProviderInterface, PropertyTranslatorProviderInterface { + new class implements RulesProviderInterface, PropertyTranslatorProviderInterface { public function __construct( public ?int $sum = null, - ) { - } + ) {} public function getPropertyLabels(): array { @@ -122,7 +121,7 @@ public function getRules(): array ['sum' => ['"Сумма" - нецелое число.']], ], 'using as attribute' => [ - new class () { + new class { #[IntegerType] private float $sum = 1.5; }, @@ -139,7 +138,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new IntegerType(), new IntegerType(when: $when)); } diff --git a/tests/Rule/Type/StringTypeTest.php b/tests/Rule/Type/StringTypeTest.php index ad8861b77..fd84a5342 100644 --- a/tests/Rule/Type/StringTypeTest.php +++ b/tests/Rule/Type/StringTypeTest.php @@ -83,7 +83,7 @@ public static function dataValidationPassed(): array new StringType(), ], 'using as attribute' => [ - new class () { + new class { #[StringType] private string $name = 'test'; }, @@ -99,7 +99,7 @@ public static function dataValidationFailed(): array 'float' => [1.5, new StringType(), ['' => ['Value must be a string. float given.']]], 'ingeter' => [1, new StringType(), ['' => ['Value must be a string. int given.']]], 'stringable' => [ - new class () implements Stringable { + new class implements Stringable { public function __toString(): string { return 'test'; @@ -115,11 +115,10 @@ public function __toString(): string ['name' => ['Property - name, type - array']], ], 'message, translated property' => [ - new class () implements RulesProviderInterface, PropertyTranslatorProviderInterface { + new class implements RulesProviderInterface, PropertyTranslatorProviderInterface { public function __construct( public ?string $name = null, - ) { - } + ) {} public function getPropertyLabels(): array { @@ -144,7 +143,7 @@ public function getRules(): array ['name' => ['"Название" - не строка.']], ], 'using as attribute' => [ - new class () { + new class { #[StringType] private array $name = ['test']; }, @@ -161,7 +160,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new StringType(), new StringType(when: $when)); } diff --git a/tests/Rule/UniqueIterableTest.php b/tests/Rule/UniqueIterableTest.php index 822a1cea5..d2f835c0e 100644 --- a/tests/Rule/UniqueIterableTest.php +++ b/tests/Rule/UniqueIterableTest.php @@ -43,8 +43,8 @@ public static function dataOptions(): array 'parameters' => [], ], 'incorrectItemValueMessage' => [ - 'template' => 'The allowed types for iterable\'s item values of {property} are integer, ' . - 'float, string, boolean and object implementing \Stringable or \DateTimeInterface.', + 'template' => 'The allowed types for iterable\'s item values of {property} are integer, ' + . 'float, string, boolean and object implementing \Stringable or \DateTimeInterface.', 'parameters' => [], ], 'differentTypesMessage' => [ @@ -66,7 +66,7 @@ public static function dataOptions(): array differentTypesMessage: 'Custom message 3.', message: 'Custom message 4.', skipOnEmpty: true, - skipOnError: true + skipOnError: true, ), [ 'incorrectInputMessage' => [ @@ -101,13 +101,13 @@ public static function dataValidationPassed(): array 'boolean values' => [[false, true], new UniqueIterable()], 'stringable values' => [ [ - new class () implements Stringable { + new class implements Stringable { public function __toString() { return 'a'; } }, - new class () implements Stringable { + new class implements Stringable { public function __toString() { return 'b'; @@ -121,7 +121,7 @@ public function __toString() new UniqueIterable(), ], 'using as attribute' => [ - new class () { + new class { #[UniqueIterable] private array $data = [1, 2]; }, @@ -133,8 +133,8 @@ public function __toString() public static function dataValidationFailed(): array { $incorrectInputMessage = 'Value must be array or iterable.'; - $incorrectItemValueMessage = 'The allowed types for iterable\'s item values of value are integer, float, ' . - 'string, boolean and object implementing \Stringable or \DateTimeInterface.'; + $incorrectItemValueMessage = 'The allowed types for iterable\'s item values of value are integer, float, ' + . 'string, boolean and object implementing \Stringable or \DateTimeInterface.'; $message = 'Every iterable\'s item of value must be unique.'; return [ @@ -146,11 +146,10 @@ public static function dataValidationFailed(): array ['data' => ['Property - data, type - int.']], ], 'incorrect input, custom message, translated property' => [ - new class () implements RulesProviderInterface, PropertyTranslatorProviderInterface { + new class implements RulesProviderInterface, PropertyTranslatorProviderInterface { public function __construct( public int $data = 1, - ) { - } + ) {} public function getPropertyLabels(): array { @@ -185,11 +184,10 @@ public function getRules(): array ['data' => ['Property - data, type - array.']], ], 'incorrect item value, custom message, translated property' => [ - new class () implements RulesProviderInterface, PropertyTranslatorProviderInterface { + new class implements RulesProviderInterface, PropertyTranslatorProviderInterface { public function __construct( public array $data = [1, 2, [], 3], - ) { - } + ) {} public function getPropertyLabels(): array { @@ -221,25 +219,25 @@ public function getRules(): array 'boolean values' => [[false, true, false], new UniqueIterable(), ['' => [$message]]], 'stringable values' => [ [ - new class () implements Stringable { + new class implements Stringable { public function __toString() { return 'a'; } }, - new class () implements Stringable { + new class implements Stringable { public function __toString() { return 'b'; } }, - new class () implements Stringable { + new class implements Stringable { public function __toString() { return 'a'; } }, - new class () implements Stringable { + new class implements Stringable { public function __toString() { return 'c'; @@ -270,11 +268,10 @@ public function __toString() ['data' => ['Property - data.']], ], 'different types, translated property' => [ - new class () implements RulesProviderInterface, PropertyTranslatorProviderInterface { + new class implements RulesProviderInterface, PropertyTranslatorProviderInterface { public function __construct( public array $data = [1, '2', 3], - ) { - } + ) {} public function getPropertyLabels(): array { @@ -306,11 +303,10 @@ public function getRules(): array ['data' => ['Property - data.']], ], 'custom message, translated property' => [ - new class () implements RulesProviderInterface, PropertyTranslatorProviderInterface { + new class implements RulesProviderInterface, PropertyTranslatorProviderInterface { public function __construct( public array $data = [1, 2, 1, 3], - ) { - } + ) {} public function getPropertyLabels(): array { @@ -333,7 +329,7 @@ public function getRules(): array ['data' => ['"Данные" - в списке есть дубликаты.']], ], 'using as attribute' => [ - new class () { + new class { #[UniqueIterable] private array $data = [1, 2, 1, 3]; }, @@ -350,7 +346,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new UniqueIterable(), new UniqueIterable(when: $when)); } diff --git a/tests/Rule/UrlTest.php b/tests/Rule/UrlTest.php index 80be1ceed..74e809dbb 100644 --- a/tests/Rule/UrlTest.php +++ b/tests/Rule/UrlTest.php @@ -208,7 +208,7 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new Url(), new Url(when: $when)); } diff --git a/tests/Rule/UuidTest.php b/tests/Rule/UuidTest.php index a451e84dd..860d83c18 100644 --- a/tests/Rule/UuidTest.php +++ b/tests/Rule/UuidTest.php @@ -108,11 +108,6 @@ public static function dataOptions(): array ]; } - protected function getDifferentRuleInHandlerItems(): array - { - return [Uuid::class, UuidHandler::class]; - } - public function testSkipOnError(): void { $this->testSkipOnErrorInternal(new Uuid(), new Uuid(skipOnError: true)); @@ -120,7 +115,12 @@ public function testSkipOnError(): void public function testWhen(): void { - $when = static fn (mixed $value): bool => $value !== null; + $when = static fn(mixed $value): bool => $value !== null; $this->testWhenInternal(new Uuid(), new Uuid(when: $when)); } + + protected function getDifferentRuleInHandlerItems(): array + { + return [Uuid::class, UuidHandler::class]; + } } diff --git a/tests/RuleHandlerResolver/RuleHandlerContainerTest.php b/tests/RuleHandlerResolver/RuleHandlerContainerTest.php index ab941f493..6e9fbeba6 100644 --- a/tests/RuleHandlerResolver/RuleHandlerContainerTest.php +++ b/tests/RuleHandlerResolver/RuleHandlerContainerTest.php @@ -32,7 +32,7 @@ public function testNotFound(): void $this->expectException(RuleHandlerNotFoundException::class); $this->expectExceptionMessage( - 'Handler was not found for "not-exists-handler" rule or unresolved "not-exists-handler" class.' + 'Handler was not found for "not-exists-handler" rule or unresolved "not-exists-handler" class.', ); $this->expectExceptionCode(0); $handlersContainer->resolve('not-exists-handler'); diff --git a/tests/RuleHandlerResolver/SimpleRuleHandlerContainerTest.php b/tests/RuleHandlerResolver/SimpleRuleHandlerContainerTest.php index 4bd32457a..34f790f38 100644 --- a/tests/RuleHandlerResolver/SimpleRuleHandlerContainerTest.php +++ b/tests/RuleHandlerResolver/SimpleRuleHandlerContainerTest.php @@ -23,7 +23,7 @@ public function testInvalidInstance(): void public function testPredefinedHandler(): void { - $handler = new class () implements RuleHandlerInterface { + $handler = new class implements RuleHandlerInterface { public function validate(mixed $value, RuleInterface $rule, ValidationContext $context): Result { return new Result(); diff --git a/tests/RulesProvider/AttributesRulesProviderTest.php b/tests/RulesProvider/AttributesRulesProviderTest.php index e5fa6b7ae..2c13c364d 100644 --- a/tests/RulesProvider/AttributesRulesProviderTest.php +++ b/tests/RulesProvider/AttributesRulesProviderTest.php @@ -40,14 +40,13 @@ public static function dataBase(): array ], 'object with no properties' => [ [], - new class () { - }, + new class {}, ], 'object with properties without rule attributes' => [ [ 'title' => [Length::class], ], - new class () { + new class { #[Deprecated(reason: 'test reason', replacement: 'test replacement')] private int $viewsCount = 1; @@ -153,7 +152,7 @@ public static function dataStaticProperties(): array return [ [ ['a' => [Required::class], 'b' => [TrueValue::class]], - new class () { + new class { #[Required] public int $a = 1; #[TrueValue] @@ -163,7 +162,7 @@ public static function dataStaticProperties(): array ], [ ['a' => [Required::class]], - new class () { + new class { #[Required] public int $a = 1; #[TrueValue] @@ -173,7 +172,7 @@ public static function dataStaticProperties(): array ], [ ['a' => [Required::class], 'b' => [TrueValue::class]], - new class () { + new class { #[Required] public int $a = 1; #[TrueValue] @@ -204,8 +203,8 @@ private function convertRulesToClassNames(iterable $rules): array $classNames = []; foreach ($rules as $property => $propertyRules) { $classNames[$property] = array_map( - static fn (RuleInterface $rule): string => $rule::class, - $propertyRules instanceof Traversable ? iterator_to_array($propertyRules) : $propertyRules + static fn(RuleInterface $rule): string => $rule::class, + $propertyRules instanceof Traversable ? iterator_to_array($propertyRules) : $propertyRules, ); } diff --git a/tests/Support/Data/AtLeastDto.php b/tests/Support/Data/AtLeastDto.php index c6a46793e..54732c7fa 100644 --- a/tests/Support/Data/AtLeastDto.php +++ b/tests/Support/Data/AtLeastDto.php @@ -16,8 +16,7 @@ public function __construct( public ?int $a = null, public ?int $b = null, public ?int $c = null, - ) { - } + ) {} public function getPropertyLabels(): array { diff --git a/tests/Support/Data/CallbackDto.php b/tests/Support/Data/CallbackDto.php index 8bddb58db..92d74d60d 100644 --- a/tests/Support/Data/CallbackDto.php +++ b/tests/Support/Data/CallbackDto.php @@ -13,8 +13,7 @@ final class CallbackDto public function __construct( public int $a, public int $b, - ) { - } + ) {} } function validate(CallbackDto $dto): Result diff --git a/tests/Support/Data/CompareObject.php b/tests/Support/Data/CompareObject.php index ff7aec669..279f15c99 100644 --- a/tests/Support/Data/CompareObject.php +++ b/tests/Support/Data/CompareObject.php @@ -9,6 +9,5 @@ final class CompareObject public function __construct( private readonly mixed $a, private readonly mixed $b, - ) { - } + ) {} } diff --git a/tests/Support/Data/CountDto.php b/tests/Support/Data/CountDto.php index f88d47926..a828b7478 100644 --- a/tests/Support/Data/CountDto.php +++ b/tests/Support/Data/CountDto.php @@ -12,8 +12,7 @@ final class CountDto implements Countable { public function __construct( private readonly int $count = 0, - ) { - } + ) {} public function count(): int { diff --git a/tests/Support/Data/EachDto.php b/tests/Support/Data/EachDto.php index 01127ccbf..11061409f 100644 --- a/tests/Support/Data/EachDto.php +++ b/tests/Support/Data/EachDto.php @@ -17,8 +17,7 @@ public function __construct( public int $a, public int $b, public int $c, - ) { - } + ) {} private function validate(int $value): Result { diff --git a/tests/Support/Data/FilledOnlyOneOfDto.php b/tests/Support/Data/FilledOnlyOneOfDto.php index 36c00f09e..42f427962 100644 --- a/tests/Support/Data/FilledOnlyOneOfDto.php +++ b/tests/Support/Data/FilledOnlyOneOfDto.php @@ -16,8 +16,7 @@ public function __construct( public ?int $a = null, public ?int $b = null, public ?int $c = null, - ) { - } + ) {} public function getPropertyLabels(): array { diff --git a/tests/Support/Data/NestedHookProvider/NestedObjectWithPostValidationHook.php b/tests/Support/Data/NestedHookProvider/NestedObjectWithPostValidationHook.php index 423d23c66..71289643e 100644 --- a/tests/Support/Data/NestedHookProvider/NestedObjectWithPostValidationHook.php +++ b/tests/Support/Data/NestedHookProvider/NestedObjectWithPostValidationHook.php @@ -23,7 +23,7 @@ class NestedObjectWithPostValidationHook implements PostValidationHookInterface [ new StringValue(), new Length(min: 6), - ] + ], )] public array $firstArray = [ 'short', diff --git a/tests/Support/Data/NestedHookProvider/SecondNestedObjectWithPostValidationHook.php b/tests/Support/Data/NestedHookProvider/SecondNestedObjectWithPostValidationHook.php index cad2e9bc2..169291c83 100644 --- a/tests/Support/Data/NestedHookProvider/SecondNestedObjectWithPostValidationHook.php +++ b/tests/Support/Data/NestedHookProvider/SecondNestedObjectWithPostValidationHook.php @@ -21,9 +21,7 @@ class SecondNestedObjectWithPostValidationHook implements PostValidationHookInte public string $secondString = 'short'; public ?Result $validationResult = null; - public function __construct() - { - } + public function __construct() {} public function processValidationResult(Result $result): void { diff --git a/tests/Support/Data/ObjectForIterableCollection.php b/tests/Support/Data/ObjectForIterableCollection.php index 1fca5900f..fb79cc26e 100644 --- a/tests/Support/Data/ObjectForIterableCollection.php +++ b/tests/Support/Data/ObjectForIterableCollection.php @@ -16,6 +16,5 @@ public function __construct( #[Required] #[Length(min: 5)] public ?string $name = null, - ) { - } + ) {} } diff --git a/tests/Support/Data/ObjectWithDynamicDataSet.php b/tests/Support/Data/ObjectWithDynamicDataSet.php index 73646670e..fa0a40127 100644 --- a/tests/Support/Data/ObjectWithDynamicDataSet.php +++ b/tests/Support/Data/ObjectWithDynamicDataSet.php @@ -10,9 +10,7 @@ final class ObjectWithDynamicDataSet implements DataSetInterface { - public function __construct(private readonly string $name) - { - } + public function __construct(private readonly string $name) {} public function getPropertyValue(string $property): mixed { diff --git a/tests/Support/Data/SimpleForm.php b/tests/Support/Data/SimpleForm.php index b8620850a..5b4f407ad 100644 --- a/tests/Support/Data/SimpleForm.php +++ b/tests/Support/Data/SimpleForm.php @@ -16,8 +16,7 @@ final class SimpleForm implements RulesProviderInterface, PropertyTranslatorProv public function __construct( public string $name = '', public string $mail = '', - ) { - } + ) {} /** * @psalm-return array diff --git a/tests/Support/DataSet/RulesProvidedDataSet.php b/tests/Support/DataSet/RulesProvidedDataSet.php index 9537f8eba..baef24780 100644 --- a/tests/Support/DataSet/RulesProvidedDataSet.php +++ b/tests/Support/DataSet/RulesProvidedDataSet.php @@ -11,9 +11,7 @@ final class RulesProvidedDataSet implements RulesProviderInterface, DataSetInterface { - public function __construct(private array $data, private readonly array $rules) - { - } + public function __construct(private array $data, private readonly array $rules) {} public function getPropertyValue(string $property): mixed { diff --git a/tests/Support/Rule/NotRuleAttribute.php b/tests/Support/Rule/NotRuleAttribute.php index a6d393e06..14a2a8862 100644 --- a/tests/Support/Rule/NotRuleAttribute.php +++ b/tests/Support/Rule/NotRuleAttribute.php @@ -7,6 +7,4 @@ use Attribute; #[Attribute(Attribute::TARGET_PROPERTY)] -class NotRuleAttribute -{ -} +class NotRuleAttribute {} diff --git a/tests/Support/Rule/RuleWithCustomHandler.php b/tests/Support/Rule/RuleWithCustomHandler.php index 77df70afb..9e920e985 100644 --- a/tests/Support/Rule/RuleWithCustomHandler.php +++ b/tests/Support/Rule/RuleWithCustomHandler.php @@ -10,8 +10,7 @@ final class RuleWithCustomHandler implements RuleInterface { public function __construct( private readonly string $handlerClassName, - ) { - } + ) {} public function getHandler(): string { diff --git a/tests/Support/Rule/StubRule/StubDumpedRule.php b/tests/Support/Rule/StubRule/StubDumpedRule.php index 7986e5051..6c440b987 100644 --- a/tests/Support/Rule/StubRule/StubDumpedRule.php +++ b/tests/Support/Rule/StubRule/StubDumpedRule.php @@ -8,9 +8,7 @@ final class StubDumpedRule implements DumpedRuleInterface { - public function __construct(private readonly string $name, private readonly array $options) - { - } + public function __construct(private readonly string $name, private readonly array $options) {} public function getName(): string { diff --git a/tests/Support/RulesProvider/SimpleRulesProvider.php b/tests/Support/RulesProvider/SimpleRulesProvider.php index 12433f711..514b97c66 100644 --- a/tests/Support/RulesProvider/SimpleRulesProvider.php +++ b/tests/Support/RulesProvider/SimpleRulesProvider.php @@ -10,8 +10,7 @@ final class SimpleRulesProvider implements RulesProviderInterface { public function __construct( private readonly iterable $rules, - ) { - } + ) {} public function getRules(): iterable { diff --git a/tests/Support/StringableObject.php b/tests/Support/StringableObject.php index b8ac512aa..0b522b243 100644 --- a/tests/Support/StringableObject.php +++ b/tests/Support/StringableObject.php @@ -10,8 +10,7 @@ final class StringableObject implements Stringable { public function __construct( private readonly string $value, - ) { - } + ) {} public function __toString(): string { diff --git a/tests/TestEnvironments/WithoutIntl/ConfigTest.php b/tests/TestEnvironments/WithoutIntl/ConfigTest.php index 6cdb1246e..c8719ff0b 100644 --- a/tests/TestEnvironments/WithoutIntl/ConfigTest.php +++ b/tests/TestEnvironments/WithoutIntl/ConfigTest.php @@ -14,7 +14,7 @@ public function testSimpleMessageFormatter(): void { $container = $this->createContainer(); - $customFormatter = new class () implements MessageFormatterInterface { + $customFormatter = new class implements MessageFormatterInterface { public function format(string $message, array $parameters, string $locale): string { return 'test'; diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index 3e8c2e553..d42a8b6b1 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -62,7 +62,7 @@ public function testBase(): void $this->assertFalse($result->isValid()); $this->assertSame( ['name' => ['Name must contain at least 5 characters.']], - $result->getErrorMessagesIndexedByPath() + $result->getErrorMessagesIndexedByPath(), ); } @@ -195,7 +195,7 @@ public static function dataDataAndRulesCombinations(): array [ 'age' => 17, ], - new class () implements RulesProviderInterface { + new class implements RulesProviderInterface { public function getRules(): iterable { return [ @@ -215,7 +215,7 @@ public function getRules(): iterable 'array-and-callable' => [ ['' => ['test message']], [], - static fn (): Result => (new Result())->addError('test message'), + static fn(): Result => (new Result())->addError('test message'), ], ]; } @@ -313,7 +313,7 @@ public function testPreValidation(): void $validator = new Validator(); $result = $validator->validate( new ArrayDataSet(['property' => '']), - ['property' => [new Required(when: static fn (mixed $value, ?ValidationContext $context): bool => false)]], + ['property' => [new Required(when: static fn(mixed $value, ?ValidationContext $context): bool => false)]], ); $this->assertTrue($result->isValid()); @@ -321,17 +321,14 @@ public function testPreValidation(): void public function testRuleHandlerWithoutImplement(): void { - $ruleHandler = new class () { - }; + $ruleHandler = new class {}; $validator = new Validator(); $this->expectException(RuleHandlerInterfaceNotImplementedException::class); $validator->validate(new ArrayDataSet(['property' => '']), [ 'property' => [ new class ($ruleHandler) implements RuleInterface { - public function __construct(private $ruleHandler) - { - } + public function __construct(private $ruleHandler) {} public function getHandler(): string { @@ -349,7 +346,7 @@ public function testRuleWithoutHandler(): void $validator = new Validator(); $validator->validate(new ArrayDataSet(['property' => '']), [ 'property' => [ - new class () implements RuleInterface { + new class implements RuleInterface { public function getHandler(): string { return 'NonExistClass'; @@ -366,7 +363,7 @@ public static function requiredDataProvider(): array 'sort' => [ new In( ['asc', 'desc'], - skipOnEmpty: static fn (mixed $value, bool $isPropertyMissing): bool => $isPropertyMissing + skipOnEmpty: static fn(mixed $value, bool $isPropertyMissing): bool => $isPropertyMissing, ), ], ]; @@ -375,10 +372,10 @@ public static function requiredDataProvider(): array 'sort' => [ new In( ['asc', 'desc'], - skipOnEmpty: static fn ( + skipOnEmpty: static fn( mixed $value, - bool $isPropertyMissing - ): bool => $isPropertyMissing || $value === '' + bool $isPropertyMissing, + ): bool => $isPropertyMissing || $value === '', ), ], ]; @@ -523,10 +520,10 @@ public static function requiredDataProvider(): array 'description' => [new Required(), new Length(min: 5, skipOnError: true)], ], new ObjectDataSet( - new class () { + new class { private string $title = ''; private string $description = 'abc123'; - } + }, ), [new Error('Name not passed.', ['property' => 'name', 'Property' => 'Name'], ['name'], Error::MESSAGE_NONE)], ], @@ -543,7 +540,7 @@ public static function requiredDataProvider(): array * @link https://github.com/yiisoft/validator/issues/289 */ #[DataProvider('requiredDataProvider')] - public function testRequired(array|null $rules, DataSetInterface $dataSet, array $expectedErrors): void + public function testRequired(?array $rules, DataSetInterface $dataSet, array $expectedErrors): void { $validator = new Validator(); $result = $validator->validate($dataSet, $rules); @@ -831,7 +828,7 @@ public static function skipOnEmptyDataProvider(): array 'age' => [ new Integer( min: 18, - skipOnEmpty: static fn (mixed $value, bool $isPropertyMissing): bool => $value === 0 + skipOnEmpty: static fn(mixed $value, bool $isPropertyMissing): bool => $value === 0, ), ], ], @@ -860,7 +857,7 @@ public static function skipOnEmptyDataProvider(): array 'age' => [ new Integer( min: 18, - skipOnEmpty: static fn (mixed $value, bool $isPropertyMissing): bool => $value === 0 + skipOnEmpty: static fn(mixed $value, bool $isPropertyMissing): bool => $value === 0, ), ], ], @@ -884,7 +881,7 @@ public static function skipOnEmptyDataProvider(): array 'age' => [ new Integer( min: 18, - skipOnEmpty: static fn (mixed $value, bool $isPropertyMissing): bool => $value === 0 + skipOnEmpty: static fn(mixed $value, bool $isPropertyMissing): bool => $value === 0, ), ], ], @@ -914,7 +911,7 @@ public static function skipOnEmptyDataProvider(): array 'age' => [ new Integer( min: 18, - skipOnEmpty: static fn (mixed $value, bool $isPropertyMissing): bool => $value === 0 + skipOnEmpty: static fn(mixed $value, bool $isPropertyMissing): bool => $value === 0, ), ], ], @@ -1064,7 +1061,7 @@ public static function skipOnEmptyDataProvider(): array 'validator, skipOnEmpty: custom callback, value not passed' => [ new Validator( - defaultSkipOnEmpty: static fn (mixed $value, bool $isPropertyMissing): bool => $value === 0 + defaultSkipOnEmpty: static fn(mixed $value, bool $isPropertyMissing): bool => $value === 0, ), new ArrayDataSet([ 'name' => 'Dmitriy', @@ -1086,7 +1083,7 @@ public static function skipOnEmptyDataProvider(): array ], 'validator, skipOnEmpty: custom callback, value is empty' => [ new Validator( - defaultSkipOnEmpty: static fn (mixed $value, bool $isPropertyMissing): bool => $value === 0 + defaultSkipOnEmpty: static fn(mixed $value, bool $isPropertyMissing): bool => $value === 0, ), new ArrayDataSet([ 'name' => 'Dmitriy', @@ -1104,7 +1101,7 @@ public static function skipOnEmptyDataProvider(): array ], 'validator, skipOnEmpty: custom callback, value is not empty' => [ new Validator( - defaultSkipOnEmpty: static fn (mixed $value, bool $isPropertyMissing): bool => $value === 0 + defaultSkipOnEmpty: static fn(mixed $value, bool $isPropertyMissing): bool => $value === 0, ), new ArrayDataSet([ 'name' => 'Dmitriy', @@ -1128,7 +1125,7 @@ public static function skipOnEmptyDataProvider(): array ], 'validator, skipOnEmpty: custom callback, value is not empty (null)' => [ new Validator( - defaultSkipOnEmpty: static fn (mixed $value, bool $isPropertyMissing): bool => $value === 0 + defaultSkipOnEmpty: static fn(mixed $value, bool $isPropertyMissing): bool => $value === 0, ), new ArrayDataSet([ 'name' => 'Dmitriy', @@ -1168,7 +1165,7 @@ public static function initSkipOnEmptyDataProvider(): array return [ 'null' => [ null, - new class () { + new class { #[Number] public ?string $name = null; }, @@ -1176,7 +1173,7 @@ public static function initSkipOnEmptyDataProvider(): array ], 'false' => [ false, - new class () { + new class { #[Number] public ?string $name = null; }, @@ -1184,7 +1181,7 @@ public static function initSkipOnEmptyDataProvider(): array ], 'true' => [ true, - new class () { + new class { #[Number] public ?string $name = null; }, @@ -1192,7 +1189,7 @@ public static function initSkipOnEmptyDataProvider(): array ], 'callable' => [ new WhenNull(), - new class () { + new class { #[Number] public ?string $name = null; }, @@ -1200,7 +1197,7 @@ public static function initSkipOnEmptyDataProvider(): array ], 'do-not-override-rule' => [ false, - new class () { + new class { #[Number(skipOnEmpty: true)] public string $name = ''; }, @@ -1239,7 +1236,7 @@ public function testRuleWithoutSkipOnEmpty(): void { $validator = new Validator(defaultSkipOnEmpty: new WhenNull()); - $data = new class () { + $data = new class { #[NotNull] public ?string $name = null; }; @@ -1262,7 +1259,7 @@ public function testValidateWithSingleRule(): void public function testComposition(): void { - $validator = new class () implements ValidatorInterface { + $validator = new class implements ValidatorInterface { private readonly Validator $validator; public function __construct() @@ -1273,7 +1270,7 @@ public function __construct() public function validate( mixed $data, callable|iterable|object|string|null $rules = null, - ?ValidationContext $context = null + ?ValidationContext $context = null, ): Result { $context ??= new ValidationContext(); @@ -1354,7 +1351,7 @@ public function testSkippingRuleInPreValidate(): void { $data = ['agree' => false, 'viewsCount' => -1]; $rules = [ - 'agree' => [new BooleanValue(skipOnEmpty: static fn (): bool => true), new TrueValue()], + 'agree' => [new BooleanValue(skipOnEmpty: static fn(): bool => true), new TrueValue()], 'viewsCount' => [new Integer(min: 0)], ]; $validator = new Validator(); @@ -1421,7 +1418,7 @@ public function testSimpleForm(array $expectedMessages, ?ValidationContext $vali $this->assertSame( $expectedMessages, - $result->getErrorMessagesIndexedByPath() + $result->getErrorMessagesIndexedByPath(), ); } @@ -1472,7 +1469,7 @@ public function testRuleWithBuiltInHandler(): void $this->assertSame( ['' => ['Value must be 42.']], - $result->getErrorMessagesIndexedByPath() + $result->getErrorMessagesIndexedByPath(), ); } @@ -1492,24 +1489,24 @@ public static function dataErrorMessagesWithLabels(): array { return [ [ - new class () { + new class { #[Label('Test')] #[Length( min: 20, - lessThanMinMessage: '{property} value must contain at least {min, number} {min, plural, ' . - 'one{character} other{characters}}.', + lessThanMinMessage: '{property} value must contain at least {min, number} {min, plural, ' + . 'one{character} other{characters}}.', )] public string $property = 'test'; }, ['property' => ['Test value must contain at least 20 characters.']], ], [ - new class () { + new class { #[Label('проверка кириллицы')] #[Length( min: 20, - lessThanMinMessage: '{Property} value must contain at least {min, number} {min, plural, ' . - 'one{character} other{characters}}.', + lessThanMinMessage: '{Property} value must contain at least {min, number} {min, plural, ' + . 'one{character} other{characters}}.', )] public string $property = 'test'; },