Completed
Pull Request — master (#8)
by Michael
02:48
created

MylinksUtility::show_message()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 6
nop 3
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
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 24 and the first side effect is on line 13.

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
3
/**
4
 *  mylinks Utility Class Elements
5
 *
6
 * @copyright ::  ZySpec Incorporated
7
 * @license   ::    {@link http://www.gnu.org/licenses/gpl-2.0.html GNU Public License}
8
 * @package   ::    mylinks
9
 * @subpackage:: class
10
 * @author    ::     zyspec ([email protected])
11
 */
12
13
defined('XOOPS_ROOT_PATH') or die('Restricted access');
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
14
15
/**
16
 * MylinksUtility
17
 *
18
 * @package   ::   mylinks
19
 * @author    ::    zyspec ([email protected]), Herve Thouzard
20
 * @copyright ::  {@link http://xoops.org/ XOOPS Project}
21
 * @copyright :: Copyright (c) 2010 ZySpec Incorporated, Herve Thouzard
22
 * @access::    public
23
 */
24
class MylinksUtility
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...
25
{
26
    /**
27
     * Sanitize input variables
28
     * @param  string             $global  the input array ($_REQUEST, $_GET, $_POST)
29
     * @param  unknown_type       $key     the array key for variable to clean
30
     * @param string|unknown_type $default the default value to use if filter fails
31
     * @param  string             $type    the variable type (string, email, url, int)
32
     * @param  array              $limit   'min' 'max' keys - the lower/upper limit for integer values
0 ignored issues
show
Documentation introduced by
Should the type for parameter $limit not be array|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...
33
     * @return Ambigous|number <boolean, unknown>
34
     */
35
    public static function mylinks_cleanVars(&$global, $key, $default = '', $type = 'int', $limit = null)
36
    {
37
        switch ($type) {
38
            case 'string':
39
                $ret = isset($global[$key]) ? filter_var($global[$key], FILTER_SANITIZE_MAGIC_QUOTES) : $default;
40
                break;
41
            case 'email':
42
                $ret = isset($global[$key]) ? filter_var($global[$key], FILTER_SANITIZE_EMAIL) : $default;
43
                break;
44
            case 'url':
45
                $ret = isset($global[$key]) ? filter_var($global[$key], FILTER_SANITIZE_URL) : $default;
46
                break;
47
            case 'int':
48
            default:
49
                $default = (int)$default;
50
                $ret     = isset($global[$key]) ? filter_var($global[$key], FILTER_SANITIZE_NUMBER_INT) : false;
51
                if (isset($limit) && is_array($limit) && (false !== $ret)) {
52 View Code Duplication
                    if (array_key_exists('min', $limit)) {
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...
53
                        $ret = ($ret >= $limit['min']) ? $ret : false;
54
                    }
55 View Code Duplication
                    if (array_key_exists('max', $limit)) {
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...
56
                        $ret = ($ret <= $limit['max']) ? $ret : false;
57
                    }
58
                }
59
                break;
60
        }
61
        $ret = ($ret === false) ? $default : $ret;
62
63
        return $ret;
64
    }
65
66
    /**
67
     *
68
     * Temporary patch for errorHandler processing
69
     * @deprecated
70
     * @param  string $msg   message to display
71
     * @param  int    $pages number of pages to jump back for link
72
     * @param  string $type  error||info to add errorMsg CSS to display
73
     * @return null
74
     */
75
    public static function show_message($msg, $pages = 1, $type = 'error')
76
    {
77
        switch (mb_strtolower($type)) {
78
            case 'error':
79
                $div_class = "class='errorMsg'";
80
                break;
81
            case 'info':
82
                $div_class = '';
83
                break;
84
        }
85
        include_once XOOPS_ROOT_PATH . '/header.php';
86
        echo "<div{$div_class}><strong>{$xoopsConfig['sitename']} Error</strong><br><br>\n" . "Error Code: {$e_code}<br><br><br>\n" . "<strong>ERROR:</strong> {$msg}<br>\n";
0 ignored issues
show
Bug introduced by
The variable $div_class 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...
Bug introduced by
The variable $xoopsConfig 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...
Bug introduced by
The variable $e_code 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...
87
        $pages = (int)$pages;
88
        if (0 != $pages) {
89
            $pages = '-' . abs($pages);
90
            echo "<br><br>\n" . "[ <a href=\'javascript:history.go(-{$pages})\'>" . _BACK . '</a> ]</div>';
91
        }
92
        include_once XOOPS_ROOT_PATH . '/footer.php';
93
94
        return;
95
    }
96
}
97