| Total Complexity | 41 |
| Total Lines | 210 |
| 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 IBaseBehavior[] loaded behaviors. |
||
| 62 | */ |
||
| 63 | private static $_behaviors = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var IBaseBehavior[] behaviors attaching to the TPage |
||
| 67 | */ |
||
| 68 | private $_pageBehaviors = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var array[] delayed behaviors attaching to the loaded behaviors |
||
| 72 | */ |
||
| 73 | private $_behaviorBehaviors = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var array[] additional behaviors in a configuration format: array[], serialized php object, json object, string of xml |
||
| 77 | */ |
||
| 78 | private $_additionalBehaviors; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Constructor. |
||
| 82 | */ |
||
| 83 | public function __construct() |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Initializes the module by loading behaviors. If there are page behaviors, this |
||
| 90 | * attaches behaviors to TPage through TApplication::onBeginRequest and then |
||
| 91 | * TPageService::onPreRunPage. |
||
| 92 | * @param \Prado\Xml\TXmlElement $config configuration for this module, can be null |
||
| 93 | */ |
||
| 94 | public function init($config) |
||
| 95 | { |
||
| 96 | $this->loadBehaviors($config); |
||
| 97 | $this->loadBehaviors(['behaviors' => $this->getAdditionalBehaviors()]); |
||
| 98 | |||
| 99 | if (count($this->_pageBehaviors)) { |
||
| 100 | $this->getApplication()->attachEventHandler('onBeginRequest', [$this, 'attachTPageServiceHandler']); |
||
| 101 | } |
||
| 102 | foreach ($this->_behaviorBehaviors as $target => $behaviors) { |
||
| 103 | if (!isset(self::$_behaviors[$target])) { |
||
| 104 | throw new TConfigurationException('behaviormodule_behaviorowner_required', 'behavior:' . $target); |
||
| 105 | } |
||
| 106 | $owner = self::$_behaviors[$target]; |
||
| 107 | $owner = is_a($owner, '\WeakReference') ? $owner->get() : $owner; |
||
|
|
|||
| 108 | foreach ($behaviors as $properties) { |
||
| 109 | $priority = $properties['priority'] ?? null; |
||
| 110 | unset($properties['priority']); |
||
| 111 | $name = $properties['name']; |
||
| 112 | unset($properties['name']); |
||
| 113 | $owner->attachBehavior($name, $properties, $priority); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | $this->_behaviorBehaviors = []; |
||
| 117 | parent::init($config); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * TApplication::onBeginRequest Handler that adds {@link attachTPageBehaviors} to |
||
| 122 | * TPageService::onPreRunPage. In turn, this attaches {@link attachTPageBehaviors} |
||
| 123 | * to TPageService to then adds the page behaviors. |
||
| 124 | * @param object $sender the object that raised the event |
||
| 125 | * @param mixed $param parameter of the event |
||
| 126 | */ |
||
| 127 | public function attachTPageServiceHandler($sender, $param) |
||
| 128 | { |
||
| 129 | $service = $this->getService(); |
||
| 130 | if ($service instanceof \Prado\Web\Services\TPageService) { |
||
| 131 | $service->attachEventHandler('onPreRunPage', [$this, 'attachTPageBehaviors']); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * This method attaches page behaviors to the TPage handling the TPageService::OnPreInitPage event. |
||
| 137 | * @param object $sender the object that raised the event |
||
| 138 | * @param \Prado\Web\UI\TPage $page the page being initialized |
||
| 139 | */ |
||
| 140 | public function attachTPageBehaviors($sender, $page) |
||
| 141 | { |
||
| 142 | foreach ($this->_pageBehaviors as $name => $properties) { |
||
| 143 | $priority = $properties['priority'] ?? null; |
||
| 144 | unset($properties['priority']); |
||
| 145 | $page->attachBehavior($name, $properties, $priority); |
||
| 146 | } |
||
| 147 | $this->_pageBehaviors = []; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Loads behaviors and attach to the proper object. behaviors for pages are |
||
| 152 | * attached separately if and when the TPage is loaded on TPageSerivce::onPreRunPage |
||
| 153 | * @param mixed $config XML of PHP representation of the behaviors |
||
| 154 | * @throws \Prado\Exceptions\TConfigurationException if the parameter file format is invalid |
||
| 155 | */ |
||
| 156 | protected function loadBehaviors($config) |
||
| 157 | { |
||
| 158 | $isXml = false; |
||
| 159 | if ($config instanceof TXmlElement) { |
||
| 160 | $isXml = true; |
||
| 161 | $config = $config->getElementsByTagName('behavior'); |
||
| 162 | } elseif (is_array($config)) { |
||
| 163 | $config = $config['behaviors']; |
||
| 164 | } elseif (!$config) { |
||
| 165 | return; |
||
| 166 | } |
||
| 167 | foreach ($config as $properties) { |
||
| 168 | $element = $properties; |
||
| 169 | if ($isXml) { |
||
| 170 | $properties = array_change_key_case($properties->getAttributes()->toArray()); |
||
| 171 | } else { |
||
| 172 | if (!is_array($properties)) { |
||
| 173 | throw new TConfigurationException('behaviormodule_behavior_as_array_required'); |
||
| 174 | } |
||
| 175 | if (isset($properties['properties'])) { |
||
| 176 | $properties = $properties['properties']; |
||
| 177 | unset($element['properties']); |
||
| 178 | $properties['class'] = $element['class']; |
||
| 179 | unset($element['class']); |
||
| 180 | } else { |
||
| 181 | unset($element['name']); |
||
| 182 | unset($element['class']); |
||
| 183 | unset($element['attachto']); |
||
| 184 | unset($element['attachtoclass']); |
||
| 185 | unset($element['priority']); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | $properties[IBaseBehavior::CONFIG_KEY] = $element; |
||
| 189 | $name = $properties['name'] ?? null; |
||
| 190 | unset($properties['name']); |
||
| 191 | if (empty($name) || is_numeric($name)) { |
||
| 192 | $name = null; |
||
| 193 | } |
||
| 194 | |||
| 195 | $attachTo = $properties['attachto'] ?? null; |
||
| 196 | $attachToClass = $properties['attachtoclass'] ?? null; |
||
| 197 | unset($properties['attachto']); |
||
| 198 | unset($properties['attachtoclass']); |
||
| 199 | if ($attachTo === null && $attachToClass === null) { |
||
| 200 | throw new TConfigurationException('behaviormodule_attachto_class_required'); |
||
| 201 | } elseif ($attachTo !== null && $attachToClass !== null) { |
||
| 202 | throw new TConfigurationException('behaviormodule_attachto_and_class_only_one'); |
||
| 203 | } |
||
| 204 | if ($attachToClass) { |
||
| 205 | $priority = $properties['priority'] ?? null; |
||
| 206 | unset($properties['priority']); |
||
| 207 | $behavior = TComponent::attachClassBehavior($name, $properties, $attachToClass, $priority); |
||
| 208 | } else { |
||
| 209 | if (strtolower($attachTo) == "page") { |
||
| 210 | $this->_pageBehaviors[$name] = $properties; |
||
| 211 | continue; |
||
| 212 | } elseif (strncasecmp($attachTo, 'module:', 7) === 0) { |
||
| 213 | $owner = $this->getApplication()->getModule(trim(substr($attachTo, 7))); |
||
| 214 | } elseif (strncasecmp($attachTo, 'behavior:', 9) === 0) { |
||
| 215 | $properties['name'] = $name; |
||
| 216 | $this->_behaviorBehaviors[trim(substr($attachTo, 9))][] = $properties; |
||
| 217 | continue; |
||
| 218 | } else { |
||
| 219 | $owner = $this->getSubProperty($attachTo); |
||
| 220 | } |
||
| 221 | $priority = $properties['priority'] ?? null; |
||
| 222 | unset($properties['priority']); |
||
| 223 | if (!$owner) { |
||
| 224 | throw new TConfigurationException('behaviormodule_behaviorowner_required', $attachTo); |
||
| 225 | } |
||
| 226 | $behavior = $owner->attachBehavior($name, $properties, $priority); |
||
| 227 | } |
||
| 228 | if (!is_array($behavior)) { |
||
| 229 | self::$_behaviors[$name] = \WeakReference::create($behavior); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @return array additional behaviors in a list. |
||
| 236 | */ |
||
| 237 | public function getAdditionalBehaviors() |
||
| 238 | { |
||
| 239 | return $this->_additionalBehaviors ?? []; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * this will take a string that is an array of behaviors that has been |
||
| 244 | * through serialize(), or json array of behaviors. If one behavior is |
||
| 245 | * set as an array, then it is automatically placed into an array. |
||
| 246 | * @param array[]|string $behaviors additional behaviors |
||
| 247 | */ |
||
| 248 | public function setAdditionalBehaviors($behaviors) |
||
| 268 | } |
||
| 269 | } |
||
| 270 |