OpenGraphElement   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 12 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 9
loc 75
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A create() 0 4 1
A setButtons() 9 9 1
A getAllowedButtonsType() 0 6 1
A toArray() 0 9 1
A jsonSerialize() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Model\Message\Attachment\Template\Element;
6
7
use Kerox\Messenger\Helper\ValidatorTrait;
8
use Kerox\Messenger\Model\Common\Button\AbstractButton;
9
10
class OpenGraphElement implements \JsonSerializable
11
{
12
    use ValidatorTrait;
13
14
    /**
15
     * @var string
16
     */
17
    protected $url;
18
19
    /**
20
     * @var \Kerox\Messenger\Model\Common\Button\AbstractButton[]
21
     */
22
    protected $buttons = [];
23
24
    /**
25
     * OpenGraphElement constructor.
26
     *
27
     * @throws \Kerox\Messenger\Exception\MessengerException
28
     */
29 2
    public function __construct(string $url)
30
    {
31 2
        $this->isValidUrl($url);
32
33 2
        $this->url = $url;
34 2
    }
35
36
    /**
37
     * @throws \Kerox\Messenger\Exception\MessengerException
38
     *
39
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\Element\OpenGraphElement
40
     */
41 2
    public static function create(string $url): self
42
    {
43 2
        return new self($url);
44
    }
45
46
    /**
47
     * @param \Kerox\Messenger\Model\Common\Button\AbstractButton[] $buttons
48
     *
49
     * @throws \Kerox\Messenger\Exception\MessengerException
50
     *
51
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\Element\OpenGraphElement
52
     */
53 2 View Code Duplication
    public function setButtons(array $buttons): self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55 2
        $this->isValidArray($buttons, 1);
56 2
        $this->isValidButtons($buttons, $this->getAllowedButtonsType());
57
58 1
        $this->buttons = $buttons;
59
60 1
        return $this;
61
    }
62
63 2
    protected function getAllowedButtonsType(): array
64
    {
65
        return [
66 2
            AbstractButton::TYPE_WEB_URL,
67
        ];
68
    }
69
70 1
    public function toArray(): array
71
    {
72
        $array = [
73 1
            'url' => $this->url,
74 1
            'buttons' => $this->buttons,
75
        ];
76
77 1
        return array_filter($array);
78
    }
79
80 1
    public function jsonSerialize(): array
81
    {
82 1
        return $this->toArray();
83
    }
84
}
85