Completed
Pull Request — master (#5)
by Brian
19:55 queued 04:56
created

Flash::flashMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Flash;
4
5
use Illuminate\Support\Traits\Macroable;
6
use Illuminate\Contracts\Session\Session;
7
8
/** @mixin \Spatie\Flash\Message */
9
class Flash
10
{
11
    use Macroable;
12
13
    /** @var \Illuminate\Contracts\Session\Session */
14
    protected $session;
15
16
    public function __construct(Session $session)
17
    {
18
        $this->session = $session;
19
    }
20
21
    public function __get(string $name)
22
    {
23
        return $this->getMessage()->$name ?? null;
24
    }
25
26
    public function getMessage(): ?Message
27
    {
28
        $flashedMessageProperties = $this->session->get('laravel_flash_message');
29
30
        if (! $flashedMessageProperties) {
31
            return null;
32
        }
33
34
        return new Message($flashedMessageProperties['message'], $flashedMessageProperties['class']);
35
    }
36
37
    public function flash(Message $message): void
38
    {
39
        if ($message->class && static::hasMacro($message->class)) {
40
            $methodName = $message->class;
41
42
            $this->$methodName($message->message);
43
44
            return;
45
        }
46
47
        $this->flashMessage($message->toArray());
0 ignored issues
show
Documentation introduced by
$message->toArray() is of type array, but the function expects a object<Spatie\Flash\Message>.

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...
48
    }
49
50
    public function flashMessage(Message $message): void
51
    {
52
        $this->session->flash('laravel_flash_message', $message->toArray());
53
    }
54
55
    public static function levels(array $methodClasses): void
56
    {
57
        foreach ($methodClasses as $method => $classes) {
58
            self::macro($method, function (string $message) use ($classes) {
59
                return $this->flashMessage(new Message($message, $classes));
60
            });
61
        }
62
    }
63
}
64