Complex classes like CryptTrait 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 CryptTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | trait CryptTrait |
||
| 12 | { |
||
| 13 | protected $cipher; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Set the key |
||
| 17 | * |
||
| 18 | * @param string $key |
||
| 19 | * |
||
| 20 | * @return self |
||
| 21 | */ |
||
| 22 | public function key($key) |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Encrypt the given value. |
||
| 32 | * |
||
| 33 | * @param string $value |
||
| 34 | * |
||
| 35 | * @return string |
||
| 36 | */ |
||
| 37 | protected function encrypt($value) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Decrypt the given value. |
||
| 48 | * |
||
| 49 | * @param string $value |
||
| 50 | * |
||
| 51 | * @return string |
||
| 52 | */ |
||
| 53 | protected function decrypt($value) |
||
| 61 | } |
||
| 62 |