Skip to content

Commit

Permalink
[9.x] Fix ProhibitedIf docblock and constructor (#42964)
Browse files Browse the repository at this point in the history
This applies the same fixes to ProhibitIf as ExcludeIf.
- Missing @throws declaration in docblock: #41729
- Enforces a boolean in the constructor: #41931 #41969

Also fixes the ExcludeIf @var docblock to match as a \Closure
  • Loading branch information
BrandonSurowiec authored Jun 27, 2022
1 parent 4d46bda commit 1711a5c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Validation/Rules/ExcludeIf.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ExcludeIf
/**
* The condition that validates the attribute.
*
* @var callable|bool
* @var \Closure|bool
*/
public $condition;

Expand Down
9 changes: 6 additions & 3 deletions src/Illuminate/Validation/Rules/ProhibitedIf.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@

namespace Illuminate\Validation\Rules;

use Closure;
use InvalidArgumentException;

class ProhibitedIf
{
/**
* The condition that validates the attribute.
*
* @var callable|bool
* @var \Closure|bool
*/
public $condition;

/**
* Create a new prohibited validation rule based on a condition.
*
* @param callable|bool $condition
* @param \Closure|bool $condition
* @return void
*
* @throws \InvalidArgumentException
*/
public function __construct($condition)
{
if (! is_string($condition)) {
if ($condition instanceof Closure || is_bool($condition)) {
$this->condition = $condition;
} else {
throw new InvalidArgumentException('The provided condition must be a callable or boolean.');
Expand Down
12 changes: 9 additions & 3 deletions tests/Validation/ValidationProhibitedIfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Validation\Rules\ProhibitedIf;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use stdClass;

class ValidationProhibitedIfTest extends TestCase
{
Expand Down Expand Up @@ -38,9 +39,14 @@ public function testItOnlyCallableAndBooleanAreAcceptableArgumentsOfTheRule()

$rule = new ProhibitedIf(true);

$this->expectException(InvalidArgumentException::class);

$rule = new ProhibitedIf('phpinfo');
foreach ([1, 1.1, 'phpinfo', new stdClass] as $condition) {
try {
$rule = new ProhibitedIf($condition);
$this->fail('The ProhibitedIf constructor must not accept '.gettype($condition));
} catch (InvalidArgumentException $exception) {
$this->assertEquals('The provided condition must be a callable or boolean.', $exception->getMessage());
}
}
}

public function testItReturnedRuleIsNotSerializable()
Expand Down

0 comments on commit 1711a5c

Please sign in to comment.