Passed
Pull Request — master (#159)
by Alexander
04:51 queued 02:29
created

PlaceholderTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A usePlaceholder() 0 5 1
A preparePlaceholderInFormElementTagAttributes() 0 9 4
A placeholder() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field\Base;
6
7
trait PlaceholderTrait
8
{
9
    private ?string $placeholder = null;
10
    private bool $usePlaceholder = true;
11
12 1
    public function placeholder(?string $placeholder): self
13
    {
14 1
        $new = clone $this;
15 1
        $new->placeholder = $placeholder;
16 1
        return $new;
17
    }
18
19 3
    public function usePlaceholder(bool $use): self
20
    {
21 3
        $new = clone $this;
22 3
        $new->usePlaceholder = $use;
23 3
        return $new;
24
    }
25
26 29
    protected function preparePlaceholderInFormElementTagAttributes(array &$attributes): void
27
    {
28
        if (
29 29
            $this->usePlaceholder
30 29
            && !isset($attributes['placeholder'])
31
        ) {
32 25
            $placeholder = $this->placeholder ?? $this->getAttributePlaceholder();
0 ignored issues
show
Bug introduced by
It seems like getAttributePlaceholder() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
            $placeholder = $this->placeholder ?? $this->/** @scrutinizer ignore-call */ getAttributePlaceholder();
Loading history...
33 25
            if ($placeholder !== null) {
34 11
                $attributes['placeholder'] = $placeholder;
35
            }
36
        }
37 29
    }
38
}
39