Complex classes like ConfirmationTrait 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 ConfirmationTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | trait ConfirmationTrait |
||
| 30 | { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var int Unconfirmed. |
||
| 34 | */ |
||
| 35 | public static $confirmFalse = 0; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var int Confirmed. |
||
| 39 | */ |
||
| 40 | public static $confirmTrue = 1; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string|false attribute name of confirmation, or false if disable confirmation features. |
||
| 44 | */ |
||
| 45 | public $confirmationAttribute = false; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string This attribute specify the name of confirm_code attribute, if |
||
| 49 | * this attribute is assigned to false, this feature will be ignored. |
||
| 50 | * if $confirmationAttribute is empty or false, this attribute will be skipped. |
||
| 51 | */ |
||
| 52 | public $confirmCodeAttribute = 'confirm_code'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var integer The expiration in seconds. If $confirmCodeAttribute is |
||
| 56 | * specified, this attribute must be specified. |
||
| 57 | */ |
||
| 58 | public $confirmCodeExpiration = 3600; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var string This attribute specify the name of confirm_time attribute. if |
||
| 62 | * this attribute is assigned to false, this feature will be ignored. |
||
| 63 | * if $confirmationAttribute is empty or false, this attribute will be skipped. |
||
| 64 | */ |
||
| 65 | public $confirmTimeAttribute = 'confirmed_at'; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string initialization confirm time. |
||
| 69 | */ |
||
| 70 | public $initConfirmTime = '1970-01-01 00:00:00'; |
||
| 71 | public static $eventConfirmationChanged = "confirmationChanged"; |
||
| 72 | public static $eventConfirmationCanceled = "confirmationCanceled"; |
||
| 73 | public static $eventConfirmationSuceeded = "confirmationSucceeded"; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Apply confirmation. |
||
| 77 | * @return boolean |
||
| 78 | * @throws \yii\base\NotSupportedException |
||
| 79 | */ |
||
| 80 | 3 | public function applyConfirmation() |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Set confirm code. |
||
| 91 | * @param string $code |
||
| 92 | */ |
||
| 93 | 26 | public function setConfirmCode($code) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Get confirm code. |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | 3 | public function getConfirmCode() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Confirm the current content. |
||
| 123 | * @param string $code |
||
| 124 | * @return boolean |
||
| 125 | */ |
||
| 126 | 3 | public function confirm($code = '') |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Generate confirmation code. |
||
| 137 | * @return string code |
||
| 138 | */ |
||
| 139 | 3 | public function generateConfirmationCode() |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Validate the confirmation code. |
||
| 146 | * @param string $code |
||
| 147 | * @return boolean Whether the confirmation code is valid. |
||
| 148 | */ |
||
| 149 | 3 | public function validateConfirmationCode($code) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Get confirmation status of current model. |
||
| 160 | * @return boolean Whether current model has been confirmed. |
||
| 161 | */ |
||
| 162 | 5 | public function getIsConfirmed() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Initialize the confirmation status. |
||
| 170 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 171 | * override or modify it directly, unless you know the consequences. |
||
| 172 | * @param ModelEvent $event |
||
| 173 | */ |
||
| 174 | 209 | public function onInitConfirmation($event) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Set confirmation. |
||
| 187 | * @param mixed $value |
||
| 188 | */ |
||
| 189 | 31 | public function setConfirmation($value) |
|
| 190 | { |
||
| 191 | 31 | $cAttribute = $this->confirmationAttribute; |
|
| 192 | 31 | if (!$cAttribute || empty($cAttribute)) { |
|
| 193 | 5 | return; |
|
| 194 | } |
||
| 195 | 26 | $this->$cAttribute = $value; |
|
| 196 | 26 | $this->trigger(static::$eventConfirmationChanged); |
|
| 197 | 26 | } |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Get confirmation. |
||
| 201 | * @return mixed |
||
| 202 | */ |
||
| 203 | 1 | public function getConfirmation() |
|
| 204 | { |
||
| 205 | 1 | $cAttribute = $this->confirmationAttribute; |
|
| 206 | 1 | return (is_string($cAttribute) && !empty($cAttribute)) ? $this->$cAttribute : null; |
|
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * When confirmation status changed, this event will be triggered. If |
||
| 211 | * confirmation succeeded, the confirm_time will be assigned to current time, |
||
| 212 | * or the confirm_time will be assigned to initConfirmTime. |
||
| 213 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 214 | * override or modify it directly, unless you know the consequences. |
||
| 215 | * @param ModelEvent $event |
||
| 216 | */ |
||
| 217 | 26 | public function onConfirmationChanged($event) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Get rules associated with confirmation attributes. |
||
| 237 | * if not enable confirmation feature, it will return empty array. |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | 199 | public function getConfirmationRules() |
|
| 250 | |||
| 251 | /** |
||
| 252 | * When the content changed, reset confirmation status. |
||
| 253 | */ |
||
| 254 | 58 | protected function resetConfirmation() |
|
| 255 | { |
||
| 256 | 58 | $contentAttribute = $this->contentAttribute; |
|
| 257 | 58 | if (!$contentAttribute || empty($contentAttribute)) { |
|
| 258 | 11 | return; |
|
| 259 | } |
||
| 260 | 47 | if (is_array($contentAttribute)) { |
|
| 261 | foreach ($contentAttribute as $attribute) { |
||
| 262 | if ($this->isAttributeChanged($attribute)) { |
||
| 263 | $this->confirmation = static::$confirmFalse; |
||
| 264 | break; |
||
| 265 | } |
||
| 266 | } |
||
| 267 | 47 | } elseif ($this->isAttributeChanged($contentAttribute)) { |
|
| 268 | 6 | $this->confirmation = static::$confirmFalse; |
|
| 269 | } |
||
| 270 | 47 | } |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Reset others' confirmation when the others own the same content. |
||
| 274 | */ |
||
| 275 | 4 | protected function resetOthersConfirmation() |
|
| 289 | } |
||
| 290 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.