Complex classes like HasUploadTrait 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 HasUploadTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | trait HasUploadTrait |
||
| 8 | { |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Returns the upload container for the element |
||
| 12 | * |
||
| 13 | * @return null|mixed |
||
| 14 | */ |
||
| 15 | 1 | function getUploadContainer() |
|
| 19 | |||
| 20 | /** |
||
| 21 | * Sets the upload container for the elment |
||
| 22 | * |
||
| 23 | * @param string|\Sirius\Upload\Container\ContainerInterface $container |
||
| 24 | * |
||
| 25 | * @return $this |
||
| 26 | */ |
||
| 27 | 3 | function setUploadContainer($container) |
|
| 33 | |||
| 34 | /** |
||
| 35 | * Get the upload options (overwrite, auto confirm, etc) |
||
| 36 | * |
||
| 37 | * @see \Sirius\Upload\Handler |
||
| 38 | * @return null|array |
||
| 39 | */ |
||
| 40 | 1 | function getUploadOptions() |
|
| 44 | |||
| 45 | /** |
||
| 46 | * Get the upload options (overwrite, auto confirm, etc) |
||
| 47 | * |
||
| 48 | * @param array $options |
||
| 49 | * |
||
| 50 | * @return $this |
||
| 51 | */ |
||
| 52 | 2 | function setUploadOptions($options = array()) |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Get the validation rules for the uploaded file(s) |
||
| 61 | * |
||
| 62 | * @return null|array |
||
| 63 | */ |
||
| 64 | 1 | function getUploadRules() |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Sets the validation rules for the uploaded file(s) |
||
| 71 | * |
||
| 72 | * @param array $rules |
||
| 73 | * |
||
| 74 | * @return $this |
||
| 75 | */ |
||
| 76 | 3 | function setUploadRules($rules = array()) |
|
| 82 | } |
||
| 83 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.