PlaceholderTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 15
c 1
b 0
f 0
dl 0
loc 33
ccs 14
cts 14
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A usePlaceholder() 0 5 1
A preparePlaceholderInInputAttributes() 0 9 4
A placeholder() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field\Base\Placeholder;
6
7
use Yiisoft\Form\Field\Base\InputData\InputDataInterface;
8
9
/**
10
 * @psalm-require-extends \Yiisoft\Form\Field\Base\InputField
11
 */
12
trait PlaceholderTrait
13
{
14
    private ?string $placeholder = null;
15
    private bool $usePlaceholder = true;
16
17 6
    public function placeholder(?string $placeholder): self
18
    {
19 6
        $new = clone $this;
20 6
        $new->placeholder = $placeholder;
21 6
        return $new;
22
    }
23
24 8
    public function usePlaceholder(bool $use): self
25
    {
26 8
        $new = clone $this;
27 8
        $new->usePlaceholder = $use;
28 8
        return $new;
29
    }
30
31 225
    protected function preparePlaceholderInInputAttributes(array &$attributes): void
32
    {
33
        if (
34 225
            $this->usePlaceholder
35 225
            && !isset($attributes['placeholder'])
36
        ) {
37 220
            $placeholder = $this->placeholder ?? $this->getInputData()->getPlaceholder();
38 220
            if ($placeholder !== null) {
39 13
                $attributes['placeholder'] = $placeholder;
40
            }
41
        }
42
    }
43
44
    abstract protected function getInputData(): InputDataInterface;
45
}
46