CSession::get()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 4
c 1
b 0
f 1
nc 4
nop 2
dl 0
loc 6
rs 9.4285
1
<?php
2
3
namespace Anax\Session;
4
5
/**
6
 * Anax base class for wrapping sessions.
7
 *
8
 */
9
class CSession
10
{
11
    use \Anax\TConfigure;
12
13
14
15
    /**
16
     * Construct session.
17
     *
18
     * @param array $options to configure options.
19
     */
20
    public function __construct($options = [])
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
21
    {
22
        ;
23
    }
24
25
26
27
    /**
28
     * Set a session name or use one from config.
29
     *
30
     * @param array $aName to set as session name, default is null and then use name from config.
31
     */
32
    public function name($aName = null)
33
    {
34
        $name = isset($aName)
35
            ? $aName
36
            : (isset($this->config['name'])
37
                ? $this->config['name']
38
                : "anax");
39
40
        session_name($name);
41
    }
42
43
44
    /**
45
     * Start session.
46
     *
47
     * @param array $options to configure options.
48
     */
49
    public function start($options = [])
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
    {
51
        session_start();
52
    }
53
54
55
56
    /**
57
     * Get values from session.
58
     *
59
     * @param string $key     in session variable.
60
     * @param mixed  $default default value to return when key is not set in session.
61
     *
62
     * @return mixed
63
     */
64
    public function get($key, $default = null)
0 ignored issues
show
Coding Style introduced by
get 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...
65
    {
66
        return isset($_SESSION) && isset($_SESSION[$key])
67
            ? $_SESSION[$key]
68
            : $default;
69
    }
70
71
72
73
    /**
74
     * Set values in session.
75
     *
76
     * @param string $key   in session variable.
77
     * @param mixed  $value to set in session.
78
     *
79
     * @return void
80
     */
81
    public function set($key, $value)
0 ignored issues
show
Coding Style introduced by
set 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...
82
    {
83
        $_SESSION[$key] = $value;
84
    }
85
86
87
88
    /**
89
     * Check if a value is set in the session.
90
     *
91
     * @param string $key   in session variable.
92
     *
93
     * @return boolean true if $key is set, else false.
94
     */
95
    public function has($key)
0 ignored issues
show
Coding Style introduced by
has 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...
96
    {
97
        return isset($_SESSION[$key]);
98
    }
99
}
100