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 | 18 | public function get(Jam_Validated $model, $upload_file, $is_changed) |
|
| 46 | { |
||
| 47 | 18 | if ( ! ($upload_file instanceof Upload_File)) |
|
| 48 | 18 | { |
|
| 49 | 4 | $upload_file = $this->upload_file($model); |
|
|
|
|||
| 50 | 4 | } |
|
| 51 | |||
| 52 | return $upload_file |
||
| 53 | 18 | ->server($this->dynamic_server ? $model->{$this->dynamic_server} : $this->server) |
|
| 54 | 18 | ->path($this->path($model)); |
|
| 55 | } |
||
| 56 | |||
| 57 | 19 | public function set(Jam_Validated $model, $value, $is_changed) |
|
| 58 | { |
||
| 59 | 19 | if ( ! $is_changed) |
|
| 60 | 19 | return $this->upload_file($model)->filename($value); |
|
| 61 | |||
| 62 | 4 | if ($value instanceof Upload_File) |
|
| 63 | 4 | { |
|
| 64 | 1 | $upload_file = $value; |
|
| 65 | 1 | } |
|
| 66 | else |
||
| 67 | { |
||
| 68 | 3 | $upload_file = $model->{$this->name}; |
|
| 69 | |||
| 70 | 3 | if ($value === NULL) |
|
| 71 | 3 | { |
|
| 72 | $upload_file->filename(''); |
||
| 73 | } |
||
| 74 | 3 | elseif ($value) |
|
| 75 | { |
||
| 76 | 3 | if (Upload_Source::valid($value)) |
|
| 77 | 3 | { |
|
| 78 | 2 | $upload_file->source($value); |
|
| 79 | 2 | } |
|
| 80 | |||
| 81 | 3 | $upload_file->filename($value); |
|
| 82 | 3 | } |
|
| 83 | |||
| 84 | } |
||
| 85 | |||
| 86 | 4 | return $upload_file->path($this->path($model)); |
|
| 87 | } |
||
| 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 | 3 | public function model_before_check(Jam_Model $model) |
|
| 94 | { |
||
| 95 | 3 | if ($model->changed($this->name) AND $upload_file = $model->{$this->name} AND $upload_file->source()) |
|
| 96 | 3 | { |
|
| 97 | try { |
||
| 98 | 1 | $upload_file->save_to_temp(); |
|
| 99 | 1 | } catch (Kohana_Exception $e) { |
|
| 100 | $model->errors()->add($this->name, 'uploaded_native', array(':message' => $e->getMessage())); |
||
| 101 | } |
||
| 102 | 1 | } |
|
| 103 | 3 | } |
|
| 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 | 2 | public function model_after_check(Jam_Model $model) |
|
| 110 | { |
||
| 111 | 2 | if ($model->changed($this->name) AND ! $model->errors($this->name) AND $upload_file = $model->{$this->name} AND $upload_file->source()) |
|
| 112 | 2 | { |
|
| 113 | $upload_file->transform(); |
||
| 114 | } |
||
| 115 | 2 | } |
|
| 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 | 2 | public function model_after_save(Jam_Model $model) |
|
| 123 | { |
||
| 124 | 2 | if ($model->changed($this->name) AND $upload_file = $model->{$this->name} AND $upload_file->source()) |
|
| 125 | 2 | { |
|
| 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 | 2 | if ($model->changed($this->name) AND $upload_file = $model->{$this->name}) |
|
| 140 | 2 | { |
|
| 141 | $upload_file->clear(); |
||
| 142 | } |
||
| 143 | 2 | } |
|
| 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 | 2 | public function convert(Jam_Validated $model, $upload_file, $is_loaded) |
|
| 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 | 19 | 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 | 19 | 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.