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

InputDataWithCustomNameAndValueTrait::getName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
rs 10
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