Passed
Pull Request — master (#369)
by
unknown
02:54
created

Callback::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use InvalidArgumentException;
10
use ReflectionObject;
11
use Yiisoft\Validator\AttributeEventInterface;
12
use Yiisoft\Validator\DataSet\ObjectDataSet;
13
use Yiisoft\Validator\DataSetInterface;
14
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
15
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
16
use Yiisoft\Validator\Rule\Trait\WhenTrait;
17
use Yiisoft\Validator\SerializableRuleInterface;
18
use Yiisoft\Validator\SkipOnEmptyInterface;
19
use Yiisoft\Validator\SkipOnErrorInterface;
20
use Yiisoft\Validator\ValidationContext;
21
use Yiisoft\Validator\WhenInterface;
22
23
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
24
final class Callback implements
25
    SerializableRuleInterface,
26
    SkipOnErrorInterface,
27
    WhenInterface,
28
    SkipOnEmptyInterface,
29
    AttributeEventInterface
30
{
31
    use SkipOnEmptyTrait;
32
    use SkipOnErrorTrait;
33
    use WhenTrait;
34
35 15
    public function __construct(
36
        /**
37
         * @var callable|null
38
         */
39
        private $callback = null,
40
        private string|null $method = null,
41
42
        /**
43
         * @var bool|callable|null
44
         */
45
        private $skipOnEmpty = null,
46
        private bool $skipOnError = false,
47
        /**
48
         * @var Closure(mixed, ValidationContext):bool|null
49
         */
50
        private ?Closure $when = null,
51
    ) {
52 15
        if ($this->callback === null && $this->method === null) {
53 1
            throw new InvalidArgumentException('Either "$callback" or "$method" must be specified.');
54
        }
55
56 14
        if ($this->callback !== null && $this->method !== null) {
57 1
            throw new InvalidArgumentException('"$callback" and "$method" are mutually exclusive.');
58
        }
59
    }
60
61 1
    public function getName(): string
62
    {
63 1
        return 'callback';
64
    }
65
66 15
    public function getCallback(): callable|null
67
    {
68 15
        return $this->callback;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->callback returns the type mixed which is incompatible with the type-hinted return callable|null.
Loading history...
69
    }
70
71 2
    public function getMethod(): string|null
72
    {
73 2
        return $this->method;
74
    }
75
76 4
    public function afterInitAttribute(ObjectDataSet $dataSet): void
77
    {
78 4
        if ($this->method === null) {
79 1
            return;
80
        }
81
82 3
        $object = $dataSet->getObject();
83 3
        $method = $this->method;
84
85 3
        $reflection = new ReflectionObject($object);
86 3
        if (!$reflection->hasMethod($method)) {
87 1
            throw new InvalidArgumentException(sprintf(
88
                'Method "%s" does not exist in class "%s".',
89
                $method,
90
                $object::class,
91
            ));
92
        }
93
94
        /** @psalm-suppress MixedMethodCall */
95 2
        $this->callback = Closure::bind(fn (mixed ...$args): mixed => $object->{$method}(...$args), $object, $object);
96
    }
97
98 3
    public function getOptions(): array
99
    {
100
        return [
101 3
            'method' => $this->method,
102 3
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
103 3
            'skipOnError' => $this->skipOnError,
104
        ];
105
    }
106
107 14
    public function getHandlerClassName(): string
108
    {
109 14
        return CallbackHandler::class;
110
    }
111
}
112