mymodule3_search()   F
last analyzed

Complexity

Conditions 21
Paths 9216

Size

Total Lines 109
Code Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 21
eloc 82
nc 9216
nop 5
dl 0
loc 109
rs 0
c 1
b 1
f 1

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 * My Module 3 module for xoops
14
 *
15
 * @copyright     2020 XOOPS Project (https://xooops.org)
16
 * @license        GPL 2.0 or later
17
 * @package        mymodule3
18
 * @since          1.0
19
 * @min_xoops      2.5.9
20
 * @author         TDM XOOPS - Email:<[email protected]> - Website:<http://xoops.org>
21
 */
22
23
use XoopsModules\Mymodule3;
24
25
26
/**
27
 * search callback functions 
28
 *
29
 * @param $queryarray 
30
 * @param $andor 
31
 * @param $limit 
32
 * @param $offset 
33
 * @param $userid 
34
 * @return mixed $itemIds
35
 */
36
function mymodule3_search($queryarray, $andor, $limit, $offset, $userid)
37
{
38
	$ret = [];
39
	$helper = \XoopsModules\Mymodule3\Helper::getInstance();
40
41
	// search in table articles
42
	// search keywords
43
	$elementCount = 0;
44
	$articlesHandler = $helper->getHandler('articles');
45
	if (is_array($queryarray)) {
46
		$elementCount = count($queryarray);
47
	}
48
	if ($elementCount > 0) {
49
		$criteriaKeywords = new \CriteriaCompo();
50
		for($i = 0; $i  <  $elementCount; $i++) {
51
			$criteriaKeyword = new \CriteriaCompo();
52
			$criteriaKeyword->add( new \Criteria( 'art_cat', '%' . $queryarray[$i] . '%', 'LIKE' ), 'OR' );
53
			$criteriaKeyword->add( new \Criteria( 'art_title', '%' . $queryarray[$i] . '%', 'LIKE' ), 'OR' );
54
			$criteriaKeyword->add( new \Criteria( 'art_descr', '%' . $queryarray[$i] . '%', 'LIKE' ), 'OR' );
55
			$criteriaKeywords->add( $criteriaKeyword, $andor );
56
			unset($criteriaKeyword);
57
		}
58
	}
59
	// search user(s)
60
	if ($userid && is_array($userid)) {
61
		$userid = array_map('intval', $userid);
62
		$criteriaUser = new \CriteriaCompo();
63
		$criteriaUser->add( new \Criteria( 'art_submitter', '(' . implode(',', $userid) . ')', 'IN' ), 'OR' );
64
	} elseif (is_numeric($userid) && $userid > 0) {
65
		$criteriaUser = new \CriteriaCompo();
66
		$criteriaUser->add( new \Criteria( 'art_submitter', $userid ), 'OR' );
67
	}
68
	$criteriaSearch = new \CriteriaCompo();
69
	if (isset($criteriaKeywords)) {
70
		$criteriaSearch->add( $criteriaKeywords, 'AND' );
71
	}
72
	if (isset($criteriaUser)) {
73
		$criteriaSearch->add( $criteriaUser, 'AND' );
74
	}
75
	$criteriaSearch->setStart( $offset );
76
	$criteriaSearch->setLimit( $limit );
77
	$criteriaSearch->setSort( 'art_created' );
78
	$criteriaSearch->setOrder( 'DESC' );
79
	$articlesAll = $articlesHandler->getAll($criteriaSearch);
0 ignored issues
show
Bug introduced by
The method getAll() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoUserHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
	/** @scrutinizer ignore-call */ 
80
 $articlesAll = $articlesHandler->getAll($criteriaSearch);
Loading history...
80
	foreach(array_keys($articlesAll) as $i) {
81
		$ret[] = [
82
			'image'  => 'assets/icons/16/articles.png',
83
			'link'   => 'articles.php?op=show&amp;art_id=' . $articlesAll[$i]->getVar('art_id'),
84
			'title'  => $articlesAll[$i]->getVar('art_title'),
85
			'time'   => $articlesAll[$i]->getVar('art_created')
86
		];
87
	}
88
	unset($criteriaKeywords);
89
	unset($criteriaKeyword);
90
	unset($criteriaUser);
91
	unset($criteriaSearch);
92
93
	// search in table testfields
94
	// search keywords
95
	$elementCount = 0;
96
	$testfieldsHandler = $helper->getHandler('testfields');
97
	if (is_array($queryarray)) {
98
		$elementCount = count($queryarray);
99
	}
100
	if ($elementCount > 0) {
101
		$criteriaKeywords = new \CriteriaCompo();
102
		for($i = 0; $i  <  $elementCount; $i++) {
103
			$criteriaKeyword = new \CriteriaCompo();
104
			$criteriaKeyword->add( new \Criteria( 'tf_text', '%' . $queryarray[$i] . '%', 'LIKE' ), 'OR' );
105
			$criteriaKeyword->add( new \Criteria( 'tf_textarea', '%' . $queryarray[$i] . '%', 'LIKE' ), 'OR' );
106
			$criteriaKeywords->add( $criteriaKeyword, $andor );
107
			unset($criteriaKeyword);
108
		}
109
	}
110
	// search user(s)
111
	if ($userid && is_array($userid)) {
112
		$userid = array_map('intval', $userid);
113
		$criteriaUser = new \CriteriaCompo();
114
		$criteriaUser->add( new \Criteria( 'tf_submitter', '(' . implode(',', $userid) . ')', 'IN' ), 'OR' );
115
	} elseif (is_numeric($userid) && $userid > 0) {
116
		$criteriaUser = new \CriteriaCompo();
117
		$criteriaUser->add( new \Criteria( 'tf_submitter', $userid ), 'OR' );
118
	}
119
	$criteriaSearch = new \CriteriaCompo();
120
	if (isset($criteriaKeywords)) {
121
		$criteriaSearch->add( $criteriaKeywords, 'AND' );
122
	}
123
	if (isset($criteriaUser)) {
124
		$criteriaSearch->add( $criteriaUser, 'AND' );
125
	}
126
	$criteriaSearch->setStart( $offset );
127
	$criteriaSearch->setLimit( $limit );
128
	$criteriaSearch->setSort( 'tf_datetime' );
129
	$criteriaSearch->setOrder( 'DESC' );
130
	$testfieldsAll = $testfieldsHandler->getAll($criteriaSearch);
131
	foreach(array_keys($testfieldsAll) as $i) {
132
		$ret[] = [
133
			'image'  => 'assets/icons/16/testfields.png',
134
			'link'   => 'testfields.php?op=show&amp;tf_id=' . $testfieldsAll[$i]->getVar('tf_id'),
135
			'title'  => $testfieldsAll[$i]->getVar('tf_text'),
136
			'time'   => $testfieldsAll[$i]->getVar('tf_datetime')
137
		];
138
	}
139
	unset($criteriaKeywords);
140
	unset($criteriaKeyword);
141
	unset($criteriaUser);
142
	unset($criteriaSearch);
143
144
	return $ret;
145
146
}
147