Completed
Push — master ( d3bd06...5d3395 )
by Mark
8s
created

syntax_plugin_backlinks::render()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 55
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 2 Features 2
Metric Value
c 10
b 2
f 2
dl 0
loc 55
rs 7.4033
cc 8
eloc 34
nc 7
nop 3

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
 * DokuWiki Syntax Plugin Backlinks.
4
 *
5
 * Shows a list of pages that link back to a given page.
6
 *
7
 * Syntax:  {{backlinks>[pagename][#filterNS|!#filterNS]}}
8
 *
9
 *   [pagename] - a valid wiki pagename or a . for the current page
10
 *   [filterNS] - a valid,absolute namespace name, optionally prepended with ! to exclude
11
 *
12
 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
13
 * @author  Michael Klier <[email protected]>
14
 * @author  Mark C. Prins <[email protected]>
15
 */
16
if(!defined('DOKU_INC')) die();
17
18
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
19
if(!defined('DW_LF')) define('DW_LF',"\n");
20
21
require_once(DOKU_PLUGIN.'syntax.php');
22
require_once(DOKU_INC.'inc/parserutils.php');
23
24
/**
25
 * All DokuWiki plugins to extend the parser/rendering mechanism
26
 * need to inherit from this class
27
 */
28
class syntax_plugin_backlinks extends DokuWiki_Syntax_Plugin {
29
    /**
30
     * Syntax Type.
31
     *
32
     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
33
     * @see DokuWiki_Syntax_Plugin::getType()
34
     */
35
    function getType()  { return 'substition'; }
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
36
37
    /**
38
     * @see DokuWiki_Syntax_Plugin::getPType()
39
     */
40
    function getPType() { return 'block'; }
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
41
42
    /**
43
     * @see Doku_Parser_Mode::getSort()
44
     */
45
    function getSort()  { return 304; }
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
46
47
    /**
48
     * Connect pattern to lexer.
49
     * @see Doku_Parser_Mode::connectTo()
50
     */
51
    function connectTo($mode) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
52
        $this->Lexer->addSpecialPattern('\{\{backlinks>.+?\}\}', $mode, 'plugin_backlinks');
53
    }
54
55
    /**
56
     * Handler to prepare matched data for the rendering process.
57
     * @see DokuWiki_Syntax_Plugin::handle()
58
     */
59
    function handle($match, $state, $pos, Doku_Handler $handler){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
60
61
        // Take the id of the source
62
        // It can be a rendering of a sidebar
63
        global $INFO;
64
        global $ID;
65
        $id = $ID;
66
        // If it's a sidebar, get the original id.
67
        if ($INFO != null) {
68
            $id = $INFO['id'];
69
        }
70
71
        $match = substr($match,12,-2); //strip {{backlinks> from start and }} from end
72
73
        if(strstr($match, "#")){
74
            $includeNS = substr(strstr($match, "#", FALSE), 1);
75
            $match= strstr($match, "#", TRUE);
76
        }
77
78
        $match = ($match == '.') ? $id : $match;
79
80
        if(strstr($match,".:")) {
81
            resolve_pageid(getNS($id),$match,$exists);
0 ignored issues
show
Bug introduced by
The variable $exists does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
82
        }
83
84
        return (array($match, $includeNS));
0 ignored issues
show
Bug introduced by
The variable $includeNS does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
85
    }
86
87
    /**
88
     * Handles the actual output creation.
89
     * @see DokuWiki_Syntax_Plugin::render()
90
     */
91
    function render($mode, Doku_Renderer $renderer, $data) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
92
        global $lang;
93
94
        if($mode == 'xhtml'){
95
            $renderer->info['cache'] = false;
96
97
            @require_once(DOKU_INC.'inc/fulltext.php');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
98
            $backlinks = ft_backlinks($data[0]);
99
100
            dbglog($backlinks, "backlinks: all backlinks to: $data[0]");
101
102
            $renderer->doc .= '<div id="plugin__backlinks">' . DW_LF;
103
104
            $filterNS = $data[1];
105
            if(!empty($backlinks) && !empty($filterNS)) {
106
                if (stripos($filterNS, "!", 0) === 0) {
107
                    $filterNS = substr($filterNS, 1);
108
                    dbglog($filterNS, "backlinks: exluding all of namespace: $filterNS");
109
                    $backlinks= array_filter($backlinks, function($ns) use($filterNS) {
110
                        return stripos($ns, $filterNS, 0) !== 0;
111
                    });
112
                } else {
113
                    dbglog($filterNS, "backlinks: including namespace: $filterNS only");
114
                    $backlinks= array_filter($backlinks, function($ns) use($filterNS) {
115
                        return stripos($ns, $filterNS, 0) === 0;
116
                    });
117
                }
118
            }
119
120
            dbglog($backlinks, "backlinks: all backlinks to be rendered");
121
122
            if(!empty($backlinks)) {
123
124
                $renderer->doc .= '<ul class="idx">';
125
126
                foreach($backlinks as $backlink){
127
                    $name = p_get_metadata($backlink, 'title');
128
                    if(empty($name)) $name = $backlink;
129
                    $renderer->doc .= '<li><div class="li">';
130
                    //$renderer->doc .= html_wikilink(':'.$backlink, $name);
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
131
                    $renderer->doc .= '<a href="'.wl(':'.$backlink).'">'.$name.'</a>';
132
                    $renderer->doc .= '</div></li>' . DW_LF;
133
                }
134
135
                $renderer->doc .= '</ul>' . DW_LF;
136
            } else {
137
                $renderer->doc .= "<strong>Plugin Backlinks: " . $lang['nothingfound'] . "</strong>" . DW_LF;
138
            }
139
140
            $renderer->doc .= '</div>' . DW_LF;
141
142
            return true;
143
        }
144
        return false;
145
    }
146
}
147