Passed
Push — master ( 9e7cad...7929d2 )
by Caen
03:29 queued 12s
created

LinkElement::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Hyde\Framework\Modules\Metadata\Models;
4
5
class LinkElement extends BaseMetadataElement
6
{
7
    protected string $rel;
8
    protected string $href;
9
    protected array $attr = [];
10
11
    public function __construct(string $rel, string $href, array $attr = [])
12
    {
13
        $this->rel = $rel;
14
        $this->href = $href;
15
        $this->attr = $attr;
16
    }
17
18
    public function __toString(): string
19
    {
20
        return sprintf('<link rel="%s" href="%s"%s>', e($this->rel), e($this->href), $this->formatOptionalAttributes());
21
    }
22
23
    public function uniqueKey(): string
24
    {
25
        return $this->rel;
26
    }
27
28
    protected function formatOptionalAttributes(): string
29
    {
30
        if (empty($this->attr)) {
31
            return '';
32
        }
33
34
        return sprintf(' %s', collect($this->attr)->map(function ($value, $key) {
0 ignored issues
show
Bug introduced by
$this->attr of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

34
        return sprintf(' %s', collect(/** @scrutinizer ignore-type */ $this->attr)->map(function ($value, $key) {
Loading history...
35
            return e($key).'="'.e($value).'"';
36
        })->implode(' '));
37
    }
38
}
39