Completed
Branch master (9dda00)
by Michael
04:54 queued 02:33
created

functions.session.php ➔ newbbGetCookie()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 5
nop 2
dl 0
loc 26
rs 8.439
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 17 and the first side effect is on line 16.

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
 * NewBB 5.0x,  the forum module for XOOPS project
4
 *
5
 * @copyright      XOOPS Project (http://xoops.org)
6
 * @license        GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
7
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>
8
 * @since          4.00
9
 * @package        module::newbb
10
 */
11
12
use Xmf\Request;
13
14
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
15
16
defined('NEWBB_FUNCTIONS_INI') || include_once __DIR__ . '/functions.ini.php';
17
define('NEWBB_FUNCTIONS_SESSION_LOADED', true);
18
xoops_load('XoopsRequest');
19
20
if (!defined('NEWBB_FUNCTIONS_SESSION')) {
21
    define('NEWBB_FUNCTIONS_SESSION', 1);
22
23
    /*
24
     * Currently the newbb session/cookie handlers are limited to:
25
     * -- one dimension
26
     * -- "," and "|" are preserved
27
     *
28
     */
29
    /**
30
     * @param        $name
31
     * @param string $string
32
     */
33
    function newbbSetSession($name, $string = '')
0 ignored issues
show
Coding Style introduced by
newbbSetSession uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
34
    {
35 View Code Duplication
        if (is_array($string)) {
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...
36
            $value = [];
37
            foreach ($string as $key => $val) {
38
                $value[] = $key . '|' . $val;
39
            }
40
            $string = implode(',', $value);
41
        }
42
        $_SESSION['newbb_' . $name] = $string;
43
    }
44
45
    /**
46
     * @param             $name
47
     * @param  bool       $isArray
48
     * @return array|bool
49
     */
50
    function newbbGetSession($name, $isArray = false)
0 ignored issues
show
Coding Style introduced by
newbbGetSession uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
51
    {
52
        $value = !empty($_SESSION['newbb_' . $name]) ? $_SESSION['newbb_' . $name] : false;
53
        if ($isArray) {
54
            $_value = $value ? explode(',', $value) : [];
55
            $value  = [];
56
            if (count($_value) > 0) {
57
                foreach ($_value as $string) {
58
                    $key         = substr($string, 0, strpos($string, '|'));
59
                    $val         = substr($string, strpos($string, '|') + 1);
60
                    $value[$key] = $val;
61
                }
62
            }
63
            unset($_value);
64
        }
65
66
        return $value;
67
    }
68
69
    /**
70
     * @param        $name
71
     * @param string $string
72
     * @param int    $expire
73
     */
74
    function newbbSetCookie($name, $string = '', $expire = 0)
75
    {
76
        global $forumCookie;
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...
77 View Code Duplication
        if (is_array($string)) {
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...
78
            $value = [];
79
            foreach ($string as $key => $val) {
80
                $value[] = $key . '|' . $val;
81
            }
82
            $string = implode(',', $value);
83
        }
84
        setcookie($forumCookie['prefix'] . $name, $string, (int)$expire, $forumCookie['path'], $forumCookie['domain'], $forumCookie['secure']);
85
    }
86
87
    /**
88
     * @param             $name
89
     * @param  bool       $isArray
90
     * @return array|null
91
     */
92
    function newbbGetCookie($name, $isArray = false)
93
    {
94
        global $forumCookie;
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...
95
        //        $value = !empty($_COOKIE[$forumCookie['prefix'] . $name]) ? $_COOKIE[$forumCookie['prefix'] . $name] : null;
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
96
        $value = Request::getString($forumCookie['prefix'] . $name, null, 'COOKIE');
97
98
        if ($isArray) {
99
            $_value = $value ? explode(',', $value) : [];
100
            $value  = [];
101
            if (count($_value) > 0) {
102
                foreach ($_value as $string) {
103
                    $sep = strpos($string, '|');
104
                    if ($sep === false) {
105
                        $value[] = $string;
106
                    } else {
107
                        $key         = substr($string, 0, $sep);
108
                        $val         = substr($string, $sep + 1);
109
                        $value[$key] = $val;
110
                    }
111
                }
112
            }
113
            unset($_value);
114
        }
115
116
        return $value;
117
    }
118
}
119