Completed
Push — master ( 8d2cc2...d7f68f )
by Freek
10:09 queued 08:19
created

HandlesEvents::checkNonAssociativeEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Spatie\EventProjector\EventHandlers;
4
5
use Exception;
6
7
trait HandlesEvents
8
{
9
    public function handlesEvents(): array
10
    {
11
        return collect($this->handlesEvents ?? [])
0 ignored issues
show
Bug introduced by
The property handlesEvents does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
12
            ->mapWithKeys(function ($methodName, $eventClass) {
13
                if (is_numeric($eventClass)) {
14
                    $eventClass = $methodName;
15
                    $methodName = 'on' . ucfirst(class_basename($eventClass));
16
                }
17
18
                return [$eventClass => $methodName];
19
            })
20
            ->toArray();
21
    }
22
23
    public function handlesEventClassNames(): array
24
    {
25
        return array_keys($this->handlesEvents());
26
    }
27
28
    public function methodNameThatHandlesEvent(object $event): string
29
    {
30
        $handlesEvents = $this->handlesEvents();
31
32
        $eventClass = get_class($event);
33
34
        return $handlesEvents[$eventClass] ?? '';
35
    }
36
37
    public function handleException(Exception $exception)
38
    {
39
        report($exception);
40
    }
41
}
42