Completed
Push — master ( 743f45...5838ba )
by Freek
01:51
created

HandlesEvents   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 34
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handlesEvents() 0 4 1
A handlesEvent() 0 4 1
A methodNameThatHandlesEvent() 0 16 3
A handleException() 0 4 1
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 $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
    }
13
14
    public function handlesEvent(object $event): bool
15
    {
16
        return array_key_exists(get_class($event), $this->handlesEvents());
17
    }
18
19
    public function methodNameThatHandlesEvent(object $event): string
20
    {
21
        $methodName = $this->handlesEvents()[get_class($event)] ?? '';
22
23
        if ($methodName !== '') {
24
            return $methodName;
25
        }
26
27
        $wildcardMethod = $this->handlesEvents()['*'] ?? '';
28
29
        if ($wildcardMethod !== '') {
30
            return $wildcardMethod;
31
        }
32
33
        return '';
34
    }
35
36
    public function handleException(Exception $exception)
37
    {
38
        report($exception);
39
    }
40
}
41