InputDataWithCustomNameAndValueTrait   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 54
ccs 28
cts 28
cp 1
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getInputData() 0 7 2
A getValue() 0 5 2
A name() 0 6 1
A value() 0 6 1
A getName() 0 5 2
A inputData() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field\Base\InputData;
6
7
trait InputDataWithCustomNameAndValueTrait
8
{
9
    private ?InputDataInterface $inputData = null;
10
    private bool $useCustomName = false;
11
    private ?string $customName = null;
12
    private bool $useCustomValue = false;
13
    private mixed $customValue = null;
14
15 246
    final public function inputData(InputDataInterface $inputData): static
16
    {
17 246
        $new = clone $this;
18 246
        $new->inputData = $inputData;
19 246
        $new->useCustomName = false;
20 246
        $new->useCustomValue = false;
21 246
        return $new;
22
    }
23
24 484
    final protected function getInputData(): InputDataInterface
25
    {
26 484
        if ($this->inputData === null) {
27 239
            $this->inputData = new PureInputData();
28
        }
29
30 484
        return $this->inputData;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->inputData could return the type null which is incompatible with the type-hinted return Yiisoft\Form\Field\Base\...Data\InputDataInterface. Consider adding an additional type-check to rule them out.
Loading history...
31
    }
32
33 223
    final public function name(?string $name): static
34
    {
35 223
        $new = clone $this;
36 223
        $new->customName = $name;
37 223
        $new->useCustomName = true;
38 223
        return $new;
39
    }
40
41 40
    final public function value(mixed $value): static
42
    {
43 40
        $new = clone $this;
44 40
        $new->customValue = $value;
45 40
        $new->useCustomValue = true;
46 40
        return $new;
47
    }
48
49 481
    final protected function getName(): ?string
50
    {
51 481
        return $this->useCustomName
52 221
            ? $this->customName
53 481
            : $this->getInputData()->getName();
54
    }
55
56 468
    final protected function getValue(): mixed
57
    {
58 468
        return $this->useCustomValue
59 38
            ? $this->customValue
60 468
            : $this->getInputData()->getValue();
61
    }
62
}
63