Completed
Push — master ( 508240...149fba )
by Vasily
03:46
created

DeferredEventHandlers::__call()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
rs 9.4286
c 1
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
1
<?php
2
namespace PHPDaemon\Traits;
3
4
use PHPDaemon\Core\DeferredEvent;
5
use PHPDaemon\Core\Daemon;
6
use PHPDaemon\Core\Debug;
7
use PHPDaemon\Exceptions\UndefinedMethodCalled;
8
use PHPDaemon\Exceptions\UndefinedEventCalledException;
9
10
/**
11
 * Deferred event handlers trait
12
 * @package PHPDaemon\Traits
13
 * @author  Zorin Vasily <[email protected]>
14
 */
15
trait DeferredEventHandlers {
16
	protected $DefEvHandlersUsed = false;
17
18
	/**
19
	 * @param  string $event
20
	 * @throws UndefinedEventCalledException
21
	 * @return mixed
22
	 */
23
	public function __get($event) {
24
		if (!$this->DefEvHandlersUsed) {
25
			$this->DefEvHandlersUsed = true;
26
			$this->firstDeferredEventUsed();
27
		}
28
		if (substr($event, 0, 2) !== 'on') {
29
			return $this->{$event};
30
		}
31
		if (!method_exists($this, $event . 'Event')) {
32
			throw new \PHPDaemon\Exceptions\UndefinedEventCalled('Undefined event called: ' . get_class($this). '->' . $event);
33
		}
34
		$e = new DeferredEvent($this->{$event . 'Event'}());
35
		$e->name = $event;
36
		$e->parent = $this;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this of type this<PHPDaemon\Traits\DeferredEventHandlers> is incompatible with the declared type object of property $parent.

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...
37
		$this->{$event} = &$e;
38
		return $e;
39
	}
40
41
	/**
42
	 * Called when first deferred event is used
43
	 * @return void
44
	 */
45
	protected function firstDeferredEventUsed() {}
46
47
	/**
48
	 * Cleans up events
49
	 * @return void
50
	 */
51
	public function cleanupDeferredEventHandlers() {
52
		foreach ($this as $key => $property) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<PHPDaemon\Traits\DeferredEventHandlers> is not traversable.
Loading history...
53
			if ($property instanceof DeferredEvent) {
54
				$property->cleanup();
55
				$this->{$key} = null;
56
			}
57
		}
58
	}
59
60
	/**
61
	 * @param  string $method Method name
62
	 * @param  array  $args   Arguments
63
	 * @throws UndefinedMethodCalled
64
	 * @return mixed
65
	 */
66
	public function __call($method, $args) {
67
		if (substr($method, 0, 2) !== 'on') {
68
			throw new UndefinedMethodCalled('Call to undefined method ' . get_class($this) . '->' . $method);
69
		}
70
		$o = $this->{$method};
71
		if (!$o) {
72
			throw new UndefinedMethodCalled('Call to undefined method ' . get_class($this) . '->' . $method);
73
		}
74
		return call_user_func_array($o, $args);
75
	}
76
}
77