Completed
Push — master ( 9b78f7...5dcb4f )
by Andreas
08:15 queued 03:39
created

Edit   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 0 34 7
A setIcon() 0 12 2
1
<?php
2
3
namespace dokuwiki\Menu\Item;
4
5
/**
6
 * Class Edit
7
 *
8
 * Most complex item. Shows the edit button but mutates to show, draft and create based on
9
 * current state.
10
 */
11
class Edit extends AbstractItem {
12
13
    /** @inheritdoc */
14
    public function __construct() {
15
        global $ACT;
16
        global $INFO;
17
        global $REV;
18
19
        parent::__construct();
20
21
        if($ACT == 'show' || $ACT == 'search') {
22
            $this->method = 'post';
23
            if($INFO['writable']) {
24
                $this->accesskey = 'e';
25
                if(!empty($INFO['draft'])) {
26
                    $this->type = 'draft';
27
                    $this->params['do'] = 'draft';
28
                } else {
29
                    $this->params['rev'] = $REV;
30
                    if(!$INFO['exists']) {
31
                        $this->type = 'create';
32
                    }
33
                }
34
            } else {
35
                if(!actionOK($this->type)) throw new \RuntimeException("action disabled: source");
36
                $params['rev'] = $REV;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
37
                $this->type = 'source';
38
                $this->accesskey = 'v';
39
            }
40
        } else {
41
            $this->params = array('do' => '');
42
            $this->type = 'show';
43
            $this->accesskey = 'v';
44
        }
45
46
        $this->setIcon();
47
    }
48
49
    /**
50
     * change the icon according to what type the edit button has
51
     */
52
    protected function setIcon() {
53
        $icons = array(
54
            'edit' => '01-edit_pencil.svg',
55
            'create' => '02-create_pencil.svg',
56
            'draft' => '03-draft_android-studio.svg',
57
            'show' => '04-show_file-document.svg',
58
            'source' => '05-source_file-xml.svg',
59
        );
60
        if(isset($icons[$this->type])) {
61
            $this->svg = DOKU_INC . 'lib/images/menu/' . $icons[$this->type];
62
        }
63
    }
64
65
}
66