Completed
Push — master ( dc0d47...ca555a )
by Michael
05:19 queued 02:43
created

NavegadorProva::renderImageNav()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 62
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 44
nc 6
nop 3
dl 0
loc 62
rs 6.943
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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 32 and the first side effect is on line 27.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
// $Id: navegacao.php,v 1.6 2007/03/24 17:50:52 marcellobrandao Exp $
3
//  ------------------------------------------------------------------------ //
4
//                XOOPS - PHP Content Management System                      //
5
//                    Copyright (c) 2000 XOOPS.org                           //
6
//                       <http://www.xoops.org/>                             //
7
//  ------------------------------------------------------------------------ //
8
//  This program is free software; you can redistribute it and/or modify     //
9
//  it under the terms of the GNU General Public License as published by     //
10
//  the Free Software Foundation; either version 2 of the License, or        //
11
//  (at your option) any later version.                                      //
12
//                                                                           //
13
//  You may not change or alter any portion of this comment or credits       //
14
//  of supporting developers from this source code or any supporting         //
15
//  source code which is considered copyrighted (c) material of the          //
16
//  original comment or credit authors.                                      //
17
//                                                                           //
18
//  This program is distributed in the hope that it will be useful,          //
19
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21
//  GNU General Public License for more details.                             //
22
//                                                                           //
23
//  You should have received a copy of the GNU General Public License        //
24
//  along with this program; if not, write to the Free Software              //
25
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26
//  ------------------------------------------------------------------------ //
27
include_once XOOPS_ROOT_PATH . '/class/pagenav.php';
28
29
/**
30
 * Class NavegadorProva
31
 */
32
class NavegadorProva extends XoopsPageNav
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
33
{
34
    /**
35
     * Create navigation with images
36
     *
37
     * @param int     $cod_perguntas
0 ignored issues
show
Documentation introduced by
Should the type for parameter $cod_perguntas not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
38
     * @param         $cod_perguntas_respondidas
39
     * @param integer $offset
40
     *
41
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be null|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
42
     */
43
    public function renderImageNav($cod_perguntas = null, $cod_perguntas_respondidas = null, $offset = 1)
44
    {
45
        global $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
46
        $vazio     = array();
47
        $largura   = 50 * $offset;
48
        $perguntas = @array_merge($perguntas, $vazio);
0 ignored issues
show
Bug introduced by
The variable $perguntas seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Unused Code introduced by
$perguntas is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
49
        if ($this->total < $this->perpage) {
50
            return null;
51
        }
52
        $total_pages = ceil($this->total / $this->perpage);
53
54
        if ($total_pages > 1) {
55
            $ret = '<table align="center" style="width:' . $largura . 'px;"><tr>';
56
57
            $prev = $this->current - $this->perpage;
58
            if ($prev >= 0) {
59
                $ret .= '<td align="right"><a href="' . $this->url . $prev . $this->extra . '">
60
                <img src="assets/images/anterior.gif" border="0" alt="' . _MA_ASSESSMENT_ANTERIOR . '" title="' . _MA_ASSESSMENT_ANTERIOR . '"></a></td> ';
61
            }
62
            $ret  .= '<td align="center"><table><tr>';
63
            $next = $this->current + $this->perpage;
64
65
            $counter      = 1;
66
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
0 ignored issues
show
Unused Code introduced by
$current_page is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
67
            while ($counter <= $total_pages) {
68
                $cod_pergunta_atual = $cod_perguntas[$counter - 1];
69
70
                if (in_array($cod_pergunta_atual, $cod_perguntas_respondidas)) {
71
                    $ret .= '<td  valign="center" style="height=20px; text-align:center; background:url(assets/images/feita.jpg); background-repeat:no-repeat;background-position:center;">
72
                    <a rel="sad" href="'
73
                            . $this->url
74
                            . (($counter - 1) * $this->perpage)
75
                            . $this->extra
76
                            . '">'
77
                            . $counter
78
                            . '</a></td>';
79
                } else {
80
                    $ret .= '<td  valign="center" style="height=20px; text-align:center; background:url(assets/images/naofeita.jpg); background-repeat:no-repeat;background-position:center;">
81
                    <a href="'
82
                            . $this->url
83
                            . (($counter - 1) * $this->perpage)
84
                            . $this->extra
85
                            . '">'
86
                            . $counter 
87
                            . '</a></td>';
88
                }
89
90
                if ((($counter % $offset) == 0) & $counter != $total_pages - 1) {
91
                    $ret .= '</tr><tr>';
92
                }
93
                ++$counter;
94
            }
95
            $ret .= '</tr></table></td>';
96
            if ($this->total > $next) {
97
                $ret .= '<td valign="middle"><a href="' . $this->url . $next . $this->extra . '"><img src="assets/images/proximo.gif" border="0" alt="' . _MA_ASSESSMENT_PROXIMO . '" title="' . _MA_ASSESSMENT_PROXIMO . '"></a></td>';
98
            }
99
100
            $ret .= '</tr></table>';
101
        }
102
103
        return $ret;
0 ignored issues
show
Bug introduced by
The variable $ret 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...
104
    }
105
}
106