Conditions | 11 |
Paths | 216 |
Total Lines | 60 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
37 | function b_xoopsfaq_random_show($options) |
||
38 | { |
||
39 | $moduleDirName = basename(dirname(__DIR__)); |
||
40 | $myts = \MyTextSanitizer::getInstance(); |
||
41 | |||
42 | /** @var Xoopsfaq\CategoryHandler $categoryHandler */ /** @var Xoopsfaq\ContentsHandler $contentsHandler */ |
||
43 | /** @var Xoopsfaq\Helper $helper */ |
||
44 | $helper = \XoopsModules\Xoopsfaq\Helper::getInstance(); |
||
45 | $permHelper = new \Xmf\Module\Helper\Permission($moduleDirName); |
||
46 | $contentsHandler = $helper->getHandler('Contents'); |
||
47 | $block = []; |
||
48 | |||
49 | $criteria = new \CriteriaCompo(); |
||
50 | $criteria->add(new \Criteria('contents_active', Xoopsfaq\Constants::ACTIVE, '=')); |
||
51 | |||
52 | // Filter out cats based on group permissions |
||
53 | $options[1] = isset($options[1]) ? $options[1] : [0]; |
||
54 | $cTu = $catsToUse = (false === strpos($options[1], ',')) ? (array)$options[1] : explode(',', $options[1]); |
||
55 | if (in_array(0, $catsToUse) || empty($catsToUse)) { |
||
56 | // Get a list of all cats |
||
57 | $categoryHandler = $helper->getHandler('Category'); |
||
58 | $catListArray = $categoryHandler->getList(); |
||
59 | $cTu = $catsToUse = array_keys($catListArray); |
||
60 | } |
||
61 | // Remove any cats this user doesn't have rights to view |
||
62 | foreach ($cTu as $key => $thisCat) { |
||
63 | if (false === $permHelper->checkPermission('viewcat', $thisCat)) { |
||
64 | unset($catsToUse[$key]); |
||
65 | } |
||
66 | } |
||
67 | if (!empty($catsToUse)) { |
||
68 | $criteria->add(new \Criteria('contents_cid', '(' . implode(',', $catsToUse) . ')', 'IN')); |
||
69 | } else { |
||
70 | return $block; |
||
71 | } |
||
72 | $xpFaqObjArray = $contentsHandler->getAll($criteria); |
||
73 | $faqCount = (is_array($xpFaqObjArray) && !empty($xpFaqObjArray)) ? count($xpFaqObjArray) : 0; |
||
74 | |||
75 | if ($faqCount > 0) { |
||
76 | $faqNum = array_rand($xpFaqObjArray, 1); |
||
77 | $xpFaqObj = $xpFaqObjArray[$faqNum]; |
||
78 | $faq = $myts->displayTarea($xpFaqObj->getVar('contents_title')); |
||
79 | $txtAns = strip_tags($xpFaqObj->getVar('contents_contents')); // get rid of html for block |
||
80 | $faqAns = $myts->displayTarea(xoops_substr($txtAns, 0, $options[0]), 0, 0, 0, 0, 0); |
||
81 | |||
82 | $categoryHandler = $helper->getHandler('Category'); |
||
83 | $catObj = $categoryHandler->get($xpFaqObj->getVar('contents_cid')); |
||
84 | $cid = $catObj->getVar('category_id'); |
||
85 | $block = [ |
||
86 | 'title' => _MB_XOOPSFAQ_RANDOM_TITLE, |
||
87 | 'faq' => $faq, |
||
88 | 'faqans' => $faqAns, |
||
89 | 'morelink' => $helper->url('index.php?cat_id=' . $cid . '#q' . $xpFaqObj->getVar('contents_id')), |
||
90 | 'linktxt' => _MB_XOOPSFAQ_SEE_MORE, |
||
91 | 'catlink' => $helper->url('index.php?cat_id=' . $cid), |
||
92 | 'cattxt' => $catObj->getVar('category_title'), |
||
93 | ]; |
||
94 | unset($xpFaqObj, $catObj); |
||
95 | } |
||
96 | return $block; |
||
97 | } |
||
128 |