Passed
Push — master ( 23879b...05dd78 )
by Caen
03:57 queued 14s
created

MetadataBag::addElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A MetadataBag::addGenericElement() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Features\Metadata;
6
7
use Hyde\Framework\Features\Metadata\Elements\LinkElement;
8
use Hyde\Framework\Features\Metadata\Elements\MetadataElement;
9
use Hyde\Framework\Features\Metadata\Elements\OpenGraphElement;
10
use Illuminate\Contracts\Support\Htmlable;
11
12
use function array_merge;
13
use function implode;
14
15
/**
16
 * Holds the metadata tags for a page or the site model.
17
 *
18
 * @see \Hyde\Framework\Features\Metadata\PageMetadataBag
19
 * @see \Hyde\Framework\Features\Metadata\GlobalMetadataBag
20
 */
21
class MetadataBag implements Htmlable
22
{
23
    /** @var array<string, MetadataElementContract> */
24
    protected array $links = [];
25
26
    /** @var array<string, MetadataElementContract> */
27
    protected array $metadata = [];
28
29
    /** @var array<string, MetadataElementContract> */
30
    protected array $properties = [];
31
32
    /** @var array<string> */
33
    protected array $generics = [];
34
35
    public function toHtml(): string
36
    {
37
        return $this->render();
38
    }
39
40
    public function render(): string
41
    {
42
        return implode("\n", $this->get());
43
    }
44
45
    public function get(): array
46
    {
47
        return array_merge(
48
            $this->getPrefixedArray('links'),
49
            $this->getPrefixedArray('metadata'),
50
            $this->getPrefixedArray('properties'),
51
            $this->getPrefixedArray('generics')
52
        );
53
    }
54
55
    public function add(MetadataElementContract|string $element): static
56
    {
57
        match (true) {
58
            $element instanceof LinkElement => $this->links[$element->uniqueKey()] = $element,
59
            $element instanceof MetadataElement => $this->metadata[$element->uniqueKey()] = $element,
60
            $element instanceof OpenGraphElement => $this->properties[$element->uniqueKey()] = $element,
61
            default => $this->addGenericElement((string) $element),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->addGenericElement((string)$element) targeting Hyde\Framework\Features\...ag::addGenericElement() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
62
        };
63
64
        return $this;
65
    }
66
67
    protected function addGenericElement(string $element): void
68
    {
69
        $this->generics[] = $element;
70
    }
71
72
    /** @return array<string, MetadataElementContract> */
73
    protected function getPrefixedArray(string $type): array
74
    {
75
        /** @var array<string, MetadataElementContract> $bag */
76
        $bag = $this->{$type};
77
78
        $array = [];
79
80
        foreach ($bag as $key => $element) {
81
            $array["$type:$key"] = $element;
82
        }
83
84
        return $array;
85
    }
86
}
87