Completed
Push — master ( 8f5f7c...7faed5 )
by Basenko
04:23
created

OpenGraph::setDescription()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
cc 2
eloc 5
nc 1
nop 2
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
    public function setTitle($value, string $templateKey = ''): self
25
    {
26
        $this->openGraphService->setTitle($this->parseValue(
27
            $value,
28
            $templateKey ? new Title($value, $this->model, $templateKey) : Title::class
29
        ));
30
31
        return $this;
32
    }
33
34
    /** @param array|string $value */
35
    public function setDescription($value, string $templateKey = ''): self
36
    {
37
        $this->openGraphService->setDescription($this->parseValue(
38
            $value,
39
            $templateKey ? new Description($value, $this->model, $templateKey) : Description::class
40
        ));
41
42
        return $this;
43
    }
44
45
    public function setUrl(string $url): self
46
    {
47
        $this->openGraphService->setUrl($this->parseValue($url, Url::class));
48
49
        return $this;
50
    }
51
52
    public function setSiteName(string $name): self
53
    {
54
        $this->openGraphService->setSiteName($this->parseValue($name, SiteName::class));
55
56
        return $this;
57
    }
58
59
    /** @param array|string $images */
60
    public function setImages($images): self
61
    {
62
        $this->openGraphService->addImages([$this->parseValue($images, Images::class)]);
63
64
        return $this;
65
    }
66
67
    public function setProperties(array $properties): self
68
    {
69
        foreach ($this->parseValue($properties, Properties::class) as $item) {
70
            $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
        return $this;
74
    }
75
76
    /** @param array|string $value */
77
    public function addProperty(string $key, $value): self
78
    {
79
        $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
            ...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
                $this->parseValue([compact('key', 'value')], Properties::class)[0]
82
            )
83
        );
84
85
        return $this;
86
    }
87
88
    protected function getRawFields(): array
89
    {
90
        return $this->modelSeoData['open_graph'];
91
    }
92
}
93