Completed
Pull Request — master (#4)
by Michael
03:07
created

SmartpartnerSession::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 5
c 1
b 1
f 0
nc 2
nop 0
dl 0
loc 9
rs 9.6666
1
<?php
2
3
/**
4
 * A wrapper around PHP's session functions
5
 * @package smartpartner
6
 * @author  Harry Fuecks (PHP Anthology Volume II)
7
 */
8
class SmartpartnerSession
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...
9
{
10
    /**
11
     * Session constructor<br>
12
     * Starts the session with session_start()
13
     * <b>Note:</b> that if the session has already started,
14
     * session_start() does nothing
15
     * @access public
16
     */
17
    public function __construct()
18
    {
19
        @session_start();
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...
20
    }
21
22
    /**
23
     * Sets a session variable
24
     * @param string name of variable
25
     * @param mixed  value of variable
26
     * @return void
27
     * @access public
28
     */
29
    public function set($name, $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...
30
    {
31
        $_SESSION[$name] = $value;
32
    }
33
34
    /**
35
     * Fetches a session variable
36
     * @param string name of variable
37
     * @return mixed value of session variable
38
     * @access public
39
     */
40
    public function get($name)
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...
41
    {
42
        if (isset($_SESSION[$name])) {
43
            return $_SESSION[$name];
44
        } else {
45
            return false;
46
        }
47
    }
48
49
    /**
50
     * Deletes a session variable
51
     * @param string name of variable
52
     * @return void
53
     * @access public
54
     */
55
    public function del($name)
0 ignored issues
show
Coding Style introduced by
del 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...
56
    {
57
        unset($_SESSION[$name]);
58
    }
59
60
    /**
61
     * Destroys the whole session
62
     * @return void
63
     * @access public
64
     */
65
    public function destroy()
0 ignored issues
show
Coding Style introduced by
destroy 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...
66
    {
67
        $_SESSION = array();
68
        session_destroy();
69
    }
70
71
    /**
72
     * @return SmartpartnerSession
73
     */
74
    public static function getInstance()
75
    {
76
        static $instance;
77
        if (null === $instance) {
78
            $instance = new static();
79
        }
80
81
        return $instance;
82
    }
83
}
84