Flash::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
/**
4
 * Class Flash
5
 * @method static void info(string $message, bool $closable = null, bool $fadeOut = null)
6
 * @method static void success(string $message, bool $closable = null, bool $fadeOut = null)
7
 * @method static void warning(string $message, bool $closable = null, bool $fadeOut = null)
8
 * @method static void danger(string $message, bool $closable = null, bool $fadeOut = null)
9
 * @method static void alert(string $message, bool $closable = null, bool $fadeOut = null)
10
 * @method static void modal(string $message, bool $closable = null, bool $fadeOut = null)
11
 */
12
class Flash extends ViewableData implements TemplateGlobalProvider
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...
13
{
14
    /**
15
     * @config
16
     * @var array
17
     */
18
    private static $defaults = [
0 ignored issues
show
Unused Code introduced by
The property $defaults is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
19
        'Type'     => 'success',
20
        'IsModal'  => false,
21
        'Closable' => true,
22
        'FadeOut'  => false
23
    ];
24
25
    /**
26
     * @config
27
     * @var array
28
     */
29
    private static $supported_methods = [
0 ignored issues
show
Unused Code introduced by
The property $supported_methods is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
30
        'info',
31
        'success',
32
        'warning',
33
        'danger',
34
        'alert',
35
        'modal'
36
    ];
37
38
    /**
39
     * @config
40
     * @var string
41
     */
42
    private static $template = 'FlashMessage';
0 ignored issues
show
Unused Code introduced by
The property $template is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
43
44
    /**
45
     * @config
46
     * @var string
47
     */
48
    private static $session_name = 'FlashMessage';
0 ignored issues
show
Unused Code introduced by
The property $session_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
49
50
    /**
51
     * @config
52
     * @var bool
53
     */
54
    private static $load_javascript = true;
0 ignored issues
show
Unused Code introduced by
The property $load_javascript is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
55
56
    /**
57
     * The Flash Message data
58
     *
59
     * @var array
60
     */
61
    protected $data = [];
62
63
    /**
64
     * @param array $data
65
     */
66
    public function __construct($data)
67
    {
68
        $this->data = (array)$data + (array)self::config()->defaults;
69
70
        parent::__construct();
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    public static function get_template_global_variables()
77
    {
78
        return [
79
            'FlashMessage'
80
        ];
81
    }
82
83
    /**
84
     * @return Flash
85
     */
86
    public static function FlashMessage()
87
    {
88
        return Flash::get();
89
    }
90
91
    /**
92
     * @param $message
93
     * @param string $type
94
     * @param null $closable
95
     * @param null $fadeOut
96
     */
97
    public static function set($message, $type = 'success', $closable = null, $fadeOut = null)
98
    {
99
        $data = [
100
            'Message' => $message,
101
            'Type'    => $type
102
        ];
103
104
        if (null !== $closable) {
105
            $data['Closable'] = $closable;
106
        }
107
108
        if (null !== $fadeOut) {
109
            $data['FadeOut'] = $fadeOut;
110
        }
111
112
        if('modal' === $type) {
113
            $data['IsModal'] = true;
114
        }
115
116
        Session::set(Flash::config()->session_name, $data);
0 ignored issues
show
Documentation introduced by
$data is of type array<string,?>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
117
    }
118
119
    /**
120
     * @return Flash
121
     */
122
    public static function get()
123
    {
124
        $key  = Flash::config()->session_name;
125
        $data = Session::get($key);
126
        Session::clear($key);
127
128
        return (new Flash($data));
129
    }
130
131
    /**
132
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be HTMLText?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
133
     */
134
    public function forTemplate()
135
    {
136
        if (self::config()->load_javascript) {
137
            Requirements::javascript('flashmessage/javascript/flashmessage.js');
138
        }
139
        return $this->customise($this->data)->renderWith(self::config()->template);
140
    }
141
142
    /**
143
     * @param $method
144
     * @param $args
145
     * @throws BadMethodCallException
146
     */
147
    public static function __callStatic($method, $args)
148
    {
149
        if (in_array($method, self::config()->supported_methods)) {
150
            self::set($args[0], $method, isset($args[1]) ? $args[1] : null, isset($args[2]) ? $args[2] : null);
151
        } else {
152
            throw new BadMethodCallException("Method '$method' does not exist on " . __CLASS__);
153
        }
154
    }
155
156
}
157