Skip to content

Commit 17e779a

Browse files
committed
Remove dead code and modernize validation rules
InteractsWithQueue: - Remove unreachable else branch in fail() method - After type narrowing, $exception is Throwable|null, so the instanceof Throwable || is_null() condition is always true - Remove unused InvalidArgumentException import ExcludeIf/ProhibitedIf: - Change parameter type from mixed to bool|Closure to match property type - Remove redundant runtime validation (PHP type system handles it) - Remove unused InvalidArgumentException import - Update tests to expect TypeError instead of InvalidArgumentException (both correctly reject invalid types, just different exception)
1 parent 78b1396 commit 17e779a

File tree

5 files changed

+12
-36
lines changed

5 files changed

+12
-36
lines changed

src/queue/src/InteractsWithQueue.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Hypervel\Queue\Contracts\Job as JobContract;
1111
use Hypervel\Queue\Exceptions\ManuallyFailedException;
1212
use Hypervel\Queue\Jobs\FakeJob;
13-
use InvalidArgumentException;
1413
use PHPUnit\Framework\Assert as PHPUnit;
1514
use RuntimeException;
1615
use Throwable;
@@ -52,13 +51,8 @@ public function fail(string|Throwable|null $exception = null): void
5251
$exception = new ManuallyFailedException($exception);
5352
}
5453

55-
if ($exception instanceof Throwable || is_null($exception)) {
56-
if ($this->job) {
57-
$this->job->fail($exception);
58-
return;
59-
}
60-
} else {
61-
throw new InvalidArgumentException('The fail method requires a string or an instance of Throwable.');
54+
if ($this->job) {
55+
$this->job->fail($exception);
6256
}
6357
}
6458

src/validation/src/Rules/ExcludeIf.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Hypervel\Validation\Rules;
66

77
use Closure;
8-
use InvalidArgumentException;
98
use Stringable;
109

1110
class ExcludeIf implements Stringable
@@ -17,18 +16,10 @@ class ExcludeIf implements Stringable
1716

1817
/**
1918
* Create a new exclude validation rule based on a condition.
20-
*
21-
* @param bool|Closure $condition
22-
*
23-
* @throws InvalidArgumentException
2419
*/
25-
public function __construct(mixed $condition)
20+
public function __construct(bool|Closure $condition)
2621
{
27-
if ($condition instanceof Closure || is_bool($condition)) {
28-
$this->condition = $condition;
29-
} else {
30-
throw new InvalidArgumentException('The provided condition must be a callable or boolean.');
31-
}
22+
$this->condition = $condition;
3223
}
3324

3425
/**

src/validation/src/Rules/ProhibitedIf.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Hypervel\Validation\Rules;
66

77
use Closure;
8-
use InvalidArgumentException;
98
use Stringable;
109

1110
class ProhibitedIf implements Stringable
@@ -17,18 +16,10 @@ class ProhibitedIf implements Stringable
1716

1817
/**
1918
* Create a new prohibited validation rule based on a condition.
20-
*
21-
* @param bool|Closure $condition
22-
*
23-
* @throws InvalidArgumentException
2419
*/
25-
public function __construct(mixed $condition)
20+
public function __construct(bool|Closure $condition)
2621
{
27-
if ($condition instanceof Closure || is_bool($condition)) {
28-
$this->condition = $condition;
29-
} else {
30-
throw new InvalidArgumentException('The provided condition must be a callable or boolean.');
31-
}
22+
$this->condition = $condition;
3223
}
3324

3425
/**

tests/Validation/ValidationExcludeIfTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
use Hypervel\Translation\Translator;
1010
use Hypervel\Validation\Rules\ExcludeIf;
1111
use Hypervel\Validation\Validator;
12-
use InvalidArgumentException;
1312
use PHPUnit\Framework\TestCase;
1413
use stdClass;
14+
use TypeError;
1515

1616
/**
1717
* @internal
@@ -52,8 +52,8 @@ public function testItValidatesCallableAndBooleanAreAcceptableArguments()
5252
try {
5353
new ExcludeIf($condition);
5454
$this->fail('The ExcludeIf constructor must not accept ' . gettype($condition));
55-
} catch (InvalidArgumentException $exception) {
56-
$this->assertEquals('The provided condition must be a callable or boolean.', $exception->getMessage());
55+
} catch (TypeError) {
56+
$this->assertTrue(true); // Invalid types correctly rejected by PHP type system
5757
}
5858
}
5959
}

tests/Validation/ValidationProhibitedIfTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
use Hypervel\Translation\Translator;
1010
use Hypervel\Validation\Rules\ProhibitedIf;
1111
use Hypervel\Validation\Validator;
12-
use InvalidArgumentException;
1312
use PHPUnit\Framework\TestCase;
1413
use stdClass;
14+
use TypeError;
1515

1616
/**
1717
* @internal
@@ -52,8 +52,8 @@ public function testItValidatesCallableAndBooleanAreAcceptableArguments()
5252
try {
5353
new ProhibitedIf($condition);
5454
$this->fail('The ProhibitedIf constructor must not accept ' . gettype($condition));
55-
} catch (InvalidArgumentException $exception) {
56-
$this->assertEquals('The provided condition must be a callable or boolean.', $exception->getMessage());
55+
} catch (TypeError) {
56+
$this->assertTrue(true); // Invalid types correctly rejected by PHP type system
5757
}
5858
}
5959
}

0 commit comments

Comments
 (0)