Completed
Push — master ( aec546...27a395 )
by Filipe
04:35
created

FlashMessages::set()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
ccs 0
cts 12
cp 0
rs 9.4285
cc 3
eloc 8
nc 2
nop 2
crap 12
1
<?php
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc\Http;
11
12
use Slick\Filter\StaticFilter;
13
14
/**
15
 * Session Flash Messages
16
 * 
17
 * @package Slick\Mvc\Http
18
 * @author  Filipe Silva <[email protected]>
19
 */
20
class FlashMessages
21
{
22
23
    /**#@+
24
     * @const string TYPE for message type constants
25
     */
26
    const TYPE_SUCCESS = 0;
27
    const TYPE_ERROR   = 1;
28
    const TYPE_WARNING = 2;
29
    const TYPE_INFO    = 3;
30
    /**#@-*/
31
32
    /**
33
     * Uses session
34
     */
35
    use SessionAwareMethods;
36
37
    /**
38
     * @var array message type descriptions
39
     */
40
    public $classes = [
41
        self::TYPE_SUCCESS => 'success',
42
        self::TYPE_WARNING => 'warning',
43
        self::TYPE_INFO    => 'info',
44
        self::TYPE_ERROR   => 'danger'
45
    ];
46
47
    /**
48
     * @var array
49
     */
50
    protected static $messages = [];
51
52
    /**
53
     * Set a flash message of a give type
54
     *
55
     * @param int $type
56
     * @param string $message
57
     *
58
     * @return self
59
     */
60
    public function set($type, $message)
61
    {
62
        $type = StaticFilter::filter('number', $type);
63
        if ($type < 0 || $type > 3) {
64
            $type = static::TYPE_INFO;
65
        }
66
        self::$messages[$type][] = $message;
67
        $this->getSessionDriver()
68
            ->set('_messages_', self::$messages)
69
        ;
70
        return $this;
71
    }
72
73
    /**
74
     * Retrieve all messages and flushes them all
75
     *
76
     * @return array
77
     */
78
    public function get()
79
    {
80
        self::$messages = $this->getSessionDriver()->get('_messages_', []);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string|null.

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...
Documentation Bug introduced by
It seems like $this->getSessionDriver(...('_messages_', array()) of type * is incompatible with the declared type array of property $messages.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
81
        $messages = self::$messages;
82
        $this->flush();
83
        return $messages;
84
    }
85
86
    /**
87
     * clears all messages
88
     *
89
     * @return FlashMessages
90
     */
91
    public function flush()
92
    {
93
        self::$messages = [];
94
        $this->getSessionDriver()
95
            ->set('_messages_', self::$messages)
96
        ;
97
        return $this;
98
    }
99
}