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 |
||
| 28 | class Action extends \yii\base\Action |
||
| 29 | { |
||
| 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 | * @var array|Closure additional data passed when rendering |
||
| 52 | */ |
||
| 53 | public $data = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param string $scenario |
||
| 57 | */ |
||
| 58 | public function setScenario($scenario) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | public function getScenario() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var Collection|array the options that will be used to create the collection. |
||
| 73 | * Stores collection after creating |
||
| 74 | */ |
||
| 75 | protected $_collection; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Setter for the collection. |
||
| 79 | * |
||
| 80 | * @param array $collection config for the collection |
||
| 81 | */ |
||
| 82 | public function setCollection($collection) |
||
| 83 | { |
||
| 84 | $this->_collection = $collection; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Gets the instance of the collection. |
||
| 89 | * |
||
| 90 | * @return Collection |
||
| 91 | */ |
||
| 92 | public function getCollection() |
||
| 93 | { |
||
| 94 | if ($this->parent) { |
||
| 95 | return $this->parent->getCollection(); |
||
| 96 | } |
||
| 97 | |||
| 98 | if (!is_object($this->_collection)) { |
||
| 99 | $action = $this->controller->action; |
||
| 100 | if ($action instanceof self) { |
||
| 101 | $scenario = $action->getScenario(); |
||
| 102 | } else { |
||
| 103 | $scenario = $action->id; |
||
| 104 | } |
||
| 105 | |||
| 106 | $this->_collection = Yii::createObject(ArrayHelper::merge([ |
||
| 107 | 'class' => Collection::className(), |
||
| 108 | 'model' => $this->controller->newModel(), |
||
| 109 | 'scenario' => $scenario, |
||
| 110 | ], (array) $this->_collection)); |
||
| 111 | } |
||
| 112 | |||
| 113 | return $this->_collection; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var callable the custom callback to load data into the collection. Gets [[$this]] as the only argument |
||
| 118 | * Should call `$this->collection->load()` |
||
| 119 | */ |
||
| 120 | public $collectionLoader; |
||
| 121 | |||
| 122 | public function beforeLoad() |
||
| 123 | { |
||
| 124 | if (isset($this->parent)) { |
||
| 125 | $this->parent->trigger(static::EVENT_BEFORE_LOAD); |
||
| 126 | } |
||
| 127 | |||
| 128 | $this->trigger(static::EVENT_BEFORE_LOAD); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Loads data to the [[collection]]. |
||
| 133 | * |
||
| 134 | * @param array $data |
||
| 135 | */ |
||
| 136 | public function loadCollection($data = null) |
||
| 137 | { |
||
| 138 | $this->beforeLoad(); |
||
| 139 | |||
| 140 | if ($this->collectionLoader instanceof Closure) { |
||
| 141 | call_user_func($this->collectionLoader, $this, $data); |
||
| 142 | } else { |
||
| 143 | $this->collection->load($data); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | public function beforeSave() |
||
| 148 | { |
||
| 149 | if (isset($this->parent)) { |
||
| 150 | $this->parent->trigger(static::EVENT_BEFORE_SAVE); |
||
| 151 | } |
||
| 152 | |||
| 153 | $this->trigger(static::EVENT_BEFORE_SAVE); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Saves stored [[collection]]. |
||
| 158 | * |
||
| 159 | * @return bool |
||
| 160 | */ |
||
| 161 | public function saveCollection() |
||
| 162 | { |
||
| 163 | $this->beforeSave(); |
||
| 164 | return $this->collection->save(); |
||
| 165 | } |
||
| 166 | |||
| 167 | public function beforePerform() |
||
| 168 | { |
||
| 169 | if (isset($this->parent)) { |
||
| 170 | $this->parent->trigger(static::EVENT_BEFORE_PERFORM); |
||
| 171 | } |
||
| 172 | |||
| 173 | $this->trigger(static::EVENT_BEFORE_PERFORM); |
||
| 174 | } |
||
| 175 | |||
| 176 | public function afterPerform() |
||
| 177 | { |
||
| 178 | if (isset($this->parent)) { |
||
| 179 | $this->parent->trigger(static::EVENT_AFTER_PERFORM); |
||
| 180 | } |
||
| 181 | |||
| 182 | $this->trigger(static::EVENT_AFTER_PERFORM); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Performs action. |
||
| 187 | * |
||
| 188 | * @return boolean|string Whether save is success |
||
| 189 | * - boolean true or sting - an error |
||
| 190 | * - false - no errors |
||
| 191 | */ |
||
| 192 | public function perform() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @return \hiqdev\hiart\ActiveRecord |
||
| 216 | */ |
||
| 217 | public function getModel() |
||
| 218 | { |
||
| 219 | return $this->parent ? $this->parent->getModel() : $this->getCollection()->first; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Return the model class name |
||
| 224 | * |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | public function getModelClass() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @return \hiqdev\hiart\ActiveRecord[] |
||
| 242 | */ |
||
| 243 | public function getModels() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Prepares additional data for render. |
||
| 250 | * |
||
| 251 | * @param $data array Additional data, prepared by other classes. Optional. |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | public function prepareData($data = []) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * {@inheritdoc} |
||
| 261 | */ |
||
| 262 | public function getUniqueId() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Returns text for flash messages, search in parent actions. |
||
| 269 | * Used by [[addFlash()]]. |
||
| 270 | * |
||
| 271 | * @param $type string |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | public function getFlashText($type) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Adds flash message. |
||
| 287 | * |
||
| 288 | * @param string $type the type of flash |
||
| 289 | * @param string $error the text of error |
||
| 290 | */ |
||
| 291 | public function addFlash($type, $error = null) |
||
| 307 | } |
||
| 308 |