Complex classes like Action 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Action, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Action extends \yii\base\Action |
||
| 28 | { |
||
| 29 | const EVENT_BEFORE_RUN = 'beforeRun'; |
||
| 30 | const EVENT_BEFORE_SAVE = 'beforeSave'; |
||
| 31 | const EVENT_BEFORE_LOAD = 'beforeLoad'; |
||
| 32 | const EVENT_BEFORE_PERFORM = 'beforePerform'; |
||
| 33 | const EVENT_AFTER_PERFORM = 'afterPerform'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var Controller the controller that owns this action |
||
| 37 | */ |
||
| 38 | public $controller; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var Action|SwitchAction parent called action |
||
| 42 | */ |
||
| 43 | public $parent; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string scenario to be used when save |
||
| 47 | */ |
||
| 48 | public $_scenario; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Perform query options. |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | public $queryOptions = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array|Closure additional data passed when rendering |
||
| 58 | */ |
||
| 59 | public $data = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param string $scenario |
||
| 63 | */ |
||
| 64 | public function setScenario($scenario) |
||
| 65 | { |
||
| 66 | $this->_scenario = $scenario; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return string |
||
| 71 | */ |
||
| 72 | public function getScenario() |
||
| 73 | { |
||
| 74 | return $this->_scenario ?: ($this->parent ? $this->parent->getScenario() : $this->id); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var Collection|array the options that will be used to create the collection. |
||
| 79 | * Stores collection after creating |
||
| 80 | */ |
||
| 81 | protected $_collection; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Setter for the collection. |
||
| 85 | * |
||
| 86 | * @param array $collection config for the collection |
||
| 87 | */ |
||
| 88 | public function setCollection($collection) |
||
| 89 | { |
||
| 90 | $this->_collection = $collection; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Gets the instance of the collection. |
||
| 95 | * |
||
| 96 | * @return Collection |
||
| 97 | */ |
||
| 98 | public function getCollection() |
||
| 99 | { |
||
| 100 | if ($this->parent) { |
||
| 101 | return $this->parent->getCollection(); |
||
| 102 | } |
||
| 103 | |||
| 104 | if (!is_object($this->_collection)) { |
||
| 105 | $action = $this->controller->action; |
||
| 106 | if ($action instanceof self) { |
||
| 107 | $scenario = $action->getScenario(); |
||
| 108 | } else { |
||
| 109 | $scenario = $action->id; |
||
| 110 | } |
||
| 111 | |||
| 112 | $this->_collection = Yii::createObject(ArrayHelper::merge([ |
||
| 113 | 'class' => Collection::class, |
||
| 114 | 'model' => $this->controller->newModel(['scenario' => $scenario]), |
||
| 115 | 'scenario' => $scenario, |
||
| 116 | 'queryOptions' => $this->queryOptions, |
||
| 117 | ], (array) $this->_collection)); |
||
| 118 | } |
||
| 119 | |||
| 120 | return $this->_collection; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var callable the custom callback to load data into the collection. Gets [[$this]] as the only argument |
||
| 125 | * Should call `$this->collection->load()` |
||
| 126 | */ |
||
| 127 | public $collectionLoader; |
||
| 128 | |||
| 129 | protected function beforeRun() |
||
| 130 | { |
||
| 131 | $this->thoroughTrigger(static::EVENT_BEFORE_RUN); |
||
| 132 | |||
| 133 | return parent::beforeRun(); |
||
| 134 | } |
||
| 135 | |||
| 136 | public function beforeLoad() |
||
| 137 | { |
||
| 138 | $this->thoroughTrigger(static::EVENT_BEFORE_LOAD); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Loads data to the [[collection]]. |
||
| 143 | * |
||
| 144 | * @param array $data |
||
| 145 | */ |
||
| 146 | public function loadCollection($data = null) |
||
| 147 | { |
||
| 148 | $this->beforeLoad(); |
||
| 149 | |||
| 150 | if ($this->collectionLoader instanceof Closure) { |
||
| 151 | call_user_func($this->collectionLoader, $this, $data); |
||
| 152 | } else { |
||
| 153 | $this->collection->load($data); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | public function beforeSave() |
||
| 158 | { |
||
| 159 | $this->thoroughTrigger(static::EVENT_BEFORE_SAVE); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Saves stored [[collection]]. |
||
| 164 | * |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | public function saveCollection() |
||
| 173 | |||
| 174 | public function beforePerform() |
||
| 175 | { |
||
| 176 | $this->thoroughTrigger(static::EVENT_BEFORE_PERFORM); |
||
| 177 | } |
||
| 178 | |||
| 179 | protected function thoroughTrigger($event) |
||
| 180 | { |
||
| 181 | if (isset($this->parent)) { |
||
| 182 | $this->parent->trigger($event); |
||
| 187 | |||
| 188 | public function afterPerform() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Performs action. |
||
| 195 | * |
||
| 196 | * @return boolean|string Whether save is success |
||
| 197 | * - boolean true or sting - an error |
||
| 198 | * - false - no errors |
||
| 199 | */ |
||
| 200 | public function perform() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return \hiqdev\hiart\ActiveRecord |
||
| 225 | */ |
||
| 226 | public function getModel() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Return the model class name. |
||
| 233 | * |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | public function getModelClass() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @return \hiqdev\hiart\ActiveRecord[] |
||
| 251 | */ |
||
| 252 | public function getModels() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Prepares additional data for render. |
||
| 259 | * |
||
| 260 | * @param $data array Additional data, prepared by other classes. Optional. |
||
| 261 | * @return array |
||
| 262 | */ |
||
| 263 | public function prepareData($data = []) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * {@inheritdoc} |
||
| 270 | */ |
||
| 271 | public function getUniqueId() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Returns text for flash messages, search in parent actions. |
||
| 278 | * Used by [[addFlash()]]. |
||
| 279 | * |
||
| 280 | * @param $type string |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | public function getFlashText($type) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Adds flash message. |
||
| 296 | * |
||
| 297 | * @param string $type the type of flash |
||
| 298 | * @param string $error the text of error |
||
| 299 | */ |
||
| 300 | public function addFlash($type, $error = null) |
||
| 316 | } |
||
| 317 |