| Conditions | 12 |
| Paths | 36 |
| Total Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 37 | public function build_hooks() { |
||
| 38 | $ci = \CI::$APP; |
||
| 39 | |||
| 40 | $ci->load->library('lib_xml'); |
||
| 41 | |||
| 42 | $xml = '<?xml version="1.0" encoding="UTF-8"?><hooks>'; |
||
| 43 | |||
| 44 | // Get all installed modules |
||
| 45 | $ci->db->select('name'); |
||
| 46 | |||
| 47 | $modules = $ci->db->get('components'); |
||
| 48 | |||
| 49 | if ($modules) { |
||
| 50 | $modules = $modules->result_array(); |
||
| 51 | } else { |
||
| 52 | show_error($ci->db->_error_message()); |
||
| 53 | } |
||
| 54 | |||
| 55 | $modules[]['name'] = 'core'; |
||
| 56 | |||
| 57 | // Search for hooks.xml in all installed modules |
||
| 58 | foreach ($modules as $m) { |
||
| 59 | $xml_file = getModulePath($m['name']) . '/hooks.xml'; |
||
| 60 | if (file_exists($xml_file)) { |
||
| 61 | $xml .= file_get_contents($xml_file); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | $xml .= "\n</hooks>"; |
||
| 66 | |||
| 67 | $parser = xml_parser_create(); |
||
| 68 | xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); |
||
| 69 | xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0); |
||
| 70 | xml_parse_into_struct($parser, $xml, $vals); |
||
| 71 | xml_parser_free($parser); |
||
| 72 | |||
| 73 | $tmp = []; |
||
| 74 | |||
| 75 | foreach ($vals as $k => $v) { |
||
| 76 | if (isset($v['type']) && isset($v['value']) && isset($v['attributes'])) { |
||
| 77 | $hookId = trim($v['attributes']['id']); |
||
| 78 | |||
| 79 | if (empty($tmp[$hookId])) { |
||
| 80 | $tmp[$hookId] = ''; |
||
| 81 | } |
||
| 82 | |||
| 83 | $hookValue = trim($v['value']); |
||
| 84 | if ($v['type'] === 'complete' && !empty($hookValue) && !empty($hookId)) { |
||
| 85 | $tmp[$hookId] .= $hookValue; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | $this->create_hooks_file($tmp); |
||
| 91 | } |
||
| 92 | |||
| 131 | } |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.