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 | const EXPECTED_AJAX_RESPONSE_HEADER_NAME = 'X-Expected-Ajax-Response'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Controller the controller that owns this action |
||
| 38 | */ |
||
| 39 | public $controller; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var Action|SwitchAction parent called action |
||
| 43 | */ |
||
| 44 | public $parent; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string scenario to be used when save |
||
| 48 | */ |
||
| 49 | public $_scenario; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Perform query options. |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | public $queryOptions = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array|Closure additional data passed when rendering |
||
| 59 | */ |
||
| 60 | public $data = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param string $scenario |
||
| 64 | */ |
||
| 65 | public function setScenario($scenario) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | public function getScenario() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var Collection|array the options that will be used to create the collection. |
||
| 80 | * Stores collection after creating |
||
| 81 | */ |
||
| 82 | protected $_collection; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Setter for the collection. |
||
| 86 | * |
||
| 87 | * @param array $collection config for the collection |
||
| 88 | */ |
||
| 89 | public function setCollection($collection) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Gets the instance of the collection. |
||
| 96 | * |
||
| 97 | * @return Collection |
||
| 98 | */ |
||
| 99 | public function getCollection() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var callable the custom callback to load data into the collection. Gets [[$this]] as the only argument |
||
| 126 | * Should call `$this->collection->load()` |
||
| 127 | */ |
||
| 128 | public $collectionLoader; |
||
| 129 | |||
| 130 | protected function beforeRun() |
||
| 136 | |||
| 137 | public function beforeLoad() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Loads data to the [[collection]]. |
||
| 144 | * |
||
| 145 | * @param array $data |
||
| 146 | */ |
||
| 147 | public function loadCollection($data = null) |
||
| 157 | |||
| 158 | public function beforeSave() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Saves stored [[collection]]. |
||
| 165 | * |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | public function saveCollection() |
||
| 174 | |||
| 175 | public function beforePerform() |
||
| 179 | |||
| 180 | protected function thoroughTrigger($event) |
||
| 188 | |||
| 189 | public function afterPerform() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Performs action. |
||
| 196 | * |
||
| 197 | * @return boolean|string Whether save is success |
||
| 198 | * - boolean true or sting - an error |
||
| 199 | * - false - no errors |
||
| 200 | */ |
||
| 201 | public function perform() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return \hiqdev\hiart\ActiveRecord |
||
| 226 | */ |
||
| 227 | public function getModel() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Return the model class name. |
||
| 234 | * |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | public function getModelClass() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return \hiqdev\hiart\ActiveRecord[] |
||
| 252 | */ |
||
| 253 | public function getModels() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Prepares additional data for render. |
||
| 260 | * |
||
| 261 | * @param $data array Additional data, prepared by other classes. Optional. |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | public function prepareData($data = []) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * {@inheritdoc} |
||
| 271 | */ |
||
| 272 | public function getUniqueId() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Returns text for flash messages, search in parent actions. |
||
| 279 | * Used by [[addFlash()]]. |
||
| 280 | * |
||
| 281 | * @param $type string |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | public function getFlashText($type) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Adds flash message. |
||
| 297 | * |
||
| 298 | * @param string $type the type of flash |
||
| 299 | * @param string $error the text of error |
||
| 300 | */ |
||
| 301 | public function addFlash($type, $error = null) |
||
| 317 | } |
||
| 318 |