Failed Conditions
Pull Request — master (#7)
by Sergey
02:55
created

Flash::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Jasny;
4
5
/**
6
 * Class for the flash message
7
 */
8
class Flash
9
{
10
    /**
11
     * @var object
12
     */
13
    protected static $data = null;    
14
    
15
    /**
16
     * Check if the flash is set.
17
     * 
18
     * @return boolean
19
     */
20 2
    public static function isIssued()
0 ignored issues
show
Coding Style introduced by
isIssued 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...
21
    {
22 2
        return isset($_SESSION['flash']) || isset(static::$data);
23
    }
24
    
25
    /**
26
     * Set the flash.
27
     * 
28
     * @param mixed $type     flash type, eg. 'error', 'notice' or 'success'
29
     * @param mixed $message  flash message
30
     */
31 4
    public static function set($type, $message)
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...
32
    {
33 4
        if (!$type) {
34 1
            throw new \InvalidArgumentException("Type should not be empty");            
35
        }
36
37 3
        static::$data = (object)['type'=>$type, 'message'=>$message];
38
39 3
        $_SESSION['flash'] = static::$data;
40 3
    }
41
    
42
    /**
43
     * Get the flash.
44
     * 
45
     * @return object
46
     */
47 3 View Code Duplication
    public static function get()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
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...
48
    {
49 3
        if (!isset(static::$data) && isset($_SESSION['flash'])) {
50 2
            static::$data = (object)$_SESSION['flash'];
51 2
            unset($_SESSION['flash']);
52 2
        }
53
        
54 3
        return static::$data;
55
    }
56
    
57
    /**
58
     * Reissue the flash.
59
     */
60 2 View Code Duplication
    public static function reissue()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
Coding Style introduced by
reissue 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...
61
    {
62 2
        if (!isset(static::$data) && isset($_SESSION['flash'])) {
63 2
            static::$data = (object)$_SESSION['flash'];
64 2
        } else {
65 2
            $_SESSION['flash'] = static::$data;
66
        }
67 2
    }
68
    
69
    /**
70
     * Clear the flash.
71
     */
72 3
    public static function clear()
0 ignored issues
show
Coding Style introduced by
clear 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...
73
    {
74 3
        self::$data = null;
75 3
        unset($_SESSION['flash']);
76 3
    }     
77
    
78
    /**
79
     * Get the flash type
80
     * 
81
     * @return string
82
     */
83 2
    public static function getType()
84
    {
85 2
        $data = static::get();
86 2
        return isset($data) ? $data->type : null;
87
    }
88
    
89
    /**
90
     * Get the flash message
91
     * 
92
     * @return string
93
     */
94 2
    public static function getMessage()
95
    {
96 2
        $data = static::get();
97 2
        return isset($data) ? $data->message : null;
98
    }
99
    
100
    /**
101
     * Cast object to string
102
     * 
103
     * @return string
104
     */
105 2
    public function __toString()
106
    {
107 2
        return (string)$this->getMessage();
108
    }
109
}
110