Complex classes like Kohana_Jam_Form 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_Form, and based on these observations, apply Extract Interface, too.
1 | <?php defined('SYSPATH') OR die('No direct script access.'); |
||
12 | abstract class Kohana_Jam_Form { |
||
13 | |||
14 | /** |
||
15 | * Helper method to build a prefix based |
||
16 | * |
||
17 | * @param string $prefix |
||
18 | * @param string $name |
||
19 | * @return string |
||
20 | */ |
||
21 | 1 | public static function generate_prefix($prefix, $name) |
|
22 | { |
||
23 | 1 | $additional = array_slice(func_get_args(), 2); |
|
24 | 1 | foreach ($additional as $additional_name) |
|
25 | { |
||
26 | 1 | if ($additional_name !== NULL) |
|
27 | 1 | $name .= "[$additional_name]"; |
|
28 | 1 | } |
|
29 | |||
30 | 1 | if (strpos($prefix,'[') !== FALSE) |
|
31 | 1 | { |
|
32 | return str_replace('[%s]','',$prefix).preg_replace('/^([^\[]+)(.*)$/', "[\$1]\$2[%s]", $name); |
||
33 | } |
||
34 | else |
||
35 | { |
||
36 | 1 | return preg_replace('/^([^\[]+)(.*)$/', "{$name}[\$1]\$2", $prefix); |
|
37 | } |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Convert Jam_Builder or Jam_Collection to an array of id => name |
||
42 | * |
||
43 | * @param mixed $choices |
||
44 | * @return array |
||
45 | */ |
||
46 | 2 | public static function list_choices($choices) |
|
47 | { |
||
48 | 2 | if ($choices instanceof Jam_Query_Builder_Select OR $choices instanceof Jam_Array_Model) |
|
49 | 2 | { |
|
50 | 2 | $choices = $choices->as_array(':primary_key', ':name_key'); |
|
|
|||
51 | 2 | } |
|
52 | |||
53 | 2 | return $choices; |
|
54 | } |
||
55 | |||
56 | /** |
||
57 | * Get the id or list of ids of an object (Jam_Model / Jam_Colleciton respectively) |
||
58 | * |
||
59 | * @param int|array $id |
||
60 | * @param bool $force_single if a value is an array get the first value |
||
61 | */ |
||
62 | 5 | public static function list_id($id, $force_single = FALSE) |
|
80 | |||
81 | /** |
||
82 | * Add a class to the 'class' attribute, without removing existing value |
||
83 | * |
||
84 | * @param array $attributes |
||
85 | * @param array $class |
||
86 | */ |
||
87 | public static function add_class(array $attributes, $class) |
||
88 | { |
||
89 | $attributes['class'] = (isset($attributes['class']) ? $attributes['class'].' ' : '').$class; |
||
90 | return $attributes; |
||
91 | } |
||
92 | |||
93 | 1 | public static function common_params($collection, array $params = array()) |
|
94 | { |
||
95 | 1 | $collection = ($collection instanceof Jam_Query_Builder_Collection OR $collection instanceof Jam_Array_Model) ? $collection->as_array() : $collection; |
|
96 | |||
97 | 1 | $common = array(); |
|
98 | 1 | foreach ($params as $name => $param) |
|
99 | { |
||
100 | 1 | $attribute_name = is_numeric($name) ? $param : $name; |
|
101 | $param_collection = array_map(function($item) use ($attribute_name) { return $item->$attribute_name; }, $collection); |
||
102 | |||
103 | 1 | if (is_numeric($name)) |
|
104 | 1 | { |
|
105 | 1 | $common[$param] = array_reduce($param_collection, function($result, $item){ |
|
106 | 1 | return Jam_Form::list_id($result) !== Jam_Form::list_id($item) ? NULL : $item; |
|
107 | 1 | }, reset($param_collection)); |
|
108 | 1 | } |
|
109 | else |
||
110 | { |
||
111 | 1 | $common[$name] = Jam_Form::common_params($param_collection, $param); |
|
112 | } |
||
113 | 1 | } |
|
114 | |||
115 | 1 | return $common; |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * The "prefix" of the form - this is used to implement nesting with html forms |
||
120 | * |
||
121 | * @var string |
||
122 | */ |
||
123 | protected $_prefix = '%s'; |
||
124 | |||
125 | /** |
||
126 | * The current object the form is bound to |
||
127 | * |
||
128 | * @var Jam_Model |
||
129 | */ |
||
130 | protected $_object; |
||
131 | |||
132 | /** |
||
133 | * This flag determines if we want to have html5 validation of the form |
||
134 | * @var boolean |
||
135 | */ |
||
136 | protected $_validation = TRUE; |
||
137 | |||
138 | /** |
||
139 | * The meta of the Jam_Object the form is bound to |
||
140 | * |
||
141 | * @var Jam_Meta |
||
142 | */ |
||
143 | protected $_meta; |
||
144 | |||
145 | 14 | function __construct(Jam_Validated $model) |
|
146 | { |
||
147 | 14 | $this->_object = $model; |
|
148 | 14 | $this->_meta = Jam::meta($model); |
|
149 | 14 | } |
|
150 | |||
151 | /** |
||
152 | * Getter / setter of validation flag |
||
153 | * @param boolean $validation |
||
154 | */ |
||
155 | 11 | public function validation($validation = NULL) |
|
156 | { |
||
157 | 11 | if ($validation !== NULL) |
|
158 | 11 | { |
|
159 | $this->_validation = (bool) $validation; |
||
160 | return $this; |
||
161 | } |
||
162 | 11 | return $this->_validation; |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * Getter / setter of the prefix |
||
167 | * |
||
168 | * @param string $prefix |
||
169 | */ |
||
170 | 11 | public function prefix($prefix = NULL) |
|
171 | { |
||
172 | 11 | if ($prefix !== NULL) |
|
173 | 11 | { |
|
174 | 1 | $this->_prefix = $prefix; |
|
175 | 1 | return $this; |
|
176 | } |
||
177 | |||
178 | 11 | return $this->_prefix; |
|
179 | } |
||
180 | |||
181 | /** |
||
182 | * @return Jam_Validated this form is bound to |
||
183 | */ |
||
184 | 11 | public function object() |
|
185 | { |
||
186 | 11 | return $this->_object; |
|
187 | } |
||
188 | |||
189 | /** |
||
190 | * @return Jam_Meta of the model this form is bound to |
||
191 | */ |
||
192 | 2 | public function meta() |
|
193 | { |
||
194 | 2 | return $this->_meta; |
|
195 | } |
||
196 | |||
197 | /** |
||
198 | * Create a nested form for a child association of the model, assigning the correct prefix |
||
199 | * |
||
200 | * @param string $name of the association |
||
201 | * @param int $index an index id of a collection (if the association if a colleciton) |
||
202 | * @return Jam_Form |
||
203 | */ |
||
204 | 1 | public function fields_for($name, $index = NULL) |
|
205 | { |
||
206 | 1 | $object = $this->object()->$name; |
|
207 | |||
208 | 1 | if ($index !== NULL) |
|
209 | 1 | { |
|
210 | 1 | if (is_numeric($index) AND $object[$index]) |
|
211 | 1 | { |
|
212 | 1 | $object = $object[$index]; |
|
213 | 1 | } |
|
214 | else |
||
215 | { |
||
216 | $object = $object->build(); |
||
217 | } |
||
218 | 1 | } |
|
219 | 1 | elseif ( ! $object) |
|
220 | { |
||
221 | 1 | $object = $this->object()->build($name); |
|
222 | 1 | } |
|
223 | |||
224 | |||
225 | 1 | $new_prefix = Jam_Form::generate_prefix($this->prefix(), $name, $index); |
|
226 | |||
227 | 1 | return Jam::form($object, get_class($this))->prefix($new_prefix); |
|
228 | } |
||
229 | |||
230 | /** |
||
231 | * Get the default name for a field of this form |
||
232 | * |
||
233 | * @param string $name |
||
234 | * @return string |
||
235 | */ |
||
236 | 11 | public function default_name($name) |
|
237 | { |
||
238 | 11 | return sprintf($this->prefix(), $name); |
|
239 | } |
||
240 | |||
241 | /** |
||
242 | * Get the default html attribute id for a field of this form |
||
243 | * |
||
244 | * @param string $name |
||
245 | * @return string |
||
246 | */ |
||
247 | 11 | public function default_id($name) |
|
248 | { |
||
249 | 11 | return str_replace(array(']', '['), array('', '_'), $this->default_name($name)); |
|
250 | } |
||
251 | |||
252 | /** |
||
253 | * Get the default attributes (name and id) for a field of this form |
||
254 | * |
||
255 | * @param string $name |
||
256 | * @param array $overrides |
||
257 | * @return array |
||
258 | */ |
||
259 | 11 | public function default_attributes($name, array $overrides = array()) |
|
281 | } |
||
282 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: