Complex classes like Kohana_Jam_Field_Upload 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 Kohana_Jam_Field_Upload, and based on these observations, apply Extract Interface, too.
| 1 | <?php defined('SYSPATH') OR die('No direct script access.'); |
||
| 8 | abstract class Kohana_Jam_Field_Upload extends Jam_Field { |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @var boolean whether or not to delete the old file when a new file is added |
||
| 12 | */ |
||
| 13 | public $delete_file = TRUE; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var boolean save the sizes of the image when saving the field |
||
| 17 | */ |
||
| 18 | public $save_size = FALSE; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string the server used to store the file |
||
| 22 | */ |
||
| 23 | public $server = 'default'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string the path to the file |
||
| 27 | */ |
||
| 28 | public $path = ":model/:id"; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var array an array of transformation to apply to the image |
||
| 32 | */ |
||
| 33 | public $transformations = array(); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var bool|string the field that has the data which server we are using at the moment |
||
| 37 | */ |
||
| 38 | public $dynamic_server = NULL; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array specifications for all of the thumbnails that should be automatically generated when a new image is uploaded |
||
| 42 | */ |
||
| 43 | public $thumbnails = array(); |
||
| 44 | |||
| 45 | 16 | public function get(Jam_Validated $model, $upload_file, $is_changed) |
|
| 56 | |||
| 57 | 17 | public function set(Jam_Validated $model, $value, $is_changed) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Move the file to the temp directory even before it validates, so validaitons can be properly performed |
||
| 91 | * @param Jam_Model $model |
||
| 92 | */ |
||
| 93 | 1 | public function model_before_check(Jam_Model $model) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * After the validation for this field passes without any errors, fire up transformations and thumbnail generation |
||
| 107 | * @param Jam_Model $model |
||
| 108 | */ |
||
| 109 | public function model_after_check(Jam_Model $model) |
||
| 110 | { |
||
| 111 | if ($model->changed($this->name) AND ! $model->errors($this->name) AND $upload_file = $model->{$this->name} AND $upload_file->source()) |
||
| 112 | { |
||
| 113 | $upload_file->transform(); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Cleanup temporary file directories when the files are successfully saved |
||
| 119 | * @param Jam_Model $model |
||
| 120 | * @param boolean $is_changed |
||
| 121 | */ |
||
| 122 | public function model_after_save(Jam_Model $model) |
||
| 123 | { |
||
| 124 | if ($model->changed($this->name) AND $upload_file = $model->{$this->name} AND $upload_file->source()) |
||
| 125 | { |
||
| 126 | $upload_file |
||
| 127 | ->server($this->dynamic_server ? $model->{$this->dynamic_server} : $this->server) |
||
| 128 | ->path($this->path($model)); |
||
| 129 | |||
| 130 | // Delete the old file if there was one |
||
| 131 | if ($this->delete_file AND $original = $model->original($this->name)) |
||
| 132 | { |
||
| 133 | $this->upload_file($model)->filename($original)->delete(); |
||
| 134 | } |
||
| 135 | |||
| 136 | $upload_file->save(); |
||
| 137 | } |
||
| 138 | |||
| 139 | if ($model->changed($this->name) AND $upload_file = $model->{$this->name}) |
||
| 140 | { |
||
| 141 | $upload_file->clear(); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Get the filename to store in the database |
||
| 147 | * @param Jam_Model $model |
||
| 148 | * @param Upload_File $upload_file |
||
| 149 | * @param boolean $is_loaded |
||
| 150 | */ |
||
| 151 | public function convert(Jam_Validated $model, $upload_file, $is_loaded) |
||
| 152 | { |
||
| 153 | return ($upload_file AND $upload_file !== $this->default) ? $upload_file->filename() : $upload_file; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Remove files on model deletion |
||
| 158 | * |
||
| 159 | * @param Jam_Model $model |
||
| 160 | * @param boolean $is_changed |
||
| 161 | */ |
||
| 162 | public function model_after_delete(Jam_Validated $model, Jam_Event_Data $data, $delete_finishd) |
||
| 163 | { |
||
| 164 | if ($this->delete_file AND $delete_finishd AND $upload_file = $model->{$this->name}) |
||
| 165 | { |
||
| 166 | $upload_file->delete(); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Get upload file object for a given model |
||
| 172 | * |
||
| 173 | * @param Jam_Model $model |
||
| 174 | * @return Upload_File |
||
| 175 | */ |
||
| 176 | 17 | public function upload_file(Jam_Model $model) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Get the localized path for a given model, so that there are less filename conflicts and files are easily located, |
||
| 195 | * for example the default path is model/id so that Model_Image(1) images will be stored as images/1/file.jpg |
||
| 196 | * |
||
| 197 | * @param Jam_Model $model the model for the context |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | 17 | protected function path(Jam_Model $model) |
|
| 234 | |||
| 235 | } // End Jam_Core_Field_File |
||
| 236 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.