Passed
Push — master ( 032384...6ce998 )
by Fabio
05:25
created

TGlobalClassAware   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A detachEventHandlers() 0 7 2
A attachEventHandlers() 0 7 2
1
<?php
2
3
/**
4
 * TGlobalClassAware class file.
5
 *
6
 * @author Brad Anderson <[email protected]>
7
 * @link https://github.com/pradosoft/prado
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 */
10
11
namespace Prado\Util\Behaviors;
12
13
use Prado\TComponent;
14
15
/**
16
 * TGlobalClassAware class.
17
 *
18
 * This behavior registers the handlers `fxAttachClassBehavior` and `fxDetachClassBehavior`
19
 * of the owner to listen for dynamic changes to its class behaviors (after it is
20
 * instanced).
21
 *
22
 * This should only be used when the TComponent is not {@see \Prado\TComponent::listen()}ing
23
 * or the handlers will be double added.  Listening is turned on automatically with
24
 * {@see \Prado\TComponent::getAutoGlobalListen()} (returning true) for select classes.
25
 *
26
 * Without this behavior (or listen), an instanced TComponent will not update its
27
 * class behaviors when there is a change in the global class behaviors.
28
 *
29
 * @author Brad Anderson <[email protected]>
30
 * @since 4.2.3
31
 */
32
class TGlobalClassAware extends \Prado\Util\TBehavior
33
{
34
	/**
35
	 * Attaches the component event handlers:
36
	 * {@see \Prado\TComponent::fxAttachClassBehavior()} and {@see \Prado\TComponent::fxDetachClassBehavior()}
37
	 * @param TComponent $component
38
	 * @return bool Attaching handlers.
39
	 */
40
	protected function attachEventHandlers(TComponent $component): bool
41
	{
42
		if ($return = parent::attachEventHandlers($component)) {
43
			$component->attachEventHandler('fxAttachClassBehavior', [$component, 'fxAttachClassBehavior'], $this->getPriority());
0 ignored issues
show
Bug introduced by
It seems like $this->getPriority() can also be of type double; however, parameter $priority of Prado\TComponent::attachEventHandler() does only seem to accept Prado\numeric|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
			$component->attachEventHandler('fxAttachClassBehavior', [$component, 'fxAttachClassBehavior'], /** @scrutinizer ignore-type */ $this->getPriority());
Loading history...
44
			$component->attachEventHandler('fxDetachClassBehavior', [$component, 'fxDetachClassBehavior'], $this->getPriority());
45
		}
46
		return $return;
47
	}
48
49
	/**
50
	 * Detaches the component event handlers:
51
	 * {@see \Prado\TComponent::fxAttachClassBehavior()} and {@see \Prado\TComponent::fxDetachClassBehavior()}
52
	 * @param TComponent $component
53
	 * @return bool Detaching handlers.
54
	 */
55
	protected function detachEventHandlers(TComponent $component): bool
56
	{
57
		if ($return = parent::detachEventHandlers($component)) {
58
			$component->detachEventHandler('fxAttachClassBehavior', [$component, 'fxAttachClassBehavior']);
59
			$component->detachEventHandler('fxDetachClassBehavior', [$component, 'fxDetachClassBehavior']);
60
		}
61
		return $return;
62
	}
63
}
64