Failed Conditions
Pull Request — master (#3198)
by
unknown
03:06
created

Recent::show()   F

Complexity

Conditions 27
Paths > 20000

Size

Total Lines 223

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 27
nc 276864
nop 2
dl 0
loc 223
rs 0
c 0
b 0
f 0

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
3
namespace dokuwiki\Ui;
4
5
use dokuwiki\ChangeLog\MediaChangeLog;
6
use dokuwiki\Extension\Event;
7
use dokuwiki\Form\Form;
8
9
/**
10
 * DokuWiki Recent Insterface
11
 *
12
 * @package dokuwiki\Ui
13
 */
14
class Recent extends Ui
15
{
16
    /**
17
     * Display recent changes
18
     *
19
     * @author Andreas Gohr <[email protected]>
20
     * @author Matthias Grimm <[email protected]>
21
     * @author Ben Coburn <[email protected]>
22
     * @author Kate Arzamastseva <[email protected]>
23
     *
24
     * @triggers HTML_RECENTFORM_OUTPUT
25
     * @param int $first skip the first n changelog lines
26
     * @param string $show_changes
27
     * @return void
28
     */
29
    public function show($first = 0, $show_changes = 'both')
30
    {
31
        global $conf;
32
        global $lang;
33
        global $ID;
34
35
        /* we need to get one additionally log entry to be able to
36
         * decide if this is the last page or is there another one.
37
         * This is the cheapest solution to get this information.
38
         */
39
        $flags = 0;
40
        if ($show_changes == 'mediafiles' && $conf['mediarevisions']) {
41
            $flags = RECENTS_MEDIA_CHANGES;
42
        } elseif ($show_changes == 'pages') {
43
            $flags = 0;
44
        } elseif ($conf['mediarevisions']) {
45
            $show_changes = 'both';
46
            $flags = RECENTS_MEDIA_PAGES_MIXED;
47
        }
48
49
        $recents = getRecents($first, $conf['recent'] + 1, getNS($ID), $flags);
0 ignored issues
show
Security Bug introduced by
It seems like getNS($ID) targeting getNS() can also be of type false; however, getRecents() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
50
        if (count($recents) == 0 && $first != 0) {
51
            $first = 0;
52
            $recents = getRecents($first, $conf['recent'] + 1, getNS($ID), $flags);
0 ignored issues
show
Security Bug introduced by
It seems like getNS($ID) targeting getNS() can also be of type false; however, getRecents() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
53
        }
54
55
        $hasNext = false;
56
        if (count($recents) > $conf['recent']) {
57
            $hasNext = true;
58
            array_pop($recents); // remove extra log entry
59
        }
60
61
        // print intro
62
        print p_locale_xhtml('recent');
63
64
        if (getNS($ID) != '') {
65
            print '<div class="level1"><p>'
66
                . sprintf($lang['recent_global'], getNS($ID), wl('', 'do=recent'))
67
                .'</p></div>';
68
        }
69
70
        // create the form
71
        $form = new Form(['id' => 'dw__recent', 'method' => 'GET', 'action'=>wl($ID)]);
72
        $form->addClass('changes');
73
        $form->addTagOpen('div')->addClass('no');
74
        $form->setHiddenField('sectok', null);
75
        $form->setHiddenField('do', 'recent');
76
        $form->setHiddenField('id', $ID);
77
78
        // show dropdown selector, whether include not only recent pages but also recent media files?
79
        if ($conf['mediarevisions']) {
80
            $form->addTagOpen('div')->addClass('changeType');
81
            $options = array(
82
                            'pages'      => $lang['pages_changes'],
83
                            'mediafiles' => $lang['media_changes'],
84
                            'both'       => $lang['both_changes'],
85
            );
86
            $form->addDropdown('show_changes', $options, $lang['changes_type'])
87
                ->val($show_changes)->addClass('quickselect');
88
            $form->addButton('do[recent]', $lang['btn_apply'])->attr('type','submit');
89
            $form->addTagClose('div');
90
        }
91
92
        // start listing
93
        $form->addTagOpen('ul');
94
95
        foreach ($recents as $recent) {
96
            $date = dformat($recent['date']);
97
            $class = ($recent['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT) ? 'minor': '';
98
99
            $form->addTagOpen('li')->addClass($class);
100
            $form->addTagOpen('div')->addClass('li');
101
102
            if (!empty($recent['media'])) {
103
                $form->addHTML(media_printicon($recent['id']));
104
            } else {
105
                $form->addTag('img')->attrs([
106
                        'src' => DOKU_BASE .'lib/images/fileicons/file.png',
107
                        'alt' => $recent['id']
108
                ])->addClass('icon');
109
            }
110
111
            $form->addTagOpen('span')->addClass('date');
112
            $form->addHTML($date);
113
            $form->addTagClose('span');
114
115
            $diff = false;
116
            $href = '';
117
118
            if (!empty($recent['media'])) {
119
                $changelog = new MediaChangeLog($recent['id']);
120
                $revs = $changelog->getRevisions(0, 1);
121
                $diff = (count($revs) && file_exists(mediaFN($recent['id'])));
122
                if ($diff) {
123
                    $href = media_managerURL(
124
                        array(
125
                            'tab_details' => 'history',
126
                            'mediado' => 'diff',
127
                            'image' => $recent['id'],
128
                            'ns' => getNS($recent['id'])
129
                        ), '&'
130
                    );
131
                }
132
            } else {
133
                $href = wl($recent['id'], "do=diff", false, '&');
134
            }
135
136
            if (!empty($recent['media']) && !$diff) {
137
                $form->addTag('img')->attrs([
138
                        'src'    => DOKU_BASE .'lib/images/blank.gif',
139
                        'width'  => 15,
140
                        'height' => 11,
141
                        'alt' => '',
142
                ]);
143
            } else {
144
                $form->addTagOpen('a')->attr('href', $href)->addClass('diff_link');
0 ignored issues
show
Bug introduced by
It seems like $href defined by media_managerURL(array('...S($recent['id'])), '&') on line 123 can also be of type array; however, dokuwiki\Form\Element::attr() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
145
                $form->addTag('img')->attrs([
146
                        'src'    => DOKU_BASE .'lib/images/diff.png',
147
                        'width'  => 15,
148
                        'height' => 11,
149
                        'title'  => $lang['diff'],
150
                        'alt'    => $lang['diff'],
151
                ]);
152
                $form->addTagClose('a');
153
            }
154
155
            if (!empty($recent['media'])) {
156
                $href = media_managerURL(
157
                    array(
158
                        'tab_details' => 'history',
159
                        'image' => $recent['id'],
160
                        'ns' => getNS($recent['id'])
161
                    ), '&'
162
                );
163
            } else {
164
                $href = wl($recent['id'], "do=revisions", false, '&');
165
            }
166
            $form->addTagOpen('a')->attr('href', $href)->addClass('revisions_link');
0 ignored issues
show
Bug introduced by
It seems like $href defined by media_managerURL(array('...S($recent['id'])), '&') on line 156 can also be of type array; however, dokuwiki\Form\Element::attr() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
167
            $form->addTag('img')->attrs([
168
                    'src'    => DOKU_BASE .'lib/images/history.png',
169
                    'width'  => 12,
170
                    'height' => 14,
171
                    'title'  => $lang['btn_revs'],
172
                    'alt'    => $lang['btn_revs']
173
            ]);
174
            $form->addTagClose('a');
175
176
            if (!empty($recent['media'])) {
177
                $href = media_managerURL(
178
                    array(
179
                        'tab_details' => 'view',
180
                        'image' => $recent['id'],
181
                        'ns' => getNS($recent['id'])
182
                    ), '&'
183
                );
184
                $class = file_exists(mediaFN($recent['id'])) ? 'wikilink1' : 'wikilink2';
185
                $form->addTagOpen('a')->attr('href', $href)->addClass($class);
0 ignored issues
show
Bug introduced by
It seems like $href defined by media_managerURL(array('...S($recent['id'])), '&') on line 177 can also be of type array; however, dokuwiki\Form\Element::attr() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
186
                $form->addHTML($recent['id']);
187
                $form->addTagClose('a');
188
            } else {
189
                $form->addHTML(html_wikilink(':'. $recent['id'], useHeading('navigation') ? null : $recent['id']));
190
            }
191
            $form->addTagOpen('span')->addClass('sum');
192
            $form->addHTML(' – '. hsc($recent['sum']));
193
            $form->addTagClose('span');
194
195
            $form->addTagOPen('span')->addClass('user');
196
            if ($recent['user']) {
197
                $form->addHTML('<bdi>'. editorinfo($recent['user']) .'</bdi>');
198
                if (auth_ismanager()) {
199
                    $form->addHTML(' <bdo dir="ltr">('. $recent['ip'] .')</bdo>');
200
                }
201
            } else {
202
                $form->addHTML('<bdo dir="ltr">'. $recent['ip'] .'</bdo>');
203
            }
204
            $form->addTagClose('span');
205
206
            html_sizechange($recent['sizechange'], $form);
0 ignored issues
show
Documentation introduced by
$form is of type object<dokuwiki\Form\Form>, but the function expects a object<Form>|object<Doku_Form>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
207
208
            $form->addTagClose('div');
209
            $form->addTagClose('li');
210
        }
211
212
        $form->addTagClose('ul');
213
214
        // provide navigation for pagenated cecent list (of pages and/or media files)
215
        $form->addTagOpen('div')->addClass('pagenav');
216
        $last = $first + $conf['recent'];
217
        if ($first > 0) {
218
            $first = $first - $conf['recent'];
219
            if ($first < 0) $first = 0;
220
            $form->addTagOpen('div')->addClass('pagenav-prev');
221
            $form->addTagOpen('button')->attrs([
222
                    'type'      => 'submit',
223
                    'name'      => 'first['. $first .']',
224
                    'accesskey' => 'n',
225
                    'title'     => $lang['btn_newer'] .' [N]',
226
            ])->addClass('button show');
227
            $form->addHTML($lang['btn_newer']);
228
            $form->addTagClose('button');
229
            $form->addTagClose('div');
230
        }
231
        if ($hasNext) {
232
            $form->addTagOpen('div')->addClass('pagenav-next');
233
            $form->addTagOpen('button')->attrs([
234
                    'type'      => 'submit',
235
                    'name'      => 'first['. $last .']',
236
                    'accesskey' => 'p',
237
                    'title'     => $lang['btn_older'] .' [P]',
238
            ])->addClass('button show');
239
            $form->addHTML($lang['btn_older']);
240
            $form->addTagClose('button');
241
            $form->addTagClose('div');
242
        }
243
        $form->addTagClose('div');
244
245
        $form->addTagClose('div'); // close div class=no
246
247
        // emit HTML_CRECENTFORM_OUTPUT event, print the form
248
        Event::createAndTrigger('HTML_RECENTFORM_OUTPUT', $form, 'html_form_output', false);
249
250
        print DOKU_LF;
251
    }
252
253
}
254