Test Setup Failed
Pull Request — master (#491)
by
unknown
07:16 queued 37s
created

PageCustomize::InitializeContent()   B

Complexity

Conditions 9
Paths 64

Size

Total Lines 72

Duplication

Lines 23
Ratio 31.94 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
nc 64
nop 0
dl 23
loc 72
ccs 0
cts 46
cp 0
crap 90
rs 7.0553
c 0
b 0
f 0

How to fix   Long Method   

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
/**
3
 * COPS (Calibre OPDS PHP Server) class file
4
 *
5
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6
 * @author     Sébastien Lucas <[email protected]>
7
 */
8
9
class PageCustomize extends Page
10
{
11
    private function isChecked ($key, $testedValue = 1) {
12
        $value = getCurrentOption ($key);
13
        if (is_array ($value)) {
14
            if (in_array ($testedValue, $value)) {
15
                return "checked='checked'";
16
            }
17
        } else {
18
            if ($value == $testedValue) {
19
                return "checked='checked'";
20
            }
21
        }
22
        return "";
23
    }
24
25
    private function isSelected ($key, $value) {
26
        if (getCurrentOption ($key) == $value) {
27
            return "selected='selected'";
28
        }
29
        return "";
30
    }
31
32 View Code Duplication
    private function getTemplateList () {
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...
33
        $result = array ();
34
        foreach (glob ("templates/*") as $filename) {
35
            if (preg_match ('/templates\/(.*)/', $filename, $m)) {
36
                array_push ($result, $m [1]);
37
            }            
38
        }
39
        return $result;
40
    }    
41
42 View Code Duplication
    private function getStyleList () {
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...
43
        $result = array ();
44
        foreach (glob ("templates/" . getCurrentTemplate () . "/styles/style-*.css") as $filename) {
45
            if (preg_match ('/styles\/style-(.*?)\.css/', $filename, $m)) {
46
                array_push ($result, $m [1]);
47
            }
48
        }
49
        return $result;
50
    }
51
52
    public function InitializeContent ()
53
    {
54
        $this->title = localize ("customize.title");
55
        $this->entryArray = array ();
56
57
        $ignoredBaseArray = array (PageQueryResult::SCOPE_AUTHOR,
58
                                   PageQueryResult::SCOPE_TAG,
59
                                   PageQueryResult::SCOPE_SERIES,
60
                                   PageQueryResult::SCOPE_PUBLISHER,
61
                                   PageQueryResult::SCOPE_RATING,
62
                                   "language");
63
64
        $content = "";
65 View Code Duplication
        if (!preg_match("/(Kobo|Kindle\/3.0|EBRD1101)/", $_SERVER['HTTP_USER_AGENT'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
66
            $content .= "<select id='template' onchange='updateCookie (this); window.location=$(\".headleft\").attr(\"href\");'>";
67
68
            foreach ($this-> getTemplateList () as $filename) {
69
                $content .= "<option value='{$filename}' " . $this->isSelected ("template", $filename) . ">{$filename}</option>";
70
            }
71
            $content .= '</select>';
72
        } else {
73
            foreach ($this-> getTemplateList () as $filename) {
74
                $content .= "<input type='radio' onchange='updateCookieFromCheckbox (this); window.location=$(\".headleft\").attr(\"href\");' id='template' name='template' value='{$filename}' " . $this->isChecked ("template", $filename) . " /><label for='template-{$filename}'> {$filename} </label>";
75
            }
76
        }
77
        array_push ($this->entryArray, new Entry ("Template", "",
78
                                        $content, "text",
79
                                        array ()));
80
81
        $content = "";
82 View Code Duplication
        if (!preg_match("/(Kobo|Kindle\/3.0|EBRD1101)/", $_SERVER['HTTP_USER_AGENT'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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
            $content .= '<select id="style" onchange="updateCookie (this);">';
84
            foreach ($this-> getStyleList () as $filename) {
85
                $content .= "<option value='{$filename}' " . $this->isSelected ("style", $filename) . ">{$filename}</option>";
86
            }
87
            $content .= '</select>';
88
        } else {
89
            foreach ($this-> getStyleList () as $filename) {
90
                $content .= "<input type='radio' onchange='updateCookieFromCheckbox (this);' id='style-{$filename}' name='style' value='{$filename}' " . $this->isChecked ("style", $filename) . " /><label for='style-{$filename}'> {$filename} </label>";
91
            }
92
        }
93
        array_push ($this->entryArray, new Entry (localize ("customize.style"), "",
94
                                        $content, "text",
95
                                        array ()));
96
        if (!useServerSideRendering ()) {
97
            $content = '<input type="checkbox" onchange="updateCookieFromCheckbox (this);" id="use_fancyapps" ' . $this->isChecked ("use_fancyapps") . ' />';
98
            array_push ($this->entryArray, new Entry (localize ("customize.fancybox"), "",
99
                                            $content, "text",
100
                                            array ()));
101
        }
102
        $content = '<input type="number" onchange="updateCookie (this);" id="max_item_per_page" value="' . getCurrentOption ("max_item_per_page") . '" min="-1" max="1200" pattern="^[-+]?[0-9]+$" />';
103
        array_push ($this->entryArray, new Entry (localize ("customize.paging"), "",
104
                                        $content, "text",
105
                                        array ()));
106
        $content = '<input type="text" onchange="updateCookie (this);" id="email" value="' . getCurrentOption ("email") . '" />';
107
        array_push ($this->entryArray, new Entry (localize ("customize.email"), "",
108
                                        $content, "text",
109
                                        array ()));
110
        $content = '<input type="checkbox" onchange="updateCookieFromCheckbox (this);" id="html_tag_filter" ' . $this->isChecked ("html_tag_filter") . ' />';
111
        array_push ($this->entryArray, new Entry (localize ("customize.filter"), "",
112
                                        $content, "text",
113
                                        array ()));
114
        $content = "";
115
        foreach ($ignoredBaseArray as $key) {
116
            $keyPlural = preg_replace ('/(ss)$/', 's', $key . "s");
117
            $content .=  '<input type="checkbox" name="ignored_categories[]" onchange="updateCookieFromCheckboxGroup (this);" id="ignored_categories_' . $key . '" ' . $this->isChecked ("ignored_categories", $key) . ' > ' . localize ("{$keyPlural}.title") . '</input> ';
118
        }
119
120
        array_push ($this->entryArray, new Entry (localize ("customize.ignored"), "",
121
                                        $content, "text",
122
                                        array ()));
123
    }
124
}
125