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) |
|
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) |
|
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 string $class |
||
86 | */ |
||
87 | public static function add_class(array $attributes, $class) |
||
92 | |||
93 | 1 | public static function common_params($collection, array $params = array()) |
|
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) |
|
150 | |||
151 | /** |
||
152 | * Getter / setter of validation flag |
||
153 | * @param boolean $validation |
||
154 | */ |
||
155 | 11 | public function validation($validation = NULL) |
|
164 | |||
165 | /** |
||
166 | * Getter / setter of the prefix |
||
167 | * |
||
168 | * @param string $prefix |
||
169 | */ |
||
170 | 11 | public function prefix($prefix = NULL) |
|
180 | |||
181 | /** |
||
182 | * @return Jam_Validated this form is bound to |
||
183 | */ |
||
184 | 11 | public function object() |
|
188 | |||
189 | /** |
||
190 | * @return Jam_Meta of the model this form is bound to |
||
191 | */ |
||
192 | 2 | public function meta() |
|
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 static |
||
203 | */ |
||
204 | 1 | public function fields_for($name, $index = NULL) |
|
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) |
|
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) |
|
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: