Passed
Push — main ( 7c50b2...52de70 )
by Breno
01:51
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 RuntimeException;
7
8
/**
9
 * @method self allowsNull()
10
 * @method self allowsEmpty()
11
 * @method self allowsEmptyString()
12
 * @method self notRequired()
13
 * @method self notEmpty(?string $message = null)
14
 * @method self notEmptyString(?string $message = null)
15
 * @method self notNull(?string $message = null)
16
 */
17
trait RuleChain
18
{
19
    abstract public function add(Rule|RuleSet ...$rules): static;
20
21
    public function __call($name, $arguments): static
22
    {
23
        $namespace = __NAMESPACE__ . '\\Rules';
24
        $class = sprintf("%s\%s", $namespace, ucfirst($name));
25
        if (!class_exists($class)) {
26
            throw new RuntimeException(sprintf('Rule not found: (%s).', $name));
27
        }
28
29
        $rule = new $class(...$arguments);
30
        if ($rule instanceof Rule) {
31
            $this->add($rule);
32
        }
33
34
        return $this;
35
    }
36
}
37