Passed
Pull Request — master (#159)
by Sergei
11:02
created

PlaceholderTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 31
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
/**
8
 * @psalm-import-type HtmlAttributes from \Yiisoft\Html\Html
9
 * @psalm-require-extends AbstractField
10
 */
11
trait PlaceholderTrait
12
{
13
    private ?string $placeholder = null;
14
    private bool $usePlaceholder = true;
15
16 1
    public function placeholder(?string $placeholder): self
17
    {
18 1
        $new = clone $this;
19 1
        $new->placeholder = $placeholder;
20 1
        return $new;
21
    }
22
23 3
    public function usePlaceholder(bool $use): self
24
    {
25 3
        $new = clone $this;
26 3
        $new->usePlaceholder = $use;
27 3
        return $new;
28
    }
29
30
    /**
31
     * @psalm-param HtmlAttributes $attributes
32
     */
33 29
    protected function preparePlaceholderInFormElementTagAttributes(array &$attributes): void
34
    {
35
        if (
36 29
            $this->usePlaceholder
37 29
            && !isset($attributes['placeholder'])
38
        ) {
39 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

39
            $placeholder = $this->placeholder ?? $this->/** @scrutinizer ignore-call */ getAttributePlaceholder();
Loading history...
40 25
            if ($placeholder !== null) {
41 11
                $attributes['placeholder'] = $placeholder;
42
            }
43
        }
44
        /** @psalm-var HtmlAttributes $attributes */
45 29
    }
46
}
47