Passed
Push — master ( 8242d2...71bfa3 )
by Arnold
05:32
created

FlashBag::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
declare(strict_types=1);
4
5
namespace Jasny\Session\Flash;
6
7
/**
8
 * Flash messages are stored in the session and cleared after they're used.
9
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
10
class FlashBag implements \IteratorAggregate
0 ignored issues
show
introduced by
Class Jasny\Session\Flash\FlashBag implements generic interface IteratorAggregate but does not specify its types: TKey, TValue
Loading history...
11
{
12
    protected \ArrayAccess $session;
0 ignored issues
show
introduced by
Property Jasny\Session\Flash\FlashBag::$session with generic interface ArrayAccess does not specify its types: TKey, TValue
Loading history...
13
    protected string $key;
14
15
    protected array $entries = [];
0 ignored issues
show
introduced by
Property Jasny\Session\Flash\FlashBag::$entries type has no value type specified in iterable type array.
Loading history...
16
17
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $key should have a doc-comment as per coding-style.
Loading history...
18
     * Class constructor.
19
     */
20
    public function __construct(string $key = 'flash')
21
    {
22
        $this->key = $key;
23
    }
24
25
26
    /**
27
     * Get iterator for entries.
28
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
29
    public function getIterator(): \Traversable
0 ignored issues
show
introduced by
Method Jasny\Session\Flash\FlashBag::getIterator() return type has no value type specified in iterable type Traversable.
Loading history...
introduced by
Return type (Traversable) of method Jasny\Session\Flash\FlashBag::getIterator() should be covariant with return type (Traversable<mixed, mixed>) of method IteratorAggregate<mixed,mixed>::getIterator()
Loading history...
30
    {
31
        return new \ArrayIterator($this->entries);
32
    }
33
34
    /**
35
     * Get all entries as array.
36
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
37
    public function getArrayCopy(): array
0 ignored issues
show
introduced by
Method Jasny\Session\Flash\FlashBag::getArrayCopy() return type has no value type specified in iterable type array.
Loading history...
38
    {
39
        return $this->entries;
40
    }
41
42
43
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $session should have a doc-comment as per coding-style.
Loading history...
44
     * Get copy with session object.
45
     *
46
     * @return static
47
     */
48
    public function withSession(\ArrayAccess $session): self
0 ignored issues
show
introduced by
Method Jasny\Session\Flash\FlashBag::withSession() has parameter $session with generic interface ArrayAccess but does not specify its types: TKey, TValue
Loading history...
introduced by
Method Jasny\Session\Flash\FlashBag::withSession() return type has no value type specified in iterable type static(Jasny\Session\Flash\FlashBag).
Loading history...
49
    {
50
        if (isset($this->session) && $this->session === $session) {
51
            return $this;
52
        }
53
54
        $copy = clone $this;
55
        $copy->session = $session;
56
57
        $copy->initFromSession();
58
59
        return $copy;
60
    }
61
62
    /**
63
     *  Initialize the entries from the session.
64
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
65
    protected function initFromSession(): void
66
    {
67
        if (!isset($this->session[$this->key])) {
68
            return;
69
        }
70
71
        $entries = $this->session[$this->key];
72
        unset($this->session[$this->key]);
73
74
        if (!is_array($entries)) {
75
            trigger_error('Invalid flash session data', E_USER_WARNING);
76
            return;
77
        }
78
79
        $this->initEntries($entries);
80
    }
81
82
    /**
83
     * Initialize the entries.
84
     *
85
     * @param array $entries
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
86
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
87
    protected function initEntries(array $entries): void
0 ignored issues
show
introduced by
Method Jasny\Session\Flash\FlashBag::initEntries() has parameter $entries with no value type specified in iterable type array.
Loading history...
88
    {
89
        $invalid = 0;
90
91
        foreach ($entries as $entry) {
92
            if (!is_array($entry) || !isset($entry['message'])) {
93
                $invalid++;
94
                continue;
95
            }
96
97
            $this->entries[] = $entry + ['type' => '', 'contentType' => 'text/plain'];
98
        }
99
100
        if ($invalid > 0) {
101
            trigger_error(
102
                $invalid === 1 ? "Ignored invalid flash message" : "Ignoring $invalid invalid flash messages",
103
                E_USER_NOTICE,
104
            );
105
        }
106
    }
107
108
109
    /**
110
     * Add a flash message.
111
     *
112
     * @param string $type         flash type, eg. 'error', 'notice' or 'success'
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter name; 9 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
113
     * @param string $message      flash message
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 6 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
114
     * @param string $contentType  mime, eg 'text/plain' or 'text/html'
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
115
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
116
     */
117
    public function add(string $type, string $message, string $contentType = 'text/plain'): self
0 ignored issues
show
introduced by
Method Jasny\Session\Flash\FlashBag::add() return type has no value type specified in iterable type $this(Jasny\Session\Flash\FlashBag).
Loading history...
118
    {
119
        $this->session[$this->key][] = ['type' => $type, 'message' => $message, 'contentType' => $contentType];
120
121
        return $this;
122
    }
123
124
    /**
125
     * Reissue the flash messages.
126
     * Get it now and it will remain in the session for next request.
127
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
128
    public function reissue(): self
0 ignored issues
show
introduced by
Method Jasny\Session\Flash\FlashBag::reissue() return type has no value type specified in iterable type Jasny\Session\Flash\FlashBag.
Loading history...
129
    {
130
        $this->session[$this->key] = array_merge(
131
            $this->entries,
132
            $this->session[$this->key] ?? [],
133
        );
134
135
        $this->entries = [];
136
137
        return $this;
138
    }
139
140
    /**
141
     * Clear all flash messages.
142
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
143
    public function clear(): self
0 ignored issues
show
introduced by
Method Jasny\Session\Flash\FlashBag::clear() return type has no value type specified in iterable type Jasny\Session\Flash\FlashBag.
Loading history...
144
    {
145
        $this->entries = [];
146
147
        if (isset($this->session[$this->key])) {
148
            unset($this->session[$this->key]);
149
        }
150
151
        return $this;
152
    }
153
}
154