| Total Complexity | 40 |
| 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) |
||
| 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 |