efqItemTypeHandler   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 207
Duplicated Lines 39.13 %

Importance

Changes 0
Metric Value
dl 81
loc 207
rs 10
c 0
b 0
f 0
wmc 28

7 Methods

Rating   Name   Duplication   Size   Complexity  
C insert() 43 46 8
A __construct() 0 3 1
A getByDir() 0 13 3
A delete() 0 12 2
A getObjItemType() 0 3 1
A set() 0 20 4
D update() 38 39 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
// ID: class.itemtypes.php 30-apr-2007 13:39:01 efqconsultancy
4
//  ------------------------------------------------------------------------ //
5
//                				EFQ Directory			                     //
6
//                    Copyright (c) 2006 EFQ Consultancy                     //
7
//                       <http://www.efqdirectory.com/>                      //
8
//  ------------------------------------------------------------------------ //
9
//  This program is free software; you can redistribute it and/or modify     //
10
//  it under the terms of the GNU General Public License as published by     //
11
//  the Free Software Foundation; either version 2 of the License, or        //
12
//  (at your option) any later version.                                      //
13
//                                                                           //
14
//  You may not change or alter any portion of this comment or credits       //
15
//  of supporting developers from this source code or any supporting         //
16
//  source code which is considered copyrighted (c) material of the          //
17
//  original comment or credit authors.                                      //
18
//                                                                           //
19
//  This program is distributed in the hope that it will be useful,          //
20
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
21
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
22
//  GNU General Public License for more details.                             //
23
//                                                                           //
24
//  You should have received a copy of the GNU General Public License        //
25
//  along with this program; if not, write to the Free Software              //
26
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
27
//  ------------------------------------------------------------------------ //
28
//	Part of the efqDirectory module provided by: wtravel					 //
29
// 	e-mail: [email protected]											 //
30
//	Purpose: Create a business directory for xoops.		 	 				 //
31
//	Based upon the mylinks and the mxDirectory modules						 //
32
// ------------------------------------------------------------------------- //
33
*/
34
35
/**
36
 * Class efqItemType
37
 * Manages operations for item types
38
 *
39
 * @package   efqDirectory
40
 * @author    EFQ Consultancy <[email protected]>
41
 * @copyright EFQ Consultancy (c) 2007
42
 * @version   1.1.0
43
 */
44
class efqItemType extends XoopsObject
0 ignored issues
show
Bug introduced by
The type XoopsObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
45
{
46
47
    /**
48
     * efqItemType::efqItemType()
49
     *
50
     * @param bool $obj
51
     * @internal param bool $itemtype
52
     */
53
    public function __construct($obj = false)
54
    {
55
        global $moddir;
56
        $this->db = XoopsDatabaseFactory::getDatabaseConnection();
0 ignored issues
show
Bug introduced by
The type XoopsDatabaseFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
57
        $this->initVar('typeid', XOBJ_DTYPE_INT, 0, false);
0 ignored issues
show
Bug introduced by
The constant XOBJ_DTYPE_INT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
58
        $this->initVar('typename', XOBJ_DTYPE_TXTBOX, null, false, 50);
0 ignored issues
show
Bug introduced by
The constant XOBJ_DTYPE_TXTBOX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
59
        $this->initVar('level', XOBJ_DTYPE_INT, 0, true, 4);
60
        $this->initVar('dirid', XOBJ_DTYPE_INT, 0, true, 5);
61
    }
62
}
63
64
/**
65
 * Class efqFieldTypeHandler
66
 * Manages database operations for field types
67
 *
68
 * @package   efqDirectory
69
 * @author    EFQ Consultancy <[email protected]>
70
 * @copyright EFQ Consultancy (c) 2007
71
 * @version   1.1.0
72
 */
73
class efqItemTypeHandler extends xoopsObject
0 ignored issues
show
Bug introduced by
The type xoopsObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
74
{
75
    public $db; //Database reference
76
    public $objItemType;
77
78
    /**
79
     * efqItemTypeHandler::efqItemTypeHandler()
80
     *
81
     */
82
    public function __construct()
83
    {
84
        $this->db = XoopsDatabaseFactory::getDatabaseConnection();
85
    }
86
87
    /**
88
     * Function insert inserts new record into DB
89
     * @author    EFQ Consultancy <[email protected]>
90
     * @copyright EFQ Consultancy (c) 2008
91
     * @version   1.0.0
92
     *
93
     * @param   efqItemType $obj object
94
     *
95
     * @param bool     $forceQuery
96
     * @return bool true if insertion is succesful, false if unsuccesful
97
     */
98 View Code Duplication
    public function insert($obj, $forceQuery = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
    {
100
        $tablename    = 'efqdiralpha1_itemtypes';
101
        $keyName      = 'typeid';
102
        $excludedVars = array();
103
        if ($obj instanceof efqItemType) {
104
            // Variable part of this function ends. From this line you can copy
105
            // this function for similar object handling functions.
106
            $obj->cleanVars();
107
            $cleanvars = $obj->cleanVars;
108
        } else {
109
            return false;
110
        }
111
        $countVars = count($cleanvars);
112
        $i         = 1;
113
        $strFields = '';
114
        $strValues = '';
115
        foreach ($cleanvars as $k => $v) {
116
            if (!in_array($k, $excludedVars)) {
117
                $strFields .= $k;
118
                $strValues .= "'" . $v . "'";
119
                if ($i < $countVars) {
120
                    $strFields .= ', ';
121
                    $strValues .= ', ';
122
                }
123
                $i++;
124
            }
125
        }
126
        $sql = sprintf('INSERT INTO %s (%s) VALUES (%s)', $this->db->prefix($tablename), $strFields, $strValues);
127
        if ($forceQuery) {
128
            if ($this->db->queryF($sql)) {
129
                $itemid = $this->db->getInsertId();
130
                $obj->setVar($keyName, $itemid);
131
132
                return true;
133
            }
134
        } else {
135
            if ($this->db->query($sql)) {
136
                $itemid = $this->db->getInsertId();
137
                $obj->setVar($keyName, $itemid);
138
139
                return true;
140
            }
141
        }
142
143
        return false;
144
    }
145
146
    /**
147
     * Function update updates record in DB
148
     * @author    EFQ Consultancy <[email protected]>
149
     * @copyright EFQ Consultancy (c) 2008
150
     * @version   1.0.0
151
     *
152
     * @param      $obj
153
     * @param bool $forceQuery
154
     * @return bool true if update is succesful, false if unsuccesful
155
     * @internal  param object $objOffer object of type listing
156
     */
157 View Code Duplication
    public function update($obj, $forceQuery = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
    {
159
        $tablename    = 'efqdiralpha1_itemtypes';
160
        $keyName      = 'typeid';
161
        $excludedVars = array();
162
        if ($obj instanceof efqItemType) {
163
            // Variable part of this function ends. From this line you can copy
164
            // this function for similar object handling functions.
165
            $obj->cleanVars();
166
            $cleanvars = $obj->cleanVars;
167
            $keyValue  = $obj->getVar($keyName);
168
        } else {
169
            return false;
170
        }
171
        $countVars = count($cleanvars);
172
        $i         = 0;
173
        $strSet    = '';
174
        $strValues = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $strValues is dead and can be removed.
Loading history...
175
        foreach ($cleanvars as $k => $v) {
176
            if (!in_array($k, $excludedVars)) {
177
                if ($i < $countVars and $i > 0) {
178
                    $strSet .= ', ';
179
                }
180
                $strSet .= $k . '=' . "'" . $v . "'";
181
            }
182
            $i++;
183
        }
184
        $sql = sprintf('UPDATE %s SET %s WHERE %s = %u', $this->db->prefix($tablename), $strSet, $keyName, $keyValue);
185
        if ($forceQuery) {
186
            if ($this->db->queryF($sql)) {
187
                return true;
188
            }
189
        } else {
190
            if ($this->db->query($sql)) {
191
                return true;
192
            }
193
        }
194
195
        return false;
196
    }
197
198
    /**
199
     * Function set()
200
     *
201
     * Sets the object with data from query
202
     *
203
     * @param int $typeid
204
     * @return bool true or false
205
     */
206
    public function set($typeid = 0)
207
    {
208
        $sql     = 'SELECT typeid,typename,level,dirid FROM ' . $this->db->prefix('efqdiralpha1_itemtypes') . ' WHERE typeid=' . (int)$typeid . '';
209
        $result  = $this->db->query($sql);
210
        $numrows = $this->db->getRowsNum($result);
211
        if ($numrows > 0) {
212
            while (list($typeid, $typename, $level, $dirid) = $this->db->fetchRow($result)) {
213
                if (!$this->objItemType) {
214
                    $this->objItemType = new efqSubscriptionOffer();
215
                }
216
                $this->objItemType->setVar('typeid', $typeid);
217
                $this->objItemType->setVar('typename', $typename);
218
                $this->objItemType->setVar('level', $level);
219
                $this->objItemType->setVar('dirid', $dirid);
220
            }
221
        } else {
222
            return false;
223
        }
224
225
        return true;
226
    }
227
228
    /**
229
     * @return mixed
230
     */
231
    public function getObjItemType()
232
    {
233
        return $this->objItemType;
234
    }
235
236
    /**
237
     * efqItemTypeHandler::getByDir()
238
     *
239
     * @param integer $dirid
240
     * @return array|bool
241
     */
242
    public function getByDir($dirid = 0)
243
    {
244
        $arr = array();
245
        $sql = 'SELECT typeid,typename,level FROM ' . $this->db->prefix('efqdiralpha1_itemtypes') . ' WHERE dirid=' . (int)$dirid . '';
246
        if (!$result = $this->db->query($sql)) {
247
            return false;
248
        }
249
        $numrows = $this->db->getRowsNum($result);
0 ignored issues
show
Unused Code introduced by
The assignment to $numrows is dead and can be removed.
Loading history...
250
        while (list($typeid, $typename, $level) = $this->db->fetchRow($result)) {
251
            $arr[$typeid] = array('typeid' => $typeid, 'typename' => $typename, 'level' => $level);
252
        }
253
254
        return $arr;
255
    }
256
257
    /**
258
     * Function delete: Delete record
259
     *
260
     * @author    EFQ Consultancy <[email protected]>
261
     * @copyright EFQ Consultancy (c) 2007
262
     * @version   1.0.0
263
     *
264
     * @param $obj
265
     * @return array|bool
266
     * @internal  param int $orderid - Default: '0' - Order ID
267
     */
268
    public function delete($obj)
269
    {
270
        $tablename = 'efqdiralpha1_itemtypes';
271
        $keyName   = 'typeid';
272
        $id        = $obj->getVar($keyName);
273
        if ($id != 0) {
274
            $sql = 'DELETE FROM ' . $this->db->prefix($tablename) . ' WHERE ' . $keyName . '=' . (int)$id . '';
275
            $this->db->queryF($sql);
276
277
            return true;
278
        } else {
279
            return false;
280
        }
281
    }
282
}
283