Total Complexity | 46 |
Total Lines | 466 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like TagField 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.
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 TagField, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class TagField extends MultiSelectField |
||
25 | { |
||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | private static $allowed_actions = array( |
||
30 | 'suggest' |
||
31 | ); |
||
32 | |||
33 | /** |
||
34 | * @var bool |
||
35 | */ |
||
36 | protected $shouldLazyLoad = false; |
||
37 | |||
38 | /** |
||
39 | * @var int |
||
40 | */ |
||
41 | protected $lazyLoadItemLimit = 10; |
||
42 | |||
43 | /** |
||
44 | * @var bool |
||
45 | */ |
||
46 | protected $canCreate = true; |
||
47 | |||
48 | /** |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $titleField = 'Title'; |
||
52 | |||
53 | /** |
||
54 | * @var DataList |
||
55 | */ |
||
56 | protected $sourceList; |
||
57 | |||
58 | /** |
||
59 | * @var bool |
||
60 | */ |
||
61 | protected $isMultiple = true; |
||
62 | |||
63 | /** @skipUpgrade */ |
||
64 | protected $schemaComponent = 'TagField'; |
||
65 | |||
66 | /** |
||
67 | * @param string $name |
||
68 | * @param string $title |
||
69 | * @param null|DataList $source |
||
70 | * @param null|DataList $value |
||
71 | * @param string $titleField |
||
72 | */ |
||
73 | public function __construct($name, $title = '', $source = [], $value = null, $titleField = 'Title') |
||
74 | { |
||
75 | $this->setSourceList($source); |
||
76 | $this->setTitleField($titleField); |
||
77 | parent::__construct($name, $title, $source, $value); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @return bool |
||
82 | */ |
||
83 | public function getShouldLazyLoad() |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param bool $shouldLazyLoad |
||
90 | * |
||
91 | * @return static |
||
92 | */ |
||
93 | public function setShouldLazyLoad($shouldLazyLoad) |
||
94 | { |
||
95 | $this->shouldLazyLoad = $shouldLazyLoad; |
||
96 | |||
97 | return $this; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @return int |
||
102 | */ |
||
103 | public function getLazyLoadItemLimit() |
||
104 | { |
||
105 | return $this->lazyLoadItemLimit; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param int $lazyLoadItemLimit |
||
110 | * |
||
111 | * @return static |
||
112 | */ |
||
113 | public function setLazyLoadItemLimit($lazyLoadItemLimit) |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @return bool |
||
122 | */ |
||
123 | public function getIsMultiple() |
||
124 | { |
||
125 | return $this->isMultiple; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @param bool $isMultiple |
||
130 | * |
||
131 | * @return static |
||
132 | */ |
||
133 | public function setIsMultiple($isMultiple) |
||
134 | { |
||
135 | $this->isMultiple = $isMultiple; |
||
136 | |||
137 | return $this; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @return bool |
||
142 | */ |
||
143 | public function getCanCreate() |
||
144 | { |
||
145 | return $this->canCreate; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param bool $canCreate |
||
150 | * |
||
151 | * @return static |
||
152 | */ |
||
153 | public function setCanCreate($canCreate) |
||
154 | { |
||
155 | $this->canCreate = $canCreate; |
||
156 | |||
157 | return $this; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @return string |
||
162 | */ |
||
163 | public function getTitleField() |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param string $titleField |
||
170 | * |
||
171 | * @return $this |
||
172 | */ |
||
173 | public function setTitleField($titleField) |
||
174 | { |
||
175 | $this->titleField = $titleField; |
||
176 | |||
177 | return $this; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Get the DataList source. The 4.x upgrade for SelectField::setSource starts to convert this to an array |
||
182 | * @return DataList |
||
183 | */ |
||
184 | public function getSourceList() |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Set the model class name for tags |
||
191 | * @param DataList $className |
||
192 | * @return self |
||
193 | */ |
||
194 | public function setSourceList($sourceList) |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * {@inheritdoc} |
||
202 | */ |
||
203 | public function Field($properties = array()) |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @return string |
||
235 | */ |
||
236 | protected function getSuggestURL() |
||
237 | { |
||
238 | return Controller::join_links($this->Link(), 'suggest'); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @return ArrayList |
||
243 | */ |
||
244 | protected function getOptions() |
||
245 | { |
||
246 | $options = ArrayList::create(); |
||
247 | |||
248 | $source = $this->getSourceList(); |
||
249 | |||
250 | if (!$source) { |
||
251 | $source = ArrayList::create(); |
||
252 | } |
||
253 | |||
254 | $dataClass = $source->dataClass(); |
||
255 | |||
256 | $values = $this->Value(); |
||
257 | |||
258 | if (!$values) { |
||
259 | return $options; |
||
260 | } |
||
261 | |||
262 | if (is_array($values)) { |
||
263 | $values = DataList::create($dataClass) |
||
264 | ->filter($this->getTitleField(), $values); |
||
265 | } |
||
266 | |||
267 | $ids = $values->column($this->getTitleField()); |
||
268 | |||
269 | $titleField = $this->getTitleField(); |
||
270 | |||
271 | foreach ($source as $object) { |
||
272 | $options->push( |
||
273 | ArrayData::create(array( |
||
274 | 'Title' => $object->$titleField, |
||
275 | 'Value' => $object->ID, |
||
276 | 'Selected' => in_array($object->$titleField, $ids), |
||
277 | )) |
||
278 | ); |
||
279 | } |
||
280 | |||
281 | return $options; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * {@inheritdoc} |
||
286 | */ |
||
287 | public function setValue($value, $source = null) |
||
288 | { |
||
289 | if ($source instanceof DataObject) { |
||
290 | $name = $this->getName(); |
||
291 | |||
292 | if ($source->hasMethod($name)) { |
||
293 | $values = []; |
||
294 | $titleField = $this->getTitleField(); |
||
295 | |||
296 | foreach ($source->$name() as $tag) { |
||
297 | $values[] = [ |
||
298 | 'Title' => $tag->$titleField, |
||
299 | 'Value' => $tag->ID, |
||
300 | 'Selected' => true |
||
301 | ]; |
||
302 | } |
||
303 | |||
304 | return parent::setValue($values); |
||
305 | } |
||
306 | } |
||
307 | |||
308 | if (!is_array($value)) { |
||
309 | return parent::setValue($value); |
||
310 | } |
||
311 | |||
312 | return parent::setValue($value); |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * {@inheritdoc} |
||
317 | */ |
||
318 | public function getAttributes() |
||
319 | { |
||
320 | return array_merge( |
||
321 | parent::getAttributes(), |
||
322 | [ |
||
323 | 'name' => $this->getName() . '[]', |
||
324 | 'style'=> 'width: 100%' |
||
325 | ] |
||
326 | ); |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * {@inheritdoc} |
||
331 | */ |
||
332 | public function saveInto(DataObjectInterface $record) |
||
333 | { |
||
334 | $name = $this->getName(); |
||
335 | $titleField = $this->getTitleField(); |
||
336 | $source = $this->getSource(); |
||
337 | $values = $this->Value(); |
||
338 | $relation = $record->$name(); |
||
339 | $ids = array(); |
||
340 | |||
341 | if (!$values) { |
||
342 | $values = array(); |
||
343 | } |
||
344 | |||
345 | if (empty($record) || empty($titleField)) { |
||
346 | return; |
||
347 | } |
||
348 | |||
349 | if (!$record->hasMethod($name)) { |
||
350 | throw new Exception( |
||
351 | sprintf("%s does not have a %s method", get_class($record), $name) |
||
352 | ); |
||
353 | } |
||
354 | |||
355 | foreach ($values as $key => $value) { |
||
356 | // Get or create record |
||
357 | $record = $this->getOrCreateTag($value); |
||
358 | if ($record) { |
||
359 | $ids[] = $record->ID; |
||
360 | $values[$key] = $record->Title; |
||
361 | } |
||
362 | } |
||
363 | |||
364 | $relation->setByIDList(array_filter($ids)); |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Get or create tag with the given value |
||
369 | * |
||
370 | * @param string $term |
||
371 | * @return DataObject |
||
372 | */ |
||
373 | protected function getOrCreateTag($term) |
||
400 | } |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * Returns a JSON string of tags, for lazy loading. |
||
405 | * |
||
406 | * @param HTTPRequest $request |
||
407 | * @return HTTPResponse |
||
408 | */ |
||
409 | public function suggest(HTTPRequest $request) |
||
410 | { |
||
411 | $tags = $this->getTags($request->getVar('term')); |
||
412 | |||
413 | $response = new HTTPResponse(); |
||
414 | $response->addHeader('Content-Type', 'application/json'); |
||
415 | $response->setBody(json_encode(array('items' => $tags))); |
||
416 | |||
417 | return $response; |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * Returns array of arrays representing tags. |
||
422 | * |
||
423 | * @param string $term |
||
424 | * @return array |
||
425 | */ |
||
426 | protected function getTags($term) |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * DropdownField assumes value will be a scalar so we must |
||
455 | * override validate. This only applies to Silverstripe 3.2+ |
||
456 | * |
||
457 | * @param Validator $validator |
||
458 | * @return bool |
||
459 | */ |
||
460 | public function validate($validator) |
||
463 | } |
||
464 | |||
465 | /** |
||
466 | * Converts the field to a readonly variant. |
||
467 | * |
||
468 | * @return TagField_Readonly |
||
469 | */ |
||
470 | public function performReadonlyTransformation() |
||
475 | } |
||
476 | |||
477 | public function getSchemaStateDefaults() |
||
478 | { |
||
479 | $data = parent::getSchemaStateDefaults(); |
||
480 | |||
481 | // Add options to 'data' |
||
493 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.