Skip to content

Commit

Permalink
Improve naming of transformers for deprecations
Browse files Browse the repository at this point in the history
With those changes, the short-name will be shorter and more descriptive.
  • Loading branch information
henriquemoody committed Jan 16, 2025
1 parent 94d53df commit c096fb0
Show file tree
Hide file tree
Showing 24 changed files with 129 additions and 122 deletions.
42 changes: 18 additions & 24 deletions library/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidClassException;
use Respect\Validation\Transformers\Aliases;
use Respect\Validation\Transformers\DeprecatedAge;
use Respect\Validation\Transformers\DeprecatedAttribute;
use Respect\Validation\Transformers\DeprecatedComposite;
use Respect\Validation\Transformers\DeprecatedKey;
use Respect\Validation\Transformers\DeprecatedKeyNested;
use Respect\Validation\Transformers\DeprecatedKeyValue;
use Respect\Validation\Transformers\DeprecatedLength;
use Respect\Validation\Transformers\DeprecatedMinAndMax;
use Respect\Validation\Transformers\DeprecatedSize;
use Respect\Validation\Transformers\DeprecatedType;
use Respect\Validation\Transformers\Deprecated\AgeRule;
use Respect\Validation\Transformers\Deprecated\AttributeRule;
use Respect\Validation\Transformers\Deprecated\CompositeArguments;
use Respect\Validation\Transformers\Deprecated\KeyArguments;
use Respect\Validation\Transformers\Deprecated\KeyNestedRule;
use Respect\Validation\Transformers\Deprecated\KeyValueRule;
use Respect\Validation\Transformers\Deprecated\LengthArguments;
use Respect\Validation\Transformers\Deprecated\MinAndMaxArguments;
use Respect\Validation\Transformers\Deprecated\SizeArguments;
use Respect\Validation\Transformers\Deprecated\TypeRule;
use Respect\Validation\Transformers\Prefix;
use Respect\Validation\Transformers\RuleSpec;
use Respect\Validation\Transformers\Transformer;
Expand All @@ -41,20 +41,14 @@ final class Factory
private array $rulesNamespaces = ['Respect\\Validation\\Rules'];

public function __construct(
private readonly Transformer $transformer = new DeprecatedAttribute(
new DeprecatedKey(
new DeprecatedKeyValue(
new DeprecatedMinAndMax(
new DeprecatedAge(
new DeprecatedKeyNested(
new DeprecatedLength(
new DeprecatedType(
new DeprecatedSize(
new DeprecatedComposite(
new Aliases(new Prefix())
)
)
)
private readonly Transformer $transformer = new AttributeRule(
new KeyArguments(
new KeyValueRule(
new MinAndMaxArguments(
new AgeRule(
new KeyNestedRule(
new LengthArguments(
new TypeRule(new SizeArguments(new CompositeArguments(new Aliases(new Prefix()))))
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@

declare(strict_types=1);

namespace Respect\Validation\Transformers;
namespace Respect\Validation\Transformers\Deprecated;

use Respect\Validation\Rules\GreaterThanOrEqual;
use Respect\Validation\Rules\LessThanOrEqual;
use Respect\Validation\Transformers\RuleSpec;
use Respect\Validation\Transformers\Transformer;

use function in_array;
use function sprintf;
use function trigger_error;

use const E_USER_DEPRECATED;

final class DeprecatedAge implements Transformer
final class AgeRule implements Transformer
{
public function __construct(
private readonly Transformer $next
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@

declare(strict_types=1);

namespace Respect\Validation\Transformers;
namespace Respect\Validation\Transformers\Deprecated;

use Respect\Validation\Transformers\RuleSpec;
use Respect\Validation\Transformers\Transformer;

use function array_key_exists;
use function trigger_error;

use const E_USER_DEPRECATED;

final class DeprecatedAttribute implements Transformer
final class AttributeRule implements Transformer
{
public function __construct(
private readonly Transformer $next
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

declare(strict_types=1);

namespace Respect\Validation\Transformers;
namespace Respect\Validation\Transformers\Deprecated;

use Respect\Validation\Rules\AlwaysInvalid;
use Respect\Validation\Rules\AlwaysValid;
use Respect\Validation\Transformers\RuleSpec;
use Respect\Validation\Transformers\Transformer;

use function count;
use function in_array;
Expand All @@ -19,7 +21,7 @@

use const E_USER_DEPRECATED;

final class DeprecatedComposite implements Transformer
final class CompositeArguments implements Transformer
{
public function __construct(
private readonly Transformer $next
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@

declare(strict_types=1);

namespace Respect\Validation\Transformers;
namespace Respect\Validation\Transformers\Deprecated;

use Respect\Validation\Transformers\RuleSpec;
use Respect\Validation\Transformers\Transformer;

use function array_key_exists;
use function trigger_error;

use const E_USER_DEPRECATED;

final class DeprecatedKey implements Transformer
final class KeyArguments implements Transformer
{
public function __construct(
private readonly Transformer $next
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

declare(strict_types=1);

namespace Respect\Validation\Transformers;
namespace Respect\Validation\Transformers\Deprecated;

use Respect\Validation\Rule;
use Respect\Validation\Rules\ArrayVal;
Expand All @@ -18,6 +18,8 @@
use Respect\Validation\Rules\PropertyExists;
use Respect\Validation\Rules\PropertyOptional;
use Respect\Validation\Rules\When;
use Respect\Validation\Transformers\RuleSpec;
use Respect\Validation\Transformers\Transformer;

use function array_pop;
use function array_reduce;
Expand All @@ -28,7 +30,7 @@

use const E_USER_DEPRECATED;

final class DeprecatedKeyNested implements Transformer
final class KeyNestedRule implements Transformer
{
public function __construct(
private readonly Transformer $next
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@

declare(strict_types=1);

namespace Respect\Validation\Transformers;
namespace Respect\Validation\Transformers\Deprecated;

use Respect\Validation\Message\Placeholder\Quoted;
use Respect\Validation\Rules\AlwaysInvalid;
use Respect\Validation\Rules\Key;
use Respect\Validation\Rules\KeyExists;
use Respect\Validation\Rules\Lazy;
use Respect\Validation\Rules\Templated;
use Respect\Validation\Transformers\RuleSpec;
use Respect\Validation\Transformers\Transformer;
use Respect\Validation\Validator;
use Throwable;

use function trigger_error;

use const E_USER_DEPRECATED;

final class DeprecatedKeyValue implements Transformer
final class KeyValueRule implements Transformer
{
public function __construct(
private readonly Transformer $next
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@

declare(strict_types=1);

namespace Respect\Validation\Transformers;
namespace Respect\Validation\Transformers\Deprecated;

use Respect\Validation\Rule;
use Respect\Validation\Transformers\RuleSpec;
use Respect\Validation\Transformers\Transformer;

use function Respect\Stringifier\stringify;
use function sprintf;
use function trigger_error;

use const E_USER_DEPRECATED;

final class DeprecatedLength implements Transformer
final class LengthArguments implements Transformer
{
public function __construct(
private readonly Transformer $next
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@

declare(strict_types=1);

namespace Respect\Validation\Transformers;
namespace Respect\Validation\Transformers\Deprecated;

use Respect\Validation\Rule;
use Respect\Validation\Transformers\RuleSpec;
use Respect\Validation\Transformers\Transformer;

use function in_array;
use function sprintf;
use function trigger_error;

use const E_USER_DEPRECATED;

final class DeprecatedMinAndMax implements Transformer
final class MinAndMaxArguments implements Transformer
{
public function __construct(
private readonly Transformer $next
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@

declare(strict_types=1);

namespace Respect\Validation\Transformers;
namespace Respect\Validation\Transformers\Deprecated;

use Respect\Validation\Rule;
use Respect\Validation\Rules\Between;
use Respect\Validation\Rules\Equals;
use Respect\Validation\Rules\GreaterThanOrEqual;
use Respect\Validation\Rules\LessThanOrEqual;
use Respect\Validation\Transformers\RuleSpec;
use Respect\Validation\Transformers\Transformer;

use function filter_var;
use function is_float;
Expand All @@ -28,7 +30,7 @@
use const FILTER_VALIDATE_FLOAT;
use const FILTER_VALIDATE_INT;

final class DeprecatedSize implements Transformer
final class SizeArguments implements Transformer
{
public function __construct(
private readonly Transformer $next
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@

declare(strict_types=1);

namespace Respect\Validation\Transformers;
namespace Respect\Validation\Transformers\Deprecated;

use Respect\Validation\Transformers\RuleSpec;
use Respect\Validation\Transformers\Transformer;

use function current;
use function sprintf;
use function trigger_error;

use const E_USER_DEPRECATED;

final class DeprecatedType implements Transformer
final class TypeRule implements Transformer
{
private const AVAILABLE_TYPES = [
'array' => 'arrayType',
Expand Down
26 changes: 12 additions & 14 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,27 @@ parameters:
- message: '/Call to deprecated method optional\(\).+/'
path: tests/feature/Transformers/AliasesTest.php
- message: '/Call to an undefined static method Respect\\Validation\\Validator::(min|max)Age\(\)./'
path: tests/feature/Transformers/DeprecatedAgeTest.php
path: tests/feature/Transformers/Deprecated/AgeRuleTest.php
- message: '/Static method Respect\\Validation\\Mixins\\Builder::.+Of\(\) invoked with \d parameters?, at least 2 required/'
path: tests/feature/Transformers/DeprecatedCompositeTest.php
path: tests/feature/Transformers/Deprecated/CompositeArgumentsTest.php
- message: '/Call to an undefined static method Respect\\Validation\\Validator::attribute\(\)./'
path: tests/feature/Transformers/DeprecatedAttributeTest.php
path: tests/feature/Transformers/Deprecated/AttributeRuleTest.php
- message: '/Static method Respect\\Validation\\Mixins\\Builder::key\(\) invoked with \d parameters?, 2 required/'
path: tests/feature/Transformers/DeprecatedKeyTest.php
path: tests/feature/Transformers/Deprecated/KeyArgumentsTest.php
- message: '/Call to an undefined static method Respect\\Validation\\Validator::keyNested\(\)./'
path: tests/feature/Transformers/DeprecatedKeyNestedTest.php
path: tests/feature/Transformers/Deprecated/KeyNestedRuleTest.php
- message: '/Call to an undefined static method Respect\\Validation\\Validator::keyValue\(\)./'
path: tests/feature/Transformers/DeprecatedKeyValueTest.php
path: tests/feature/Transformers/Deprecated/KeyValueRuleTest.php
- message: '/Parameter #1 \$rule of static method Respect\\Validation\\Mixins\\Builder::length\(\) expects Respect\\Validation\\Rule.+/'
path: tests/feature/Transformers/DeprecatedLengthTest.php
path: tests/feature/Transformers/Deprecated/LengthArgumentsTest.php
- message: '/Static method Respect\\Validation\\Mixins\\Builder::length\(\) invoked with \d parameters, 1 required/'
path: tests/feature/Transformers/DeprecatedLengthTest.php
- message: '/Parameter #1 \$rule of static method Respect\\Validation\\Mixins\\Builder::max\(\) expects Respect\\Validation\\Rule.+/'
path: tests/feature/Transformers/DeprecatedMaxTest.php
- message: '/Parameter #1 \$rule of static method Respect\\Validation\\Mixins\\Builder::min\(\) expects Respect\\Validation\\Rule.+/'
path: tests/feature/Transformers/DeprecatedMinTest.php
path: tests/feature/Transformers/Deprecated/LengthArgumentsTest.php
- message: '/Parameter #1 \$rule of static method Respect\\Validation\\Mixins\\Builder::(min|max)\(\) expects Respect\\Validation\\Rule.+/'
path: tests/feature/Transformers/Deprecated/MinAndMaxArgumentsTest.php
- message: '/Parameter #(1|2) \$(unit|rule) of static method Respect\\Validation\\Mixins\\Builder::size\(\) expects .+/'
path: tests/feature/Transformers/DeprecatedSizeTest.php
path: tests/feature/Transformers/Deprecated/SizeArgumentsTest.php
- message: '/Call to an undefined static method Respect\\Validation\\Validator::type\(\)./'
path: tests/feature/Transformers/DeprecatedTypeTest.php
path: tests/feature/Transformers/Deprecated/TypeRuleTest.php
- message: '/Method .+\\TestingStringifier::stringify\(\) never returns null so it can be removed from the return type./'
path: tests/library/Message/TestingStringifier.php
- message: '/Parameter #1 \$messages of class .+\\ArrayTranslator constructor expects array<string, string>, array<string, int> given./'
Expand Down
56 changes: 56 additions & 0 deletions tests/feature/Transformers/Deprecated/MinAndMaxArgumentsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/

declare(strict_types=1);

test('Min #1', expectMessageAndDeprecation(
fn() => v::min(INF)->assert(10),
'10 must be greater than or equal to `INF`',
'Calling min() with a scalar value has been deprecated, and will be not allows in the next major version. Use greaterThanOrEqual() instead.',
));

test('Min #2', expectMessageAndDeprecation(
fn() => v::not(v::min(5))->assert(INF),
'`INF` must be less than 5',
'Calling min() with a scalar value has been deprecated, and will be not allows in the next major version. Use greaterThanOrEqual() instead.',
));

test('Min #3', expectMessageAndDeprecation(
fn() => v::min('today')->assert('yesterday'),
'"yesterday" must be greater than or equal to "today"',
'Calling min() with a scalar value has been deprecated, and will be not allows in the next major version. Use greaterThanOrEqual() instead.',
));

test('Min #4', expectMessageAndDeprecation(
fn() => v::not(v::min('a'))->assert('z'),
'"z" must be less than "a"',
'Calling min() with a scalar value has been deprecated, and will be not allows in the next major version. Use greaterThanOrEqual() instead.',
));

test('Max #1', expectMessageAndDeprecation(
fn() => v::max(10)->assert(11),
'11 must be less than or equal to 10',
'Calling max() with a scalar value has been deprecated, and will be not allows in the next major version. Use lessThanOrEqual() instead.',
));

test('Max #2', expectMessageAndDeprecation(
fn() => v::not(v::max(10))->assert(5),
'5 must be greater than 10',
'Calling max() with a scalar value has been deprecated, and will be not allows in the next major version. Use lessThanOrEqual() instead.',
));

test('Max #3', expectMessageAndDeprecation(
fn() => v::max('today')->assert('tomorrow'),
'"tomorrow" must be less than or equal to "today"',
'Calling max() with a scalar value has been deprecated, and will be not allows in the next major version. Use lessThanOrEqual() instead.',
));

test('Max #4', expectMessageAndDeprecation(
fn() => v::not(v::max('b'))->assert('a'),
'"a" must be greater than "b"',
'Calling max() with a scalar value has been deprecated, and will be not allows in the next major version. Use lessThanOrEqual() instead.',
));
Loading

0 comments on commit c096fb0

Please sign in to comment.