Conditions | 19 |
Paths | 7690 |
Total Lines | 111 |
Code Lines | 64 |
Lines | 0 |
Ratio | 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 |
||
22 | function menus_block_show($options) |
||
23 | { |
||
24 | $block = array(); |
||
25 | $xoops = Xoops::getInstance(); |
||
26 | $helper = Xoops::getModuleHelper('menus'); |
||
27 | |||
28 | /* @var $decorator MenusDecoratorInterface */ |
||
29 | $decorators = MenusDecorator::getAvailableDecorators(); |
||
30 | |||
31 | foreach ($decorators as $decorator) { |
||
32 | $decorator->start(); |
||
33 | } |
||
34 | |||
35 | $menu_id = $options[0]; |
||
36 | $criteria = new CriteriaCompo(new Criteria('mid', $menu_id)); |
||
37 | $criteria->setSort('weight'); |
||
38 | $criteria->setOrder('ASC'); |
||
39 | //get menus as an array with ids as keys |
||
40 | $menus = $helper->getHandlerMenu()->getAll($criteria, null, false, false); |
||
41 | unset($criteria); |
||
42 | |||
43 | foreach ($menus as $key => $menu) { |
||
44 | $hasAccess = true; |
||
45 | foreach ($decorators as $decorator) { |
||
46 | $decorator->hasAccess($menu, $hasAccess); |
||
47 | } |
||
48 | if (!$hasAccess) { |
||
49 | unset($menus[$key]); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | $count = count($menus); |
||
54 | if ($count == 0) { |
||
55 | return $block; |
||
56 | } |
||
57 | |||
58 | foreach ($menus as $key => $menu) { |
||
59 | foreach ($decorators as $decorator) { |
||
60 | $decorator->decorateMenu($menu); |
||
61 | } |
||
62 | $menus[$key] = $menu; |
||
63 | } |
||
64 | |||
65 | foreach ($decorators as $decorator) { |
||
66 | $decorator->end($menus); |
||
67 | } |
||
68 | |||
69 | $builder = new MenusBuilder($menus); |
||
70 | $block = $builder->render(); |
||
71 | |||
72 | /*--------------------------------------------------------------*/ |
||
73 | //default files to load |
||
74 | $css = array(); |
||
75 | $js = array(); |
||
76 | |||
77 | //get extra files from skins |
||
78 | $skin = $options[1]; |
||
79 | $skin_info = $helper->getSkinInfo($skin, $options[2]); |
||
80 | |||
81 | if (isset($skin_info['css'])) { |
||
82 | $css = array_merge($css, $skin_info['css']); |
||
83 | } |
||
84 | |||
85 | if (isset($skin_info['js'])) { |
||
86 | $js = array_merge($js, $skin_info['js']); |
||
87 | } |
||
88 | |||
89 | if ($helper->getConfig('assign_method') === 'xoopstpl') { |
||
90 | $tpl_vars = ''; |
||
91 | foreach ($css as $file) { |
||
92 | $tpl_vars .= "\n" . '<link rel="stylesheet" type="text/css" media="all" href="' . $file . '" />'; |
||
93 | } |
||
94 | |||
95 | foreach ($js as $file) { |
||
96 | $tpl_vars .= "\n" . '<script type="text/javascript" src="' . $file . '"></script>'; |
||
97 | } |
||
98 | |||
99 | if (isset($skin_info['header'])) { |
||
100 | $tpl_vars .= "\n" . $skin_info['header']; |
||
101 | } |
||
102 | |||
103 | $xoops->tpl()->assign('xoops_module_header', $tpl_vars . @$xoops->tpl()->getTemplateVars("xoops_module_header")); |
||
104 | } else { |
||
105 | foreach ($css as $file) { |
||
106 | $xoops->theme()->addStylesheet($file); |
||
107 | } |
||
108 | |||
109 | foreach ($js as $file) { |
||
110 | $xoops->theme()->addScript($file); |
||
111 | } |
||
112 | |||
113 | if (isset($skin_info['header'])) { |
||
114 | $xoops->tpl()->assign('xoops_footer', @$xoops->tpl()->getTemplateVars("xoops_footer") . "\n" . $skin_info['header']); |
||
115 | } |
||
116 | } |
||
117 | |||
118 | $blockTpl = new XoopsTpl(); |
||
119 | $blockTpl->assign('block', $block); |
||
120 | $blockTpl->assign('config', $skin_info['config']); |
||
121 | $blockTpl->assign('skinurl', $skin_info['url']); |
||
122 | $blockTpl->assign('skinpath', $skin_info['path']); |
||
123 | |||
124 | $block['content'] = $blockTpl->fetch($skin_info['template']); |
||
125 | |||
126 | if ($options[3] === 'template') { |
||
127 | $xoops->tpl()->assign('xoops_menu_' . $options[4], $block['content']); |
||
128 | $block = array(); |
||
129 | } |
||
130 | |||
131 | return $block; |
||
132 | } |
||
133 | |||
262 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: