Complex classes like source 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 source, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
46 | abstract class source{ |
||
47 | /** |
||
48 | * The name of an wrapper to use if the class wants to provide an override |
||
49 | */ |
||
50 | public $wrapperName; |
||
51 | protected $_config; |
||
52 | protected $_mapping; |
||
53 | protected $_field_defs; |
||
54 | |||
55 | /** |
||
56 | * @var bool enable_in_wizard Boolean value marking whether or not the connector may appear in the wizard (Get Data) views |
||
57 | */ |
||
58 | protected $_enable_in_wizard = true; |
||
59 | |||
60 | /** |
||
61 | * @var bool enable_in_hover Boolean value marking whether or not a hover link could be applied to the connector |
||
62 | */ |
||
63 | protected $_enable_in_hover = false; |
||
64 | |||
65 | /** |
||
66 | * @var bool enable_in_admin_mapping Boolean value marking whether or not this connector should be shown in the Modify Mapping view |
||
67 | */ |
||
68 | protected $_enable_in_admin_mapping = true; |
||
69 | |||
70 | /** |
||
71 | * @var bool enable_in_admin_properties Boolean value marking whether or not this connector should appear in the Set Connector Properties view |
||
72 | */ |
||
73 | protected $_enable_in_admin_properties = true; |
||
74 | |||
75 | /** |
||
76 | * @var bool enable_in_admin_display Boolean value marking whether or not this connector should appear in the Enable Connectors view |
||
77 | */ |
||
78 | protected $_enable_in_admin_display = true; |
||
79 | |||
80 | /** |
||
81 | * @var bool enable_in_admin_search Boolean value marking whether or not this connector should appear in the Manage Connector Search view |
||
82 | */ |
||
83 | protected $_enable_in_admin_search = true; |
||
84 | |||
85 | /** |
||
86 | * @var bool has_testing_enabled Boolean value marking whether or not the connector should display the test button in administration view |
||
87 | */ |
||
88 | protected $_has_testing_enabled = false; |
||
89 | |||
90 | protected $_required_config_fields = array(); |
||
91 | protected $_required_config_fields_for_button = array(); |
||
92 | protected $config_decrypted = false; |
||
93 | |||
94 | /** |
||
95 | * The ExternalAPI Base that instantiated this connector. |
||
96 | * @var _eapm |
||
97 | */ |
||
98 | protected $_eapm = null; |
||
99 | |||
100 | public function __construct(){ |
||
105 | |||
106 | public function init(){} |
||
107 | |||
108 | //////// CALLED FROM component.php /////// |
||
109 | public function loadMapping() { |
||
119 | |||
120 | public function saveMappingHook($mapping) { |
||
124 | |||
125 | /** |
||
126 | * Load source's vardef file |
||
127 | */ |
||
128 | public function loadVardefs() { |
||
139 | |||
140 | /** |
||
141 | * Given a parameter in a vardef field, return the list of fields that match the param and value |
||
142 | * |
||
143 | * @param string $param_name |
||
144 | * @param string $param_value |
||
145 | * @return array |
||
146 | */ |
||
147 | public function getFieldsWithParams($param_name, $param_value) |
||
160 | |||
161 | /** |
||
162 | * Save source's config to custom directory |
||
163 | */ |
||
164 | public function saveConfig() |
||
191 | |||
192 | /** |
||
193 | * Initialize config - decrypt encrypted fields |
||
194 | */ |
||
195 | public function initConfig() |
||
209 | |||
210 | /** |
||
211 | * Load config.php for this source |
||
212 | */ |
||
213 | public function loadConfig() |
||
232 | |||
233 | // Helper function for the settings panels |
||
234 | /** |
||
235 | * Filter which modules are allowed to connect |
||
236 | * @param array $moduleList |
||
237 | * @return array Allowed modules |
||
238 | */ |
||
239 | public function filterAllowedModules( $moduleList ) |
||
244 | |||
245 | ////////////// GETTERS and SETTERS //////////////////// |
||
246 | public function getMapping() |
||
250 | |||
251 | public function getOriginalMapping() { |
||
261 | |||
262 | public function setMapping($mapping) |
||
266 | |||
267 | public function getFieldDefs() |
||
271 | |||
272 | public function getConfig() |
||
277 | |||
278 | public function setConfig($config) |
||
283 | |||
284 | public function setEAPM(ExternalAPIBase $eapm) |
||
288 | |||
289 | public function getEAPM() |
||
293 | |||
294 | public function setProperties($properties=array()) |
||
301 | |||
302 | public function getProperties() |
||
310 | |||
311 | /** |
||
312 | * Check if certain property contains non-empty value |
||
313 | * @param string $name |
||
314 | * @return bool |
||
315 | */ |
||
316 | public function propertyExists($name) |
||
320 | |||
321 | public function getProperty($name) |
||
333 | |||
334 | /** |
||
335 | * hasTestingEnabled |
||
336 | * This method is used to indicate whether or not a data source has testing enabled so that |
||
337 | * the administration interface may call the test method on the data source instance |
||
338 | * |
||
339 | * @return enabled boolean value indicating whether or not testing is enabled |
||
340 | */ |
||
341 | public function hasTestingEnabled() { |
||
344 | |||
345 | /** |
||
346 | * test |
||
347 | * This method is called from the administration interface to run a test of the service |
||
348 | * It is up to subclasses to implement a test and set _has_testing_enabled to true so that |
||
349 | * a test button is rendered in the administration interface |
||
350 | * |
||
351 | * @return result boolean result of the test function |
||
352 | */ |
||
353 | public function test() { |
||
356 | |||
357 | |||
358 | /** |
||
359 | * isEnabledInWizard |
||
360 | * This method indicates whether or not the connector should be enabled in the wizard |
||
361 | * Connectors that do not support the getList/getItem methods via API calls should |
||
362 | * set the protected class variable _enable_in_wizard to false. |
||
363 | * |
||
364 | * @return $enabled boolean variable indicating whether or not the connector is enabled for the wizard |
||
365 | */ |
||
366 | public function isEnabledInWizard() { |
||
369 | |||
370 | |||
371 | /** |
||
372 | * isEnabledInHover |
||
373 | * This method indicates whether or not the connector should be enabled for the hover links |
||
374 | * Connectors that do not provide a formatter implementation should not |
||
375 | * set the protected class variable _enable_in_hover to true. |
||
376 | * |
||
377 | * @return $enabled boolean variable indicating whether or not the connector is enabled for the hover links |
||
378 | * |
||
379 | */ |
||
380 | public function isEnabledInHover() { |
||
383 | |||
384 | /** |
||
385 | * isEnabledInAdminProperties |
||
386 | * This method indicates whether or not the connector should be shown in the Set Connector Properties view. |
||
387 | * The Admin views call each source's isEnabledInAdminProperties method to verify whether or not the connector should be |
||
388 | * displayed. Connectors that do not have any administrative properties should set the protected class variable |
||
389 | * _enable_in_admin_properties to false. |
||
390 | * |
||
391 | * @return boolean value indicating whether or not the connector is enabled for admin views |
||
392 | */ |
||
393 | public function isEnabledInAdminProperties() |
||
397 | |||
398 | /** |
||
399 | * isEnabledInAdminMapping |
||
400 | * This method indicates whether or not the connector should be shown in the Map Connector Fields view. |
||
401 | * The Admin views call each source's isEnabledInAdminMapping method to verify whether or not the connector should be |
||
402 | * displayed. Connectors that do not have any administrative mapping properties should set the protected class variable |
||
403 | * _enable_in_admin_mapping to false. |
||
404 | * |
||
405 | * @return boolean value indicating whether or not the connector is enabled for admin views |
||
406 | */ |
||
407 | public function isEnabledInAdminMapping() |
||
411 | |||
412 | /** |
||
413 | * isEnabledInAdminDisplay |
||
414 | * This method indicates whether or not the connector should be shown in the Enable Connectors view. |
||
415 | * The Admin views call each source's isEnabledInAdminDisplay method to verify whether or not the connector should be |
||
416 | * displayed. Connectors that do not have any administrative display settings should set the protected class variable |
||
417 | * _enable_in_admin_display to false. |
||
418 | * |
||
419 | * @return boolean value indicating whether or not the connector is enabled for admin views |
||
420 | */ |
||
421 | public function isEnabledInAdminDisplay() |
||
425 | |||
426 | /** |
||
427 | * isEnabledInAdminSearch |
||
428 | * This method indicates whether or not the connector should be shown in the Manage Connectors Search view. |
||
429 | * The Admin views call each source's isEnabledInAdminSearch method to verify whether or not the connector should be |
||
430 | * displayed. Connectors that do not have any administrative search settings should set the protected class variable |
||
431 | * _enable_in_admin_search to false. |
||
432 | * |
||
433 | * @return boolean value indicating whether or not the connector is enabled for admin views |
||
434 | */ |
||
435 | public function isEnabledInAdminSearch() |
||
439 | |||
440 | /** |
||
441 | * getRequiredConfigFields |
||
442 | * This method returns an Array of the configuration keys that are required for the Connector. |
||
443 | * Subclasses should set the class variable _required_config_fields to |
||
444 | * return an Array of keys as specified in the Connector's config.php that are required. |
||
445 | * |
||
446 | * @return $fields Array of Connector config fields that are required |
||
447 | */ |
||
448 | public function getRequiredConfigFields() { |
||
451 | |||
452 | |||
453 | /** |
||
454 | * isRequiredConfigFieldsSet |
||
455 | * This method checks the configuration parameters against the required config fields |
||
456 | * to see if they are set |
||
457 | * |
||
458 | * @return $set boolean value indicating whether or not the required config fields are set |
||
459 | */ |
||
460 | public function isRequiredConfigFieldsSet() { |
||
469 | |||
470 | |||
471 | /** |
||
472 | * getRequiredConfigFieldsForButton |
||
473 | * This method returns an Array of the configuration keys that are required before the |
||
474 | * "Get Data" button will include the Connector. We use it as a subset of the |
||
475 | * $this->_required_config_fields Array. |
||
476 | * |
||
477 | * @return $fields Array of Connector config fields that are required to be set for the "Get Data" button to appear |
||
478 | */ |
||
479 | public function getRequiredConfigFieldsForButton() { |
||
482 | |||
483 | |||
484 | /** |
||
485 | * isRequiredConfigFieldsForButtonSet |
||
486 | * This method checks the configuration parameters against the required config fields |
||
487 | * for the "Get Button" to see if they are set |
||
488 | * |
||
489 | * @return $set boolean value indicating whether or not the required config fields are set |
||
490 | */ |
||
491 | public function isRequiredConfigFieldsForButtonSet() { |
||
500 | |||
501 | |||
502 | /** |
||
503 | * Allow data sources to log information |
||
504 | * |
||
505 | * @param string $log_data |
||
506 | */ |
||
507 | protected function log($log_data){ |
||
515 | |||
516 | /** |
||
517 | * getItem |
||
518 | * Returns an array containing a key/value pair(s) of a connector record. To be overridden by the implementation |
||
519 | * source. |
||
520 | * |
||
521 | * @param $args Array of arguments to search/filter by |
||
522 | * @param $module String optional value of the module that the connector framework is attempting to map to |
||
523 | * @return Array of key/value pair(s) of connector record; empty Array if no results are found |
||
524 | */ |
||
525 | public abstract function getItem($args=array(), $module=null); |
||
526 | |||
527 | |||
528 | /** |
||
529 | * getList |
||
530 | * Returns a nested array containing a key/value pair(s) of a connector record. To be overridden by the |
||
531 | * implementation source. |
||
532 | * |
||
533 | * @param $args Array of arguments to search/filter by |
||
534 | * @param $module String optional value of the module that the connector framework is attempting to map to |
||
535 | * @return Array of key/value pair(s) of connector record; empty Array if no results are found |
||
536 | */ |
||
537 | public abstract function getList($args=array(), $module=null); |
||
538 | |||
539 | /** |
||
540 | * Default destructor |
||
541 | * |
||
542 | */ |
||
543 | public function __destruct(){ |
||
551 | } |
||
552 |
This check looks for calls to
isset(...)
orempty()
on variables that are yet undefined. These calls will always produce the same result and can be removed.This is most likely caused by the renaming of a variable or the removal of a function/method parameter.