Passed
Pull Request — master (#192)
by Alexander
02:31
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 preparePlaceholderInInputTagAttributes() 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
/**
8
 * @psalm-require-extends AbstractField
9
 */
10
trait PlaceholderTrait
11
{
12
    private ?string $placeholder = null;
13
    private bool $usePlaceholder = true;
14
15 1
    public function placeholder(?string $placeholder): self
16
    {
17 1
        $new = clone $this;
18 1
        $new->placeholder = $placeholder;
19 1
        return $new;
20
    }
21
22 3
    public function usePlaceholder(bool $use): self
23
    {
24 3
        $new = clone $this;
25 3
        $new->usePlaceholder = $use;
26 3
        return $new;
27
    }
28
29 34
    protected function preparePlaceholderInInputTagAttributes(array &$attributes): void
30
    {
31
        if (
32 34
            $this->usePlaceholder
33 34
            && !isset($attributes['placeholder'])
34
        ) {
35 30
            $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

35
            $placeholder = $this->placeholder ?? $this->/** @scrutinizer ignore-call */ getAttributePlaceholder();
Loading history...
36 30
            if ($placeholder !== null) {
37 11
                $attributes['placeholder'] = $placeholder;
38
            }
39
        }
40
    }
41
}
42