-
Notifications
You must be signed in to change notification settings - Fork 771
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create transformer for deprecated composite rules
This transformers will help transition from the current major version to the next. In the current version, it's possible to call `allOf`, `anyOf`, `noneOf`, and `oneOf` without any arguments or with only one, and that doesn't make much sense.
- Loading branch information
1 parent
1d6d005
commit b6fdd50
Showing
14 changed files
with
258 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Respect\Validation\Transformers; | ||
|
||
use Respect\Validation\Rules\AlwaysInvalid; | ||
use Respect\Validation\Rules\AlwaysValid; | ||
|
||
use function count; | ||
use function in_array; | ||
use function sprintf; | ||
use function trigger_error; | ||
|
||
use const E_USER_DEPRECATED; | ||
|
||
final class DeprecatedComposite implements Transformer | ||
{ | ||
public function __construct( | ||
private readonly Transformer $next | ||
) { | ||
} | ||
|
||
public function transform(RuleSpec $ruleSpec): RuleSpec | ||
{ | ||
if (!in_array($ruleSpec->name, ['allOf', 'anyOf', 'noneOf', 'oneOf'])) { | ||
return $this->next->transform($ruleSpec); | ||
} | ||
|
||
$arguments = $ruleSpec->arguments; | ||
if (count($arguments) >= 2) { | ||
return $this->next->transform($ruleSpec); | ||
} | ||
|
||
if (count($arguments) === 0) { | ||
trigger_error( | ||
sprintf( | ||
'Calling %s() without any arguments has been deprecated, ' . | ||
'and will be not be allowed in the next major version. Use it with at least 2 arguments.', | ||
$ruleSpec->name | ||
), | ||
E_USER_DEPRECATED | ||
); | ||
|
||
return match ($ruleSpec->name) { | ||
'allOf', 'noneOf' => new RuleSpec('alwaysValid'), | ||
'anyOf', 'oneOf' => new RuleSpec('alwaysInvalid'), | ||
}; | ||
} | ||
|
||
trigger_error( | ||
sprintf( | ||
'Calling %s() with a single argument has been deprecated, ' . | ||
'and will be not be allowed in the next major version. Use it with at least 2 arguments.', | ||
$ruleSpec->name | ||
), | ||
E_USER_DEPRECATED | ||
); | ||
|
||
$arguments[] = match ($ruleSpec->name) { | ||
'allOf' => new AlwaysValid(), | ||
'anyOf', 'noneOf', 'oneOf' => new AlwaysInvalid(), | ||
}; | ||
|
||
return new RuleSpec($ruleSpec->name, $arguments); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
date_default_timezone_set('UTC'); | ||
|
||
test('Calling allOf without any arguments', expectDeprecation( | ||
fn() => v::allOf()->assert('input'), | ||
'Calling allOf() without any arguments has been deprecated, and will be not be allowed in the next major version. Use it with at least 2 arguments.', | ||
)); | ||
|
||
test('Calling allOf with a single passing rule as argument', expectDeprecation( | ||
fn() => v::allOf(v::stringType())->assert('input'), | ||
'Calling allOf() with a single argument has been deprecated, and will be not be allowed in the next major version. Use it with at least 2 arguments.', | ||
)); | ||
|
||
test('Calling allOf with a single failing rule as argument', expectMessageAndDeprecation( | ||
fn() => v::allOf(v::intType())->assert('input'), | ||
'"input" must be an integer', | ||
'Calling allOf() with a single argument has been deprecated, and will be not be allowed in the next major version. Use it with at least 2 arguments.', | ||
)); | ||
|
||
test('Calling anyOf without any arguments', expectMessageAndDeprecation( | ||
fn() => v::anyOf()->assert('input'), | ||
'"input" must be valid', | ||
'Calling anyOf() without any arguments has been deprecated, and will be not be allowed in the next major version. Use it with at least 2 arguments.', | ||
)); | ||
|
||
test('Calling anyOf with a single passing rule as argument', expectDeprecation( | ||
fn() => v::anyOf(v::stringType())->assert('input'), | ||
'Calling anyOf() with a single argument has been deprecated, and will be not be allowed in the next major version. Use it with at least 2 arguments.', | ||
)); | ||
|
||
test('Calling anyOf with a single failing rule as argument', expectMessageAndDeprecation( | ||
fn() => v::anyOf(v::intType())->assert('input'), | ||
'"input" must be an integer', | ||
'Calling anyOf() with a single argument has been deprecated, and will be not be allowed in the next major version. Use it with at least 2 arguments.', | ||
)); | ||
|
||
test('Calling noneOf without any arguments', expectDeprecation( | ||
fn() => v::noneOf()->assert('input'), | ||
'Calling noneOf() without any arguments has been deprecated, and will be not be allowed in the next major version. Use it with at least 2 arguments.', | ||
)); | ||
|
||
test('Calling noneOf with a single passing rule as argument', expectMessageAndDeprecation( | ||
fn() => v::noneOf(v::stringType())->assert('input'), | ||
'"input" must not be a string', | ||
'Calling noneOf() with a single argument has been deprecated, and will be not be allowed in the next major version. Use it with at least 2 arguments.', | ||
)); | ||
|
||
test('Calling noneOf with a single failing rule as argument', expectDeprecation( | ||
fn() => v::noneOf(v::intType())->assert('input'), | ||
'Calling noneOf() with a single argument has been deprecated, and will be not be allowed in the next major version. Use it with at least 2 arguments.', | ||
)); | ||
|
||
test('Calling oneOf without any arguments', expectMessageAndDeprecation( | ||
fn() => v::oneOf()->assert('input'), | ||
'"input" must be valid', | ||
'Calling oneOf() without any arguments has been deprecated, and will be not be allowed in the next major version. Use it with at least 2 arguments.', | ||
)); | ||
|
||
test('Calling oneOf with a single passing rule as argument', expectDeprecation( | ||
fn() => v::oneOf(v::stringType())->assert('input'), | ||
'Calling oneOf() with a single argument has been deprecated, and will be not be allowed in the next major version. Use it with at least 2 arguments.', | ||
)); | ||
|
||
test('Calling oneOf with a single failing rule as argument', expectMessageAndDeprecation( | ||
fn() => v::oneOf(v::intType())->assert('input'), | ||
'"input" must be an integer', | ||
'Calling oneOf() with a single argument has been deprecated, and will be not be allowed in the next major version. Use it with at least 2 arguments.', | ||
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.