Passed
Pull Request — master (#2)
by Michael
07:08 queued 02:34
created

SonglistArtistsHandler::insert()   F

Complexity

Conditions 23
Paths 328

Size

Total Lines 71
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 23
eloc 47
nc 328
nop 3
dl 0
loc 71
rs 1.7333
c 0
b 0
f 0

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
if (!defined('XOOPS_ROOT_PATH')) {
4
	exit();
5
}
6
7
include_once(dirname(dirname(__FILE__)).'/include/songlist.object.php');
8
include_once(dirname(dirname(__FILE__)).'/include/songlist.form.php');
9
10
class SonglistArtists extends XoopsObject
11
{
12
13
    function SonglistArtists($fid = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
14
    {
15
        $this->initVar('aid', XOBJ_DTYPE_INT, 0, false);
16
        $this->initVar('cids', XOBJ_DTYPE_ARRAY, array(), false);
17
        $this->initVar('sids', XOBJ_DTYPE_ARRAY, array(), false);
18
		$this->initVar('name', XOBJ_DTYPE_TXTBOX, null, false, 128);
19
		$this->initVar('albums', XOBJ_DTYPE_INT, 0, false);
20
		$this->initVar('songs', XOBJ_DTYPE_INT, 0, false);
21
		$this->initVar('hits', XOBJ_DTYPE_INT, 0, false);
22
		$this->initVar('rank', XOBJ_DTYPE_DECIMAL, 0, false);
23
		$this->initVar('votes', XOBJ_DTYPE_INT, 0, false);
24
		$this->initVar('created', XOBJ_DTYPE_INT, 0, false);
25
		$this->initVar('updated', XOBJ_DTYPE_INT, 0, false);
26
	}
27
28
	function getForm($as_array = false) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
29
		return songlist_artists_get_form($this, $as_array);
30
	}
31
	
32
	function toArray($extra = false) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
33
		$ret = parent::toArray();
34
		$form = $this->getForm(true);
35
		foreach($form as $key => $element) {
36
			$ret['form'][$key] = $form[$key]->render();	
37
		}
38
		foreach(array('created', 'updated') as $key) {
39
			if ($this->getVar($key)>0) {
40
				$ret['form'][$key] = date(_DATESTRING, $this->getVar($key)); 
41
				$ret[$key] = date(_DATESTRING, $this->getVar($key));
42
			}
43
		}
44
45
		$ret['rank'] = number_format(($this->getVar('rank')>0&&$this->getVar('votes')>0?$this->getVar('rank')/$this->getVar('votes'):0),2)._MI_SONGLIST_OFTEN;
46
		$ret['url'] = $this->getURL(true);
0 ignored issues
show
Unused Code introduced by
The call to SonglistArtists::getURL() has too many arguments starting with true. ( Ignorable by Annotation )

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

46
		/** @scrutinizer ignore-call */ 
47
  $ret['url'] = $this->getURL(true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
47
		
48
		xoops_loadLanguage('enum', 'songlist');
49
		if (!empty($ret['singer']))
50
			$ret['singer'] = constant($ret['singer']); 
51
		
52
		if ($extra==false)
53
    		return $ret;
54
    		
55
		if (count($this->getVar('cids'))!=0) {
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('cids') can also be of type boolean and null and string; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

55
		if (count(/** @scrutinizer ignore-type */ $this->getVar('cids'))!=0) {
Loading history...
56
    		$categories_handler = xoops_getmodulehandler('category', 'songlist');
57
    		foreach($this->getVar('cids') as $aid) {
58
    			$category = $categories_handler->get($aid);
59
    			if (is_object($category)) {
60
    				$ret['categories_array'][$aid] = $category->toArray(false);
0 ignored issues
show
Unused Code introduced by
The call to XoopsObject::toArray() has too many arguments starting with false. ( Ignorable by Annotation )

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

60
    				/** @scrutinizer ignore-call */ 
61
        $ret['categories_array'][$aid] = $category->toArray(false);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
61
    			}
62
    		} 	
63
    	}
64
    		
65
    	
66
		if (count($this->getVar('aids'))!=0) {
67
    		$artists_handler = xoops_getmodulehandler('artists', 'songlist');
68
    		foreach($this->getVar('aids') as $aid) {
69
    			$artist = $artists_handler->get($aid);
70
    			if (is_object($artist)) {
71
    				$ret['artists_array'][$aid] = $artist->toArray(false);
72
    			}
73
    		} 	
74
    	}
75
    	
76
		
77
		if (count($this->getVar('sids'))!=0) {
78
    		$songs_handler = xoops_getmodulehandler('songs', 'songlist');
79
    		$criteria = new Criteria('`aids`', '%"'.$this->getVar('aid').'"%', 'LIKE');
80
    		$criteria->setSort('`songid`');
81
    		$criteria->setOrder('ASC');
82
    		foreach($songs_handler->getObjects($criteria, true) as $sid => $song) {
0 ignored issues
show
Bug introduced by
The method getObjects() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of said class. However, the method does not exist in XoopsRankHandler or XoUserHandler. Are you sure you never get one of those? ( Ignorable by Annotation )

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

82
    		foreach($songs_handler->/** @scrutinizer ignore-call */ getObjects($criteria, true) as $sid => $song) {
Loading history...
83
    			if (is_object($song)) {
84
    				$ret['songs_array'][$sid] = $song->toArray(false);
85
    			}
86
    		} 	
87
    	}
88
    	
89
		return $ret;
90
	}
91
	
92
    function getURL() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
93
    	global $file, $op, $fct, $id, $value, $gid, $vid, $vcid, $cid, $start, $limit;
94
    	if ($GLOBALS['songlistModuleConfig']['htaccess']) {
95
    	    if ($id!=0) {
96
    			$artist_handler = xoops_getmodulehandler('artists', 'songlist');
97
    			$artist = $artist_handler->get($id);
98
    			if (is_object($artist)&&!$artist->isNew()) {
99
    				return XOOPS_URL.'/'.$GLOBALS['songlistModuleConfig']['baseofurl'].'/artists/'.urlencode(str_replace(array(' ', chr(9)), '-', $artist->getVar('name'))).'/'.$start.'-'.$id.'-'.$op.'-'.$fct.'-'.$gid.'-'.$cid.'/'.urlencode($value).$GLOBALS['songlistModuleConfig']['endofurl'];
100
    			} else {
101
    				return XOOPS_URL.'/'.$GLOBALS['songlistModuleConfig']['baseofurl'].'/artists/'.$start.'-'.$id.'-'.$op.'-'.$fct.'-'.$gid.'-'.$cid.'-'.$vcid.'/'.urlencode($value).$GLOBALS['songlistModuleConfig']['endofurl'];
102
    			}
103
    		} else {
104
    			return XOOPS_URL.'/'.$GLOBALS['songlistModuleConfig']['baseofurl'].'/artists/'.$start.'-'.$id.'-'.$op.'-'.$fct.'-'.$gid.'-'.$cid.'-'.$vcid.'/'.urlencode($value).$GLOBALS['songlistModuleConfig']['endofurl'];
105
    		}
106
    	} else {
107
    		return XOOPS_URL.'/modules/songlist/artists.php?op='.$op.'&fct='.$fct.'&id='.$id.'&value='.urlencode($value).'&gid='.$gid.'&vid='.$vid.'&cid='.$cid.'&start='.$start;
108
    	}
109
    }
110
		
111
}
112
113
114
class SonglistArtistsHandler extends XoopsPersistableObjectHandler
115
{
116
    function __construct(&$db) 
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
117
    {
118
        parent::__construct($db, "songlist_artists", 'SonglistArtists', "aid", "name");
119
    }
120
121
	function filterFields() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
122
		return array('aid', 'cids', 'singer', 'name', 'albums', 'songs', 'hits', 'rank', 'votes', 'created', 'updated');
123
	}
124
	
125
    function getFilterCriteria($filter) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
126
    	$parts = explode('|', $filter);
127
    	$criteria = new CriteriaCompo();
128
    	foreach($parts as $part) {
129
    		$var = explode(',', $part);
130
    		if (!empty($var[1])&&!is_numeric($var[0])) {
131
    			$object = $this->create();
132
    			if (		$object->vars[$var[0]]['data_type']==XOBJ_DTYPE_TXTBOX || 
133
    						$object->vars[$var[0]]['data_type']==XOBJ_DTYPE_TXTAREA) 	{
134
    				$criteria->add(new Criteria('`'.$var[0].'`', '%'.$var[1].'%', (isset($var[2])?$var[2]:'LIKE')));
135
    			} elseif (	$object->vars[$var[0]]['data_type']==XOBJ_DTYPE_INT || 
136
    						$object->vars[$var[0]]['data_type']==XOBJ_DTYPE_DECIMAL || 
137
    						$object->vars[$var[0]]['data_type']==XOBJ_DTYPE_FLOAT ) 	{
138
    				$criteria->add(new Criteria('`'.$var[0].'`', $var[1], (isset($var[2])?$var[2]:'=')));			
139
				} elseif (	$object->vars[$var[0]]['data_type']==XOBJ_DTYPE_ENUM ) 	{
140
    				$criteria->add(new Criteria('`'.$var[0].'`', $var[1], (isset($var[2])?$var[2]:'=')));    				
141
				} elseif (	$object->vars[$var[0]]['data_type']==XOBJ_DTYPE_ARRAY ) 	{
142
    				$criteria->add(new Criteria('`'.$var[0].'`', '%"'.$var[1].'";%', (isset($var[2])?$var[2]:'LIKE')));    				
143
				}
144
    		} elseif (!empty($var[1])&&is_numeric($var[0])) {
145
    			$criteria->add(new Criteria($var[0], $var[1]));
146
    		}
147
    	}
148
    	return $criteria;
149
    }
150
        
151
    function getFilterForm($filter, $field, $sort='created', $op = 'dashboard', $fct='list') {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
152
    	$ele = songlist_getFilterElement($filter, $field, $sort, $op, $fct);
153
    	if (is_object($ele))
154
    		return $ele->render();
155
    	else 
156
    		return '&nbsp;';
157
    }
158
    
159
	function insert($obj, $force=true, $object = null) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
160
    	if ($obj->isNew()) {
161
    		$new = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $new is dead and can be removed.
Loading history...
162
    		$old = $this->create();
163
    		$obj->setVar('created', time());	
164
    	} else {
165
    		$new = false;
166
    		$old = $this->get($obj->getVar('aid'));
167
    		$obj->setVar('updated', time());
168
    	}
169
		
170
    	$albums_handler = xoops_getmodulehandler('albums', 'songlist');
171
		$songs_handler = xoops_getmodulehandler('songs', 'songlist');
0 ignored issues
show
Unused Code introduced by
The assignment to $songs_handler is dead and can be removed.
Loading history...
172
		$genre_handler = xoops_getmodulehandler('genre', 'songlist');
173
		$voice_handler = xoops_getmodulehandler('voice', 'songlist');		
174
		$category_handler = xoops_getmodulehandler('category', 'songlist');
175
   	
176
		if (is_a($object, 'SonglistSongs')) {
177
			if ($object->vars['cid']['changed']==true) {
178
				foreach($obj->vars['cids']['value'] as $cid) {
179
					if (!in_array($cid, $object->getVar('cid'))&&$cid!=0) {
0 ignored issues
show
Bug introduced by
It seems like $object->getVar('cid') can also be of type boolean and null and string; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

179
					if (!in_array($cid, /** @scrutinizer ignore-type */ $object->getVar('cid'))&&$cid!=0) {
Loading history...
180
						$obj->setVar('cids', array_merge($obj->getVar('cids'), array($object->getVar('cid')=>$object->getVar('cid'))));
181
						$category = $category_handler->get($cid);
182
						if (is_object($category)) {
183
			    			$category->setVar('artists', $category->getVar('artists')+1);
184
			    			$category_handler->insert($category, true, $obj);
0 ignored issues
show
Unused Code introduced by
The call to XoopsObjectHandler::insert() has too many arguments starting with true. ( Ignorable by Annotation )

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

184
			    			$category_handler->/** @scrutinizer ignore-call */ 
185
                             insert($category, true, $obj);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
185
						}	
186
					}
187
				}
188
				if (!$old->isNew()) {
189
					foreach($old->getVar('cids') as $cid) {
190
						if (!in_array($cid, $obj->vars['cids']['value'])&&$cid!=0) {
191
							$category = $category_handler->get($cid);
192
							if (is_object($category)) {
193
				    			$category->setVar('artists', $category->getVar('artists')-1);
194
				    			$category_handler->insert($category, true, $obj);
195
							}	
196
						}
197
					}
198
				}
199
			}
200
		    	
201
	    	if ($object->vars['abid']['value']!=0&&$object->vars['aids']['changed']==true) {
202
    			$album = $albums_handler->get($object->vars['abid']['value']);
203
    			if (is_object($album)) {
204
    				$album->setVar('artists', $album->getVar('artists')+1);
205
    				$albums_handler->insert($album, true, $obj);
206
    			}
207
	       	}
208
	    	
209
			if ($object->vars['gid']['value']!=0&&$object->vars['gid']['changed']==true) {
210
    			$genre = $genre_handler->get($object->vars['gid']['value']);
211
    			if (is_object($genre)) {
212
	    			$genre->setVar('artists', $genre->getVar('artists')+1);
213
	    			$genre_handler->insert($genre, true, $obj);
214
    			}
215
	       	}
216
			if ($object->vars['vid']['value']!=0&&$object->vars['vid']['changed']==true) {
217
    			$voice = $voice_handler->get($object->vars['vid']['value']);
218
    			if (is_object($voice)) {
219
	    			$voice->setVar('artists', $voice->getVar('artists')+1);
220
	    			$voice_handler->insert($voice, true, $obj);
221
    			}
222
	       	}
223
			
224
		}
225
    	
226
		if (strlen($obj->getVar('name'))==0)
227
    		return false;
228
    		
229
    	return parent::insert($obj, $force);
230
    }
231
     
232
	var $_objects = array('object'=>array(), 'array'=>array());
233
    
234
    function get($id, $fields = '*') {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
235
    	if (!isset($this->_objects['object'][$id])) {
236
	    	$this->_objects['object'][$id] = parent::get($id, $fields);
237
	    	if (!isset($GLOBALS['songlistAdmin'])&&is_object($this->_objects['object'][$id])) {
238
		    	$sql = 'UPDATE `'.$this->table.'` set hits=hits+1 where `'.$this->keyName.'` = '.$this->_objects['object'][$id]->getVar($this->keyName);
239
		    	$GLOBALS['xoopsDB']->queryF($sql);
240
	    	}
241
    	}
242
    	return $this->_objects['object'][$id];
243
    }
244
    
245
    function getObjects($criteria = NULL, $id_as_key = false, $as_object = true) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
246
    	$ret = parent::getObjects($criteria, $id_as_key, $as_object);
247
    	/*if (!isset($GLOBALS['songlistAdmin'])) {
248
	    	$id = array();
249
	    	foreach($ret as $data) {
250
	    		if ($as_object==true) {
251
	    			if (!in_array($data->getVar($this->keyName), array_keys($this->_objects['object']))) {
252
	    				$this->_objects['object'][$data->getVar($this->keyName)] = $data;
253
	    				$id[$data->getVar($this->keyName)] = $data->getVar($this->keyName);
254
	    			}
255
	    		} else {
256
	    			if (!in_array($data[$this->keyName], array_keys($this->_objects['array']))) {
257
	    				$this->_objects['array'][$data[$this->keyName]] = $data;
258
	    				$id[$data[$this->keyName]] = $data[$this->keyName];;
259
	    			}
260
	    		}
261
	    	}
262
    	}
263
    	if (!isset($GLOBALS['songlistAdmin'])&&count($id)>0) {
264
	    	$sql = 'UPDATE `'.$this->table.'` set hits=hits+1 where `'.$this->keyName.'` IN ('.implode(',', $id).')';
265
	    	$GLOBALS['xoopsDB']->queryF($sql);
266
    	}*/
267
    	return $ret;
268
    }
269
    
270
    function getURL() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
271
    	global $file, $op, $fct, $id, $value, $gid, $vid, $cid, $start, $limit;
272
    	if ($GLOBALS['songlistModuleConfig']['htaccess']) {
273
    	    if ($cid!=0) {
274
    			$artist_handler = xoops_getmodulehandler('artists', 'songlist');
275
    			$artist = $artist_handler->get($cid);
276
    			if (is_object($artist)&&!$artist->isNew()) {
277
    				return XOOPS_URL.'/'.$GLOBALS['songlistModuleConfig']['baseofurl'].'/'.$file.'/'.urlencode(str_replace(array(' ', chr(9)), '-', $artist->getVar('name'))).'/'.$start.'-'.$id.'-'.$op.'-'.$fct.'-'.$gid.'-'.$cid.'/'.urlencode($value).$GLOBALS['songlistModuleConfig']['endofurl'];
278
    			} else {
279
    				return XOOPS_URL.'/'.$GLOBALS['songlistModuleConfig']['baseofurl'].'/'.$file.'/'.$start.'-'.$id.'-'.$op.'-'.$fct.'-'.$gid.'-'.$cid.'/'.urlencode($value).$GLOBALS['songlistModuleConfig']['endofurl'];
280
    			}
281
    		} else {
282
    			return XOOPS_URL.'/'.$GLOBALS['songlistModuleConfig']['baseofurl'].'/'.$file.'/'.$start.'-'.$id.'-'.$op.'-'.$fct.'-'.$gid.'-'.$cid.'/'.urlencode($value).$GLOBALS['songlistModuleConfig']['endofurl'];
283
    		}
284
    	} else {
285
    		return XOOPS_URL.'/modules/songlist/'.$file.'.php?op='.$op.'&fct='.$fct.'&id='.$id.'&value='.urlencode($value).'&gid='.$gid.'&vid='.$vid.'&cid='.$cid.'&start='.$start;
286
    	}
287
    }
288
    
289
    
290
    function getSIDs($criteria = NULL) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
291
    	$ret = array();
292
    	$song_handler = xoops_getmodulehandler('songs', 'songlist');
293
    	foreach($this->getObjects($criteria, true) as $aid => $object) {
294
    		$crita = new Criteria('`aids`', '%"'.$aid.'"%', 'LIKE');
295
    		foreach($song_handler->getObjects($crita, true) as $sid => $song) {
296
    			$ret[$sid] = $sid;
297
    		}
298
    	}
299
    	return $ret;
300
    }
301
    
302
	function getTop($limit=1) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
303
    	$sql = 'SELECT * FROM `'.$this->table.'` WHERE `rank`>=0 ORDER BY (`rank`/`votes`) DESC LIMIT '.$limit;
304
    	$results = $GLOBALS['xoopsDB']->queryF($sql);
305
    	$ret = array();
306
    	$i=0;
307
    	while ($row = $GLOBALS['xoopsDB']->fetchArray($results)) {
308
    		$ret[$i] = $this->create();
309
    		$ret[$i]->assignVars($row);
310
    		$i++;
311
    	}
312
    	return $ret;
313
    }
314
}
315
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...