Conditions | 24 |
Paths | > 20000 |
Total Lines | 162 |
Code Lines | 95 |
Lines | 62 |
Ratio | 38.27 % |
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 |
||
34 | function virt() |
||
35 | { |
||
36 | global $xoopsTpl; |
||
37 | |||
38 | //get module configuration |
||
39 | $moduleHandler = xoops_getHandler('module'); |
||
40 | $module = $moduleHandler->getByDirname('pedigree'); |
||
41 | $configHandler = xoops_getHandler('config'); |
||
42 | $moduleConfig = $configHandler->getConfigsByCat(0, $module->getVar('mid')); |
||
43 | |||
44 | // if (isset($_GET['st'])) { |
||
45 | // $st = $_GET['st']; |
||
46 | // } else { |
||
47 | // $st = 0; |
||
48 | // } |
||
49 | // if (isset($_GET['l'])) { |
||
50 | // $l = $_GET['l']; |
||
51 | // } else { |
||
52 | // $l = 'A'; |
||
53 | // } |
||
54 | $st = XoopsRequest::getInt('st', 0, 'get'); |
||
55 | $l = XoopsRequest::getString('l', 'a', 'get'); |
||
56 | |||
57 | $xoopsTpl->assign('sire', '1'); |
||
58 | //create list of males dog to select from |
||
59 | $perp = $moduleConfig['perpage']; |
||
60 | //count total number of dogs |
||
61 | $numdog = 'SELECT count(d.id) FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' d LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' m ON m.id = d.mother LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') // . " f ON f.id = d.father WHERE d.roft = '0' and d.mother != '0' and d.father != '0' and m.mother != '0' and m.father != '0' and f.mother != '0' and f.father != '0' and d.naam LIKE '" . $l . "%'"; |
||
62 | . " f ON f.id = d.father WHERE d.roft = '0' and d.mother != '0' and d.father != '0' and m.mother != '0' and m.father != '0' and f.mother != '0' and f.father != '0' and d.naam LIKE '" . $GLOBALS['xoopsDB']->escape($l) . "%'"; |
||
63 | $numres = $GLOBALS['xoopsDB']->query($numdog); |
||
64 | //total number of dogs the query will find |
||
65 | list($numresults) = $GLOBALS['xoopsDB']->fetchRow($numres); |
||
66 | //total number of pages |
||
67 | $numpages = floor($numresults / $perp) + 1; |
||
68 | if (($numpages * $perp) == ($numresults + $perp)) { |
||
69 | --$numpages; |
||
70 | } |
||
71 | //find current page |
||
72 | $cpage = floor($st / $perp) + 1; |
||
73 | //create alphabet |
||
74 | $pages = ''; |
||
75 | for ($i = 65; $i <= 90; ++$i) { |
||
76 | if ($l == chr($i)) { |
||
77 | $pages .= "<b><a href=\"virtual.php?r=1&st=0&l=" . chr($i) . "\">" . chr($i) . '</a></b> '; |
||
78 | View Code Duplication | } else { |
|
79 | $pages .= "<a href=\"virtual.php?r=1&st=0&l=" . chr($i) . "\">" . chr($i) . '</a> '; |
||
80 | } |
||
81 | } |
||
82 | $pages .= '- '; |
||
83 | $pages .= "<a href=\"virtual.php?r=1&st=0&l=Ã…\">Ã…</a> "; |
||
84 | $pages .= "<a href=\"virtual.php?r=1&st=0&l=Ö\">Ö</a> "; |
||
85 | $pages .= '<br />'; |
||
86 | //create previous button |
||
87 | if ($numpages > 1) { |
||
88 | if ($cpage > 1) { |
||
89 | $pages .= "<a href=\"virtual.php?r=1&&l=" . $l . 'st=' . ($st - $perp) . "\">" . _MA_PEDIGREE_PREVIOUS . '</a>  '; |
||
90 | } |
||
91 | } |
||
92 | //create numbers |
||
93 | for ($x = 1; $x < ($numpages + 1); ++$x) { |
||
94 | //create line break after 20 number |
||
95 | if (($x % 20) == 0) { |
||
96 | $pages .= '<br />'; |
||
97 | } |
||
98 | if ($x != $cpage) { |
||
99 | $pages .= "<a href=\"virtual.php?r=1&l=" . $l . '&st=' . ($perp * ($x - 1)) . "\">" . $x . '</a> '; |
||
100 | } else { |
||
101 | $pages .= $x . '  '; |
||
102 | } |
||
103 | } |
||
104 | //create next button |
||
105 | if ($numpages > 1) { |
||
106 | if ($cpage < $numpages) { |
||
107 | $pages .= "<a href=\"virtual.php?r=1&l=" . $l . '&st=' . ($st + $perp) . "\">" . _MA_PEDIGREE_NEXT . '</a>  '; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | //query |
||
112 | $queryString = 'SELECT d.*, d.id AS d_id, d.naam AS d_naam FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' d LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' m ON m.id = d.mother LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " f ON f.id = d.father WHERE d.roft = '0' and d.mother != '0' and d.father != '0' and m.mother != '0' and m.father != '0' and f.mother != '0' and f.father != '0' and d.naam LIKE '" . $l . "%' ORDER BY d.naam LIMIT " . $st . ', ' . $perp; |
||
113 | $result = $GLOBALS['xoopsDB']->query($queryString); |
||
114 | |||
115 | $animal = new PedigreeAnimal(); |
||
116 | //test to find out how many user fields there are... |
||
117 | $fields = $animal->getNumOfFields(); |
||
118 | $numofcolumns = 1; |
||
119 | $columns[] = array('columnname' => 'Name'); |
||
120 | View Code Duplication | for ($i = 0, $iMax = count($fields); $i < $iMax; ++$i) { |
|
121 | $userField = new Field($fields[$i], $animal->getConfig()); |
||
122 | $fieldType = $userField->getSetting('FieldType'); |
||
123 | $fieldObject = new $fieldType($userField, $animal); |
||
124 | //create empty string |
||
125 | $lookupvalues = ''; |
||
126 | if ($userField->isActive() && $userField->inList()) { |
||
127 | if ($userField->hasLookup()) { |
||
128 | $lookupvalues = $userField->lookupField($fields[$i]); |
||
129 | //debug information |
||
130 | //print_r($lookupvalues); |
||
131 | } |
||
132 | $columns[] = array('columnname' => $fieldObject->fieldname, 'columnnumber' => $userField->getId(), 'lookupval' => $lookupvalues); |
||
133 | ++$numofcolumns; |
||
134 | unset($lookupvalues); |
||
135 | } |
||
136 | } |
||
137 | |||
138 | View Code Duplication | while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) { |
|
139 | //create picture information |
||
140 | if ($row['foto'] != '') { |
||
141 | $camera = " <img src=\"assets/images/dog-icon25.png\">"; |
||
142 | } else { |
||
143 | $camera = ''; |
||
144 | } |
||
145 | $name = stripslashes($row['d_naam']) . $camera; |
||
146 | //empty array |
||
147 | unset($columnvalue); |
||
148 | //fill array |
||
149 | for ($i = 1; $i < $numofcolumns; ++$i) { |
||
150 | $x = $columns[$i]['columnnumber']; |
||
151 | //echo $x."columnnumber"; |
||
152 | if (is_array($columns[$i]['lookupval'])) { |
||
153 | foreach ($columns[$i]['lookupval'] as $key => $keyvalue) { |
||
154 | if ($keyvalue['id'] == $row['user' . $x]) { |
||
155 | //echo "key:".$row['user5']."<br />"; |
||
156 | $value = $keyvalue['value']; |
||
157 | } |
||
158 | } |
||
159 | //debug information |
||
160 | ///echo $columns[$i]['columnname']."is an array !"; |
||
161 | } //format value - cant use object because of query count |
||
162 | elseif (0 === strpos($row['user' . $x], 'http://')) { |
||
163 | $value = "<a href=\"" . $row['user' . $x] . "\">" . $row['user' . $x] . '</a>'; |
||
164 | } else { |
||
165 | $value = $row['user' . $x]; |
||
166 | } |
||
167 | $columnvalue[] = array('value' => $value); |
||
168 | unset($value); |
||
169 | } |
||
170 | $dogs[] = array( |
||
171 | 'id' => $row['d_id'], |
||
172 | 'name' => $name, |
||
173 | 'gender' => '<img src="assets/images/male.gif">', |
||
174 | 'link' => "<a href=\"virtual.php?f=dam&selsire=" . $row['d_id'] . "\">" . $name . '</a>', |
||
175 | 'colour' => '', |
||
176 | 'number' => '', |
||
177 | 'usercolumns' => isset($columnvalue) ? $columnvalue : 0 |
||
178 | ); |
||
179 | } |
||
180 | |||
181 | //add data to smarty template |
||
182 | //assign dog |
||
183 | if (isset($dogs)) { |
||
184 | $xoopsTpl->assign('dogs', $dogs); |
||
185 | } |
||
186 | $xoopsTpl->assign('columns', $columns); |
||
187 | $xoopsTpl->assign('numofcolumns', $numofcolumns); |
||
188 | $xoopsTpl->assign('tsarray', PedigreeUtilities::sortTable($numofcolumns)); |
||
189 | $xoopsTpl->assign('nummatch', strtr(_MA_PEDIGREE_ADD_SELSIRE, array('[father]' => $moduleConfig['father']))); |
||
190 | $xoopsTpl->assign('pages', $pages); |
||
191 | |||
192 | $xoopsTpl->assign('virtualtitle', strtr(_MA_PEDIGREE_VIRUTALTIT, array('[mother]' => $moduleConfig['mother']))); |
||
193 | $xoopsTpl->assign('virtualstory', strtr(_MA_PEDIGREE_VIRUTALSTO, array('[mother]' => $moduleConfig['mother'], '[father]' => $moduleConfig['father'], '[children]' => $moduleConfig['children']))); |
||
194 | $xoopsTpl->assign('nextaction', '<b>' . strtr(_MA_PEDIGREE_VIRT_SIRE, array('[father]' => $moduleConfig['father'])) . '</b>'); |
||
195 | } |
||
196 | |||
428 |
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.