PersistentMenu   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 95
Duplicated Lines 9.47 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 9
loc 95
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A create() 0 4 1
A setComposerInputDisabled() 0 6 1
A addButtons() 9 9 1
A getAllowedButtonsType() 0 8 1
A toArray() 0 10 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\ProfileSettings;
6
7
use Kerox\Messenger\Helper\ValidatorTrait;
8
use Kerox\Messenger\Model\Common\Button\AbstractButton;
9
10
class PersistentMenu implements ProfileSettingsInterface, \JsonSerializable
11
{
12
    use ValidatorTrait;
13
14
    /**
15
     * @var string
16
     */
17
    protected $locale;
18
19
    /**
20
     * @var bool
21
     */
22
    protected $composerInputDisabled = false;
23
24
    /**
25
     * @var \Kerox\Messenger\Model\Common\Button\AbstractButton[]
26
     */
27
    protected $buttons = [];
28
29
    /**
30
     * PersistentMenu constructor.
31
     *
32
     * @throws \Kerox\Messenger\Exception\MessengerException
33
     */
34 3
    public function __construct(string $locale = self::DEFAULT_LOCALE)
35
    {
36 3
        if ($locale !== self::DEFAULT_LOCALE) {
37 1
            $this->isValidLocale($locale);
38
        }
39
40 3
        $this->locale = $locale;
41 3
    }
42
43
    /**
44
     * @throws \Kerox\Messenger\Exception\MessengerException
45
     *
46
     * @return \Kerox\Messenger\Model\ProfileSettings\PersistentMenu
47
     */
48 3
    public static function create(string $locale = self::DEFAULT_LOCALE): self
49
    {
50 3
        return new self($locale);
51
    }
52
53
    /**
54
     * @return \Kerox\Messenger\Model\ProfileSettings\PersistentMenu
55
     */
56 3
    public function setComposerInputDisabled(bool $composerInputDisabled): self
57
    {
58 3
        $this->composerInputDisabled = $composerInputDisabled;
59
60 3
        return $this;
61
    }
62
63
    /**
64
     * @param \Kerox\Messenger\Model\Common\Button\AbstractButton[] $buttons
65
     *
66
     * @throws \Kerox\Messenger\Exception\MessengerException
67
     *
68
     * @return \Kerox\Messenger\Model\ProfileSettings\PersistentMenu
69
     */
70 3 View Code Duplication
    public function addButtons(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...
71
    {
72 3
        $this->isValidArray($buttons, 5);
73 3
        $this->isValidButtons($buttons, $this->getAllowedButtonsType());
74
75 1
        $this->buttons = $buttons;
76
77 1
        return $this;
78
    }
79
80 3
    protected function getAllowedButtonsType(): array
81
    {
82
        return [
83 3
            AbstractButton::TYPE_WEB_URL,
84
            AbstractButton::TYPE_POSTBACK,
85
            AbstractButton::TYPE_NESTED,
0 ignored issues
show
Deprecated Code introduced by
The constant Kerox\Messenger\Model\Co...ractButton::TYPE_NESTED has been deprecated with message: Since version 3.3.1 and will be removed in version 4.0.0.

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
86
        ];
87
    }
88
89 1
    public function toArray(): array
90
    {
91
        $array = [
92 1
            'locale' => $this->locale,
93 1
            'composer_input_disabled' => $this->composerInputDisabled,
94 1
            'call_to_actions' => $this->buttons,
95
        ];
96
97 1
        return array_filter($array);
98
    }
99
100 1
    public function jsonSerialize(): array
101
    {
102 1
        return $this->toArray();
103
    }
104
}
105