Conditions | 15 |
Paths | 288 |
Total Lines | 86 |
Code Lines | 50 |
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 namespace Xoopsmodules\xsitemap; |
||
59 | public static function generateSitemap() |
||
60 | { |
||
61 | $block = []; |
||
62 | $moduleDirName = basename(dirname(__DIR__)); |
||
63 | /** @internal can't use Helper since function called during install |
||
64 | * $helper = \Xmf\Module\Helper::getHelper($moduleDirName); |
||
65 | * $pluginHandler = $helper->getHandler('plugin', $moduleDirName); |
||
66 | */ |
||
67 | xoops_load('plugin', $moduleDirName); |
||
68 | |||
69 | // Get list of modules admin wants to hide from xsitemap |
||
70 | $invisibleDirnames = empty($GLOBALS['xoopsModuleConfig']['invisible_dirnames']) ? ['xsitemap'] : explode(',', $GLOBALS['xoopsModuleConfig']['invisible_dirnames'] . ',xsitemap'); |
||
71 | $invisibleDirnames = array_map('trim', $invisibleDirnames); |
||
72 | $invisibleDirnames = array_map('mb_strtolower', $invisibleDirnames); |
||
73 | |||
74 | // Get the mid for any of these modules if they're active and hasmain (visible frontside) |
||
75 | /** @var \XoopsModuleHandler $moduleHandler */ |
||
76 | $moduleHandler = xoops_getHandler('module'); |
||
77 | $invisibleMidArray = []; |
||
78 | foreach ($invisibleDirnames as $hiddenDir) { |
||
79 | $criteria = new \CriteriaCompo(new \Criteria('hasmain', 1)); |
||
80 | $criteria->add(new \Criteria('isactive', 1)); |
||
81 | $criteria->add(new \Criteria('name', $hiddenDir)); |
||
82 | $modObj = $moduleHandler->getByDirname($hiddenDir); |
||
83 | if (false !== $modObj && $modObj instanceof \XoopsModule) { |
||
84 | $invisibleMidArray[] = $modObj->mid(); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | // Where user has permissions |
||
89 | /** @var \XoopsGroupPermHandler $modulepermHandler */ |
||
90 | $modulepermHandler = xoops_getHandler('groupperm'); |
||
91 | $groups = ($GLOBALS['xoopsUser'] instanceof \XoopsUser) ? $GLOBALS['xoopsUser']->getGroups() : XOOPS_GROUP_ANONYMOUS; |
||
92 | $readAllowed = $modulepermHandler->getItemIds('module_read', $groups); |
||
93 | $filteredMids = array_diff($readAllowed, $invisibleMidArray); |
||
94 | /** @var \XsitemapPluginHandler $pluginHandler */ |
||
95 | $pluginHandler = xoops_getModuleHandler('plugin', $moduleDirName); |
||
96 | $criteria = new \CriteriaCompo(new \Criteria('hasmain', 1)); |
||
97 | $criteria->add(new \Criteria('isactive', 1)); |
||
98 | if (count($filteredMids) > 0) { |
||
99 | $criteria->add(new \Criteria('mid', '(' . implode(',', $filteredMids) . ')', 'IN')); |
||
100 | } |
||
101 | |||
102 | /** @var array $modules */ |
||
103 | $modules = $moduleHandler->getObjects($criteria, true); |
||
104 | |||
105 | $criteria = new \CriteriaCompo(); |
||
106 | $criteria->setSort('plugin_id'); |
||
107 | $criteria->order = 'ASC'; |
||
108 | $pluginObjArray = $pluginHandler->getAll($criteria); |
||
109 | |||
110 | /** @var array $sublinks */ |
||
111 | foreach ($modules as $mid => $modObj) { |
||
112 | $sublinks = $modObj->subLink(); |
||
113 | $modDirName = $modObj->getVar('dirname', 'n'); |
||
114 | $block['modules'][$mid] = [ |
||
115 | 'id' => $mid, |
||
116 | 'name' => $modObj->getVar('name'), |
||
117 | 'directory' => $modDirName, |
||
118 | 'sublinks' => [] |
||
119 | // init the sublinks array |
||
120 | ]; |
||
121 | // Now 'patch' the sublink to include module path |
||
122 | if (count($sublinks) > 0) { |
||
123 | foreach ($sublinks as $sublink) { |
||
124 | $block['modules'][$mid]['sublinks'][] = [ |
||
125 | 'name' => $sublink['name'], |
||
126 | 'url' => $GLOBALS['xoops']->url("www/modules/{$modDirName}/{$sublink['url']}") |
||
127 | ]; |
||
128 | } |
||
129 | } |
||
130 | |||
131 | /** @var array $pluginObjArray */ |
||
132 | foreach ($pluginObjArray as $pObj) { |
||
133 | if ((0 == $pObj->getVar('topic_pid')) && in_array($pObj->getVar('plugin_mod_table'), (array)$modObj->getInfo('tables'))) { |
||
134 | $objVars = $pObj->getValues(); |
||
135 | if (1 == $objVars['plugin_online']) { |
||
136 | $tmpMap = self::getSitemap($objVars['plugin_mod_table'], $objVars['plugin_cat_id'], $objVars['plugin_cat_pid'], $objVars['plugin_cat_name'], $objVars['plugin_call'], $objVars['plugin_weight']); |
||
137 | $block['modules'][$mid]['parent'] = isset($tmpMap['parent']) ? $tmpMap['parent'] : null; |
||
138 | } |
||
139 | } |
||
140 | } |
||
141 | } |
||
142 | |||
143 | return $block; |
||
144 | } |
||
145 | |||
259 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.