Passed
Push — main ( 6afe0c...47aea4 )
by Breno
01:54
created

RuleChain   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 18
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A __call() 0 14 3
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation;
5
6
use BrenoRoosevelt\Validation\Contracts\Rule;
7
use RuntimeException;
8
9
/**
10
 * @internal
11
 * @method self allowNull()
12
 * @method self allowEmpty()
13
 * @method self required(?string $message = null)
14
 * @method self notEmpty(?string $message = null)
15
 * @method self notEmptyString(?string $message = null)
16
 * @method self notNull(?string $message = null)
17
 * @method self email(?string $message = null)
18
 */
19
trait RuleChain
20
{
21
    abstract public function add(Rule|RuleSet ...$rules): static;
22
23
    public function __call($name, $arguments): static
24
    {
25
        $namespace = __NAMESPACE__ . '\\Rules';
26
        $class = sprintf("%s\%s", $namespace, ucfirst($name));
27
        if (!class_exists($class)) {
28
            throw new RuntimeException(sprintf('Rule not found: (%s).', $name));
29
        }
30
31
        $rule = new $class(...$arguments);
32
        if ($rule instanceof Rule) {
33
            return $this->add($rule);
34
        }
35
36
        return $this;
37
    }
38
}
39