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