Conditions | 12 |
Paths | 37 |
Total Lines | 114 |
Code Lines | 85 |
Lines | 35 |
Ratio | 30.7 % |
Changes | 3 | ||
Bugs | 0 | Features | 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 |
||
17 | function b_tdmdownloads_search_show() |
||
18 | { |
||
19 | require_once XOOPS_ROOT_PATH."/modules/TDMDownloads/include/functions.php"; |
||
20 | include_once XOOPS_ROOT_PATH . "/class/xoopsformloader.php"; |
||
21 | include_once XOOPS_ROOT_PATH."/class/tree.php"; |
||
22 | //appel des class |
||
23 | $downloadscat_Handler =& xoops_getModuleHandler('tdmdownloads_cat', 'TDMDownloads'); |
||
24 | $downloads_Handler =& xoops_getModuleHandler('tdmdownloads_downloads', 'TDMDownloads'); |
||
25 | $downloadsfield_Handler =& xoops_getModuleHandler('tdmdownloads_field', 'TDMDownloads'); |
||
26 | $downloadsfielddata_Handler =& xoops_getModuleHandler('tdmdownloads_fielddata', 'TDMDownloads'); |
||
27 | //appel des fichiers de langues |
||
28 | xoops_loadLanguage('main', 'TDMDownloads'); |
||
29 | xoops_loadLanguage('admin', 'TDMDownloads'); |
||
30 | |||
31 | $categories = TDMDownloads_MygetItemIds('tdmdownloads_view', 'TDMDownloads'); |
||
32 | |||
33 | $block = array(); |
||
34 | |||
35 | //formulaire de recherche |
||
36 | $form = new XoopsThemeForm(_MD_TDMDOWNLOADS_SEARCH, "search", XOOPS_URL . '/modules/TDMDownloads/search.php', 'post'); |
||
37 | $form->setExtra('enctype="multipart/form-data"'); |
||
38 | //recherche par titre |
||
39 | $form->addElement(new XoopsFormText(_MD_TDMDOWNLOADS_SEARCH_TITLE, 'title', 25, 255, '')); |
||
40 | //recherche par cat�gorie |
||
41 | $criteria = new CriteriaCompo(); |
||
42 | $criteria->setSort('cat_weight ASC, cat_title'); |
||
43 | $criteria->setOrder('ASC'); |
||
44 | $criteria->add(new Criteria('cat_cid', '(' . implode(',', $categories) . ')','IN')); |
||
45 | $downloadscat_arr = $downloadscat_Handler->getall($criteria); |
||
46 | $mytree = new XoopsObjectTree($downloadscat_arr, 'cat_cid', 'cat_pid'); |
||
47 | $form->addElement(new XoopsFormLabel(_AM_TDMDOWNLOADS_FORMINCAT, $mytree->makeSelBox('cat', 'cat_title','--','', true))); |
||
48 | //recherche champ sup. |
||
49 | $downloadsfield_Handler =& xoops_getModuleHandler('tdmdownloads_field', 'TDMDownloads'); |
||
50 | $criteria = new CriteriaCompo(); |
||
51 | $criteria->add(new Criteria('search', 1)); |
||
52 | $criteria->add(new Criteria('status', 1)); |
||
53 | $criteria->setSort('weight ASC, title'); |
||
54 | $criteria->setOrder('ASC'); |
||
55 | $downloads_field = $downloadsfield_Handler->getall($criteria); |
||
56 | foreach (array_keys($downloads_field) as $i) { |
||
57 | $title_sup = ''; |
||
58 | $contenu_arr = array(); |
||
59 | $lid_arr = array(); |
||
60 | $nom_champ = 'champ' . $downloads_field[$i]->getVar('fid'); |
||
61 | $criteria = new CriteriaCompo(); |
||
62 | $champ_contenu[$downloads_field[$i]->getVar('fid')] = 999; |
||
|
|||
63 | if ($downloads_field[$i]->getVar('status_def') == 1) { |
||
64 | $criteria->add(new Criteria('status', 0, '!=')); |
||
65 | if ($downloads_field[$i]->getVar('fid') == 1) { |
||
66 | //page d'accueil |
||
67 | $title_sup = _AM_TDMDOWNLOADS_FORMHOMEPAGE; |
||
68 | $criteria->setSort('homepage'); |
||
69 | $nom_champ_base = 'homepage'; |
||
70 | } |
||
71 | View Code Duplication | if ($downloads_field[$i]->getVar('fid') == 2) { |
|
72 | //version |
||
73 | $title_sup = _AM_TDMDOWNLOADS_FORMVERSION; |
||
74 | $criteria->setSort('version'); |
||
75 | $nom_champ_base = 'version'; |
||
76 | } |
||
77 | View Code Duplication | if ($downloads_field[$i]->getVar('fid') == 3) { |
|
78 | //taille du fichier |
||
79 | $title_sup = _AM_TDMDOWNLOADS_FORMSIZE; |
||
80 | $criteria->setSort('size'); |
||
81 | $nom_champ_base = 'size'; |
||
82 | } |
||
83 | if ($downloads_field[$i]->getVar('fid') == 4) { |
||
84 | //platform |
||
85 | $title_sup = _AM_TDMDOWNLOADS_FORMPLATFORM; |
||
86 | $platform_array = explode('|',xoops_getModuleOption('platform', 'TDMDownloads')); |
||
87 | foreach ($platform_array as $platform) { |
||
88 | $contenu_arr[$platform] = $platform; |
||
89 | } |
||
90 | } else { |
||
91 | $criteria->setOrder('ASC'); |
||
92 | $tdmdownloads_arr = $downloads_Handler->getall( $criteria ); |
||
93 | View Code Duplication | foreach (array_keys($tdmdownloads_arr) as $j) { |
|
94 | $contenu_arr[$tdmdownloads_arr[$j]->getVar($nom_champ_base)] = $tdmdownloads_arr[$j]->getVar($nom_champ_base); |
||
95 | } |
||
96 | } |
||
97 | View Code Duplication | } else { |
|
98 | $title_sup = $downloads_field[$i]->getVar('title'); |
||
99 | $criteria->add(new Criteria('fid', $downloads_field[$i]->getVar('fid'))); |
||
100 | $criteria->setSort('data'); |
||
101 | $criteria->setOrder('ASC'); |
||
102 | $tdmdownloads_arr = $downloadsfielddata_Handler->getall( $criteria ); |
||
103 | foreach (array_keys($tdmdownloads_arr) as $j) { |
||
104 | $contenu_arr[$tdmdownloads_arr[$j]->getVar('data', 'n')] = $tdmdownloads_arr[$j]->getVar('data'); |
||
105 | } |
||
106 | if ($champ_contenu[$downloads_field[$i]->getVar('fid')] != '') { |
||
107 | $criteria_1 = new CriteriaCompo(); |
||
108 | $criteria_1->add(new Criteria('data', $champ_contenu[$downloads_field[$i]->getVar('fid')])); |
||
109 | $data_arr = $downloadsfielddata_Handler->getall($criteria_1); |
||
110 | foreach (array_keys($data_arr) as $k) { |
||
111 | $lid_arr[] = $data_arr[$k]->getVar('lid'); |
||
112 | } |
||
113 | |||
114 | } |
||
115 | $form->addElement($select_sup); |
||
116 | } |
||
117 | $select_sup = new XoopsFormSelect($title_sup, $nom_champ, $champ_contenu[$downloads_field[$i]->getVar('fid')]); |
||
118 | $select_sup->addOption(999,_MD_TDMDOWNLOADS_SEARCH_ALL1); |
||
119 | $select_sup->addOptionArray($contenu_arr); |
||
120 | $form->addElement($select_sup); |
||
121 | unset ($select_sup); |
||
122 | } |
||
123 | //bouton validation |
||
124 | $button_tray = new XoopsFormElementTray('' ,''); |
||
125 | $button_tray->addElement(new XoopsFormButton('', 'submit', _MD_TDMDOWNLOADS_SEARCH_BT, 'submit')); |
||
126 | $form->addElement($button_tray); |
||
127 | $block['form'] = $form->render(); |
||
128 | |||
129 | return $block; |
||
130 | } |
||
131 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.