Completed
Push — master ( df8184...e3b994 )
by Aydin
02:31
created

BuilderUtils   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 32.69 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 34
loc 104
c 0
b 0
f 0
wmc 10
lcom 1
cbo 5
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addItem() 0 10 1
A addStaticItem() 0 6 1
A addLineBreak() 0 6 1
A addSubMenu() 17 17 2
A addSubMenuFromExistingBuilder() 17 17 2
A buildSubMenus() 0 17 3

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
namespace PhpSchool\CliMenu\Builder;
4
5
use PhpSchool\CliMenu\MenuItem\LineBreakItem;
6
use PhpSchool\CliMenu\MenuItem\MenuMenuItem;
7
use PhpSchool\CliMenu\MenuItem\SelectableItem;
8
use PhpSchool\CliMenu\MenuItem\StaticItem;
9
use RuntimeException;
10
11
/**
12
 * @author Aydin Hassan <[email protected]>
13
 */
14
trait BuilderUtils
15
{
16
    /**
17
     * @var self[]
18
     */
19
    private $subMenuBuilders = [];
20
21
    /**
22
     * @var CliMenu[]
23
     */
24
    private $subMenus = [];
25
26
    /**
27
     * @var array
28
     */
29
    private $menuItems = [];
30
31
    public function addItem(
32
        string $text,
33
        callable $itemCallable,
34
        bool $showItemExtra = false,
35
        bool $disabled = false
36
    ) : self {
37
        $this->menuItems[] = new SelectableItem($text, $itemCallable, $showItemExtra, $disabled);
38
39
        return $this;
40
    }
41
42
    public function addStaticItem(string $text) : self
43
    {
44
        $this->menuItems[] = new StaticItem($text);
45
46
        return $this;
47
    }
48
49
    public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self
50
    {
51
        $this->menuItems[] = new LineBreakItem($breakChar, $lines);
52
53
        return $this;
54
    }
55
    
56
    /**
57
     * Add a submenu with a unique ID and the text. The text will be displayed as the item text
58
     * in the parent menu.
59
     */
60 View Code Duplication
    public function addSubMenu(string $id, string $text) : CliMenuBuilder
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...
61
    {
62
        if (isset($this->subMenuBuilders[$id])) {
63
            throw new \InvalidArgumentException(
64
                sprintf('SubMenu with id: "%s" already exists. $id must be unique', $id)
65
            );
66
        }
67
        
68
        $this->menuItems[] = [
69
            'type' => 'submenu-placeholder',
70
            'text' => $text,
71
            'id'   => $id
72
        ];
73
        
74
        $this->subMenuBuilders[$id] = new CliMenuBuilder($this);
75
        return $this->subMenuBuilders[$id];
76
    }
77
78
    /**
79
     * Add a submenu from an existing builder. Required a unique ID and the text. The text will be displayed as the
80
     * item text in the parent menu.
81
     */
82 View Code Duplication
    public function addSubMenuFromExistingBuilder(string $id, string $text, CliMenuBuilder $subMenuBuilder) : 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...
83
    {
84
        if (isset($this->subMenuBuilders[$id])) {
85
            throw new \InvalidArgumentException(
86
                sprintf('SubMenu with id: "%s" already exists. $id must be unique', $id)
87
            );
88
        }
89
90
        $this->menuItems[] = [
91
            'type' => 'submenu-placeholder',
92
            'text' => $text,
93
            'id'   => $id
94
        ];
95
96
        $this->subMenuBuilders[$id] = $subMenuBuilder;
97
        return $this;
98
    }
99
100
    private function buildSubMenus(array $items) : array
101
    {
102
        return array_map(function ($item) {
103
            if (!is_array($item) || $item['type'] !== 'submenu-placeholder') {
104
                return $item;
105
            }
106
107
            $menuBuilder                 = $this->subMenuBuilders[$item['id']];
108
            $this->subMenus[$item['id']] = $menuBuilder->build();
109
110
            return new MenuMenuItem(
111
                $item['text'],
112
                $this->subMenus[$item['id']],
113
                $menuBuilder->isMenuDisabled()
114
            );
115
        }, $items);
116
    }
117
}
118