Passed
Push — master ( 5e485e...857d21 )
by Sergei
02:50
created

InputDataWithCustomNameAndValueTrait   A

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 401
    final public function inputData(InputDataInterface $inputData): static
16
    {
17 401
        $new = clone $this;
18 401
        $new->inputData = $inputData;
19 401
        $new->useCustomName = false;
20 401
        $new->useCustomValue = false;
21 401
        return $new;
22
    }
23
24 402
    final protected function getInputData(): InputDataInterface
25
    {
26 402
        if ($this->inputData === null) {
27 2
            $this->inputData = new PureInputData();
28
        }
29
30 402
        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 3
    final public function name(?string $name): static
34
    {
35 3
        $new = clone $this;
36 3
        $new->customName = $name;
37 3
        $new->useCustomName = true;
38 3
        return $new;
39
    }
40
41 2
    final public function value(mixed $value): static
42
    {
43 2
        $new = clone $this;
44 2
        $new->customValue = $value;
45 2
        $new->useCustomValue = true;
46 2
        return $new;
47
    }
48
49 369
    final protected function getName(): ?string
50
    {
51 369
        return $this->useCustomName
52 2
            ? $this->customName
53 369
            : $this->getInputData()->getName();
54
    }
55
56 366
    final protected function getValue(): mixed
57
    {
58 366
        return $this->useCustomValue
59 1
            ? $this->customValue
60 366
            : $this->getInputData()->getValue();
61
    }
62
}
63