Backend::help()   C
last analyzed

Complexity

Conditions 15
Paths 2

Size

Total Lines 75
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 15
eloc 49
c 2
b 0
f 1
nc 2
nop 0
dl 0
loc 75
rs 5.9166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
class Backend extends Handler {
3
    public function loading() {
4
        header("Content-type: text/html");
5
        print __("Loading, please wait...")." ".
6
            "<img src='images/indicator_tiny.gif'>";
7
    }
8
9
    public function digestTest() {
10
        if (isset($_SESSION['uid'])) {
11
            header("Content-type: text/html");
12
13
            $rv = Digest::prepare_headlines_digest($_SESSION['uid'], 1, 1000);
14
15
            print "<h1>HTML</h1>";
16
            print $rv[0];
17
            print "<h1>Plain text</h1>";
18
            print "<pre>".$rv[3]."</pre>";
19
        } else {
20
            print error_json(6);
21
        }
22
    }
23
24
    public function help() {
25
        $topic = clean_filename($_REQUEST["topic"]); // only one for now
26
27
        if ($topic == "main") {
28
            $info = get_hotkeys_info();
29
            $imap = get_hotkeys_map();
30
            $omap = array();
31
32
            foreach ($imap[1] as $sequence => $action) {
33
                if (!isset($omap[$action])) {
34
                    $omap[$action] = array();
35
                }
36
37
                array_push($omap[$action], $sequence);
38
            }
39
40
            print "<ul class='panel panel-scrollable hotkeys-help' style='height : 300px'>";
41
42
            $cur_section = "";
43
            foreach ($info as $section => $hotkeys) {
44
45
                if ($cur_section) {
46
                    print "<li>&nbsp;</li>";
47
                }
48
                print "<li><h3>".$section."</h3></li>";
49
                $cur_section = $section;
50
51
                foreach ($hotkeys as $action => $description) {
52
53
                    if (is_array($omap[$action])) {
54
                        foreach ($omap[$action] as $sequence) {
55
                            if (strpos($sequence, "|") !== false) {
56
                                $sequence = substr($sequence,
57
                                    strpos($sequence, "|") + 1,
58
                                    strlen($sequence));
59
                            } else {
60
                                $keys = explode(" ", $sequence);
61
62
                                for ($i = 0; $i < count($keys); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
63
                                    if (strlen($keys[$i]) > 1) {
64
                                        $tmp = '';
65
                                        foreach (str_split($keys[$i]) as $c) {
66
                                            switch ($c) {
67
                                            case '*':
68
                                                $tmp .= __('Shift').'+';
69
                                                break;
70
                                            case '^':
71
                                                $tmp .= __('Ctrl').'+';
72
                                                break;
73
                                            default:
74
                                                $tmp .= $c;
75
                                            }
76
                                        }
77
                                        $keys[$i] = $tmp;
78
                                    }
79
                                }
80
                                $sequence = join(" ", $keys);
81
                            }
82
83
                            print "<li>";
84
                            print "<div class='hk'><code>$sequence</code></div>";
85
                            print "<div class='desc'>$description</div>";
86
                            print "</li>";
87
                        }
88
                    }
89
                }
90
            }
91
92
            print "</ul>";
93
        }
94
95
        print "<footer class='text-center'>";
96
        print "<button dojoType='dijit.form.Button'
97
			onclick=\"return dijit.byId('helpDlg').hide()\">".__('Close this window')."</button>";
98
        print "</footer>";
99
100
    }
101
}
102