Completed
Push — master ( e600ef...7ac924 )
by Basenko
07:00
created

OpenGraph::addProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
3
namespace MadWeb\Seoable\Protocols;
4
5
use MadWeb\Seoable\Fields\OpenGraph\Url;
6
use MadWeb\Seoable\Fields\OpenGraph\Title;
7
use MadWeb\Seoable\Fields\OpenGraph\Images;
8
use MadWeb\Seoable\Fields\OpenGraph\SiteName;
9
use MadWeb\Seoable\Fields\OpenGraph\Properties;
10
use MadWeb\Seoable\Fields\OpenGraph\Description;
11
12
/**
13
 * @method OpenGraph setTitleRaw(string $title)
14
 * @method OpenGraph setDescriptionRaw(string $description)
15
 * @method OpenGraph setUrlRaw(string $url)
16
 * @method OpenGraph setSiteNameRaw(string $name)
17
 * @method OpenGraph setImagesRaw(array|string $images)
18
 * @method OpenGraph setPropertiesRaw(array $properties)
19
 * @method OpenGraph addPropertyRaw(string $key, mixed $value)
20
 */
21
class OpenGraph extends Protocol
22
{
23
    /** @param array|string $value */
24 3
    public function setTitle($value, string $templateKey = ''): self
25
    {
26 3
        $this->openGraphService->setTitle($this->parseValue(
27 3
            $value,
28 3
            $templateKey ? new Title($value, $this->model, $templateKey) : Title::class
29
        ));
30
31 3
        return $this;
32
    }
33
34
    /** @param array|string $value */
35 3
    public function setDescription($value, string $templateKey = ''): self
36
    {
37 3
        $this->openGraphService->setDescription($this->parseValue(
38 3
            $value,
39 3
            $templateKey ? new Description($value, $this->model, $templateKey) : Description::class
40
        ));
41
42 3
        return $this;
43
    }
44
45 3
    public function setUrl(string $url): self
46
    {
47 3
        $this->openGraphService->setUrl($this->parseValue($url, Url::class));
48
49 3
        return $this;
50
    }
51
52 3
    public function setSiteName(string $name): self
53
    {
54 3
        $this->openGraphService->setSiteName($this->parseValue($name, SiteName::class));
55
56 3
        return $this;
57
    }
58
59
    /** @param array|string $images */
60 3
    public function setImages($images): self
61
    {
62 3
        $this->openGraphService->addImages([$this->parseValue($images, Images::class)]);
63
64 3
        return $this;
65
    }
66
67 3
    public function setProperties(array $properties): self
68
    {
69 3
        foreach ($this->parseValue($properties, Properties::class) as $item) {
70 3
            $this->openGraphService->addProperty(...array_values($item));
0 ignored issues
show
Bug introduced by
The call to addProperty() misses a required argument $value.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
array_values($item) is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
        }
72
73 3
        return $this;
74
    }
75
76
    /** @param array|string $value */
77 3
    public function addProperty(string $key, $value): self
78
    {
79 3
        $this->openGraphService->addProperty(
0 ignored issues
show
Bug introduced by
The call to addProperty() misses a required argument $value.

This check looks for function calls that miss required arguments.

Loading history...
80 3
            ...array_values(
0 ignored issues
show
Documentation introduced by
array_values($this->pars...\Properties::class)[0]) is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
81 3
                $this->parseValue([compact('key', 'value')], Properties::class)[0]
82
            )
83
        );
84
85 3
        return $this;
86
    }
87
88 3
    protected function getRawFields(): array
89
    {
90 3
        return $this->modelSeoData['open_graph'] ?? [];
91
    }
92
}
93