Total Complexity | 41 |
Total Lines | 205 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like TBehaviorsModule often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TBehaviorsModule, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
58 | class TBehaviorsModule extends \Prado\TModule |
||
59 | { |
||
60 | /** |
||
61 | * @var bool wether or not WeakReference is available |
||
62 | */ |
||
63 | private static $_weak; |
||
64 | |||
65 | /** |
||
66 | * @var IBaseBehavior[] loaded behaviors. |
||
67 | */ |
||
68 | private static $_behaviors = []; |
||
69 | |||
70 | /** |
||
71 | * @var IBaseBehavior[] behaviors attaching to the TPage |
||
72 | */ |
||
73 | private $_pageBehaviors = []; |
||
74 | |||
75 | /** |
||
76 | * @var array[] delayed behaviors attaching to the loaded behaviors |
||
77 | */ |
||
78 | private $_behaviorBehaviors = []; |
||
79 | |||
80 | /** |
||
81 | * @var array[] additional behaviors in a configuration format: array[], serialized php object, json object, string of xml |
||
82 | */ |
||
83 | private $_additionalBehaviors; |
||
84 | |||
85 | /** |
||
86 | * Constructor. |
||
87 | * Discovers the availability of the {@link WeakReference} object in PHP 7.4.0+. |
||
88 | */ |
||
89 | public function __construct() |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Initializes the module by loading behaviors. If there are page behaviors, this |
||
99 | * attaches behaviors to TPage through TApplication::onBeginRequest and then |
||
100 | * TPageService::onPreRunPage. |
||
101 | * @param \Prado\Xml\TXmlElement $config configuration for this module, can be null |
||
102 | */ |
||
103 | public function init($config) |
||
104 | { |
||
105 | $this->loadBehaviors($config); |
||
106 | $this->loadBehaviors(['behaviors' => $this->getAdditionalBehaviors()]); |
||
107 | |||
108 | if (count($this->_pageBehaviors)) { |
||
109 | $this->getApplication()->attachEventHandler('onBeginRequest', [$this, 'attachTPageServiceHandler']); |
||
110 | } |
||
111 | foreach ($this->_behaviorBehaviors as $target => $behaviors) { |
||
112 | if (!isset(self::$_behaviors[$target])) { |
||
113 | throw new TConfigurationException('behaviormodule_behaviorowner_required', 'behavior:' . $target); |
||
114 | } |
||
115 | $owner = self::$_behaviors[$target]; |
||
116 | $owner = is_a($owner, '\WeakReference') ? $owner->get() : $owner; |
||
117 | foreach ($behaviors as $properties) { |
||
118 | $priority = $properties['priority'] ?? null; |
||
119 | unset($properties['priority']); |
||
120 | $name = $properties['name']; |
||
121 | unset($properties['name']); |
||
122 | $owner->attachBehavior($name, $properties, $priority); |
||
123 | } |
||
124 | } |
||
125 | $this->_behaviorBehaviors = []; |
||
126 | parent::init($config); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * TApplication::onBeginRequest Handler that adds {@link attachTPageBehaviors} to |
||
131 | * TPageService::onPreRunPage. In turn, this attaches {@link attachTPageBehaviors} |
||
132 | * to TPageService to then adds the page behaviors. |
||
133 | * @param object $sender the object that raised the event |
||
134 | * @param mixed $param parameter of the event |
||
135 | */ |
||
136 | public function attachTPageServiceHandler($sender, $param) |
||
137 | { |
||
138 | $service = $this->getService(); |
||
139 | if ($service instanceof \Prado\Web\Services\TPageService) { |
||
140 | $service->attachEventHandler('onPreRunPage', [$this, 'attachTPageBehaviors']); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * This method attaches page behaviors to the TPage handling the TPageService::OnPreInitPage event. |
||
146 | * @param object $sender the object that raised the event |
||
147 | * @param \Prado\Web\UI\TPage $page the page being initialized |
||
148 | */ |
||
149 | public function attachTPageBehaviors($sender, $page) |
||
150 | { |
||
151 | foreach ($this->_pageBehaviors as $name => $properties) { |
||
152 | $priority = $properties['priority'] ?? null; |
||
153 | unset($properties['priority']); |
||
154 | $page->attachBehavior($name, $properties, $priority); |
||
155 | } |
||
156 | $this->_pageBehaviors = []; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Loads behaviors and attach to the proper object. behaviors for pages are |
||
161 | * attached separately if and when the TPage is loaded on TPageSerivce::onPreRunPage |
||
162 | * @param mixed $config XML of PHP representation of the behaviors |
||
163 | * @throws \Prado\Exceptions\TConfigurationException if the parameter file format is invalid |
||
164 | */ |
||
165 | protected function loadBehaviors($config) |
||
225 | } |
||
226 | } |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * @return array additional behaviors in a list. |
||
231 | */ |
||
232 | public function getAdditionalBehaviors() |
||
233 | { |
||
234 | return $this->_additionalBehaviors ?? []; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * this will take a string that is an array of behaviors that has been |
||
239 | * through serialize(), or json array of behaviors. If one behavior is |
||
240 | * set as an array, then it is automatically placed into an array. |
||
241 | * @param array[]|string $behaviors additional behaviors |
||
242 | */ |
||
243 | public function setAdditionalBehaviors($behaviors) |
||
263 | } |
||
264 | } |
||
265 |