efqXdirHandler   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 179
Duplicated Lines 1.68 %

Importance

Changes 0
Metric Value
dl 3
loc 179
rs 10
c 0
b 0
f 0
wmc 17

9 Methods

Rating   Name   Duplication   Size   Complexity  
A get_xdir_cats() 0 3 1
A setCategories_xdir() 3 19 4
A saveCategories() 0 18 3
A set_xdir_cats() 0 4 2
A get_errors() 0 3 1
A __construct() 0 3 1
A createFieldTypes() 0 13 2
B createDataTypes() 0 24 2
A doMigrate() 0 6 1

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.offer.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 efqXdirHandler
37
 * Manages database operations for migration from the xDirectory module
38
 *
39
 * @package   efqDirectory
40
 * @author    EFQ Consultancy <[email protected]>
41
 * @copyright EFQ Consultancy (c) 2007
42
 * @version   1.1.0
43
 */
44
class efqXdirHandler extends XoopsObjectHandler
0 ignored issues
show
Bug introduced by
The type XoopsObjectHandler 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
//    public $db; //Database reference
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
47
    public $objXdir;
48
    public $errors; //array();
49
    public $categories; //array();
50
    public $listings; //array();
51
    public $xdir_cats; //array();
52
    public $efq_dirid;
53
    public $fieldtypes; //array();
54
    public $datatypes; //array();
55
56
    /**
57
     * efqSubscriptionOfferHandler::efqItemTypeHandler()
58
     *
59
     */
60
    public function __construct()
61
    {
62
        $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...
63
    }
64
65
    /**
66
     * @param $arr
67
     */
68
    public function set_xdir_cats($arr)
69
    {
70
        if (is_array($arr)) {
71
            $this->xdir_cats = $arr;
72
        }
73
    }
74
75
    /**
76
     * @return mixed
77
     */
78
    public function get_xdir_cats()
79
    {
80
        return $this->xdir_cats;
81
    }
82
83
    /**
84
     * @return mixed
85
     */
86
    public function get_errors()
87
    {
88
        return $this->errors;
89
    }
90
91
    /**
92
     * getCategories_xdir()
93
     *
94
     * Get xdir categories from database and return
95
     *
96
     * @return \arr|bool
97
     */
98
    public function setCategories_xdir()
99
    {
100
        $arr    = array();
101
        $sql    = 'SELECT cid, pid, title, imgurl FROM ' . $this->db->prefix('efqdiralpha1_xdir_cat');
102
        $result = $this->db->query($sql);
103
        if (!$result) {
104
            return false;
105
        }
106
        $numrows = $this->db->getRowsNum($result);
107
        if ($numrows > 0) {
108 View Code Duplication
            while (list($cid, $pid, $title, $imgurl) = $this->db->fetchRow($result)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
109
                $arr[] = array('cid' => $cid, 'pid' => $pid, 'title' => $title, 'imgurl' => $imgurl);
110
            }
111
        } else {
112
            return false;
113
        }
114
        $this->set_xdir_cats($arr);
115
116
        return true;
117
    }
118
119
    /**
120
     * @param int $dirid
121
     * @return bool
122
     */
123
    public function saveCategories($dirid = 0)
124
    {
125
        $tablename = 'efqdiralpha1_cat';
126
        $xdir_cats = $this->get_xdir_cats();
127
        foreach ($xdir_cats as $xdir_cat) {
128
            $cid    = $xdir_cat['cid'];
129
            $pid    = $xdir_cat['pid'];
130
            $title  = $xdir_cat['title'];
131
            $imgurl = $xdir_cat['imgurl'];
132
            //Set fields and values
133
            $strFields = 'cid,dirid,title,active,pid,img,allowlist';
134
            $strValues = "$cid,$dirid,'$title',1,$pid,'$imgurl',1";
135
            $sql       = sprintf('INSERT INTO %s (%s) VALUES (%s)', $this->db->prefix($tablename), $strFields, $strValues);
136
            if ($this->db->queryF($sql)) {
137
                $itemid = $this->db->getInsertId();
138
                $obj->setVar($keyName, $itemid);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $keyName seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $obj seems to be never defined.
Loading history...
139
140
                return true;
141
            }
142
        }
143
    }
144
145
    /**
146
     * Function createDataTypes inserts datatypes into DB
147
     * @author    EFQ Consultancy <[email protected]>
148
     * @copyright EFQ Consultancy (c) 2008
149
     * @version   1.0.0
150
     * @return bool true if insertion is succesful, false if unsuccesful
151
     * @internal  param object $obj object
152
     *
153
     */
154
    public function createDataTypes()
155
    {
156
        $datatypeHandler = new efqDataTypeHandler();
157
        $arr[]            = array('title' => _MD_XDIR_DTYPE_ADDRESS, 'fieldtype' => _MD_XDIR_FIELDTYPE_TEXTBOX);
0 ignored issues
show
Bug introduced by
The constant _MD_XDIR_DTYPE_ADDRESS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant _MD_XDIR_FIELDTYPE_TEXTBOX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Comprehensibility Best Practice introduced by
$arr was never initialized. Although not strictly required by PHP, it is generally a good practice to add $arr = array(); before regardless.
Loading history...
158
        $arr[]            = array('title' => _MD_XDIR_DTYPE_ADDRESS2, 'fieldtype' => _MD_XDIR_FIELDTYPE_TEXTBOX);
0 ignored issues
show
Bug introduced by
The constant _MD_XDIR_DTYPE_ADDRESS2 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
159
        $arr[]            = array('title' => _MD_XDIR_DTYPE_CITY, 'fieldtype' => _MD_XDIR_FIELDTYPE_TEXTBOX);
0 ignored issues
show
Bug introduced by
The constant _MD_XDIR_DTYPE_CITY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
160
        $arr[]            = array('title' => _MD_XDIR_DTYPE_STATE, 'fieldtype' => _MD_XDIR_FIELDTYPE_TEXTBOX);
0 ignored issues
show
Bug introduced by
The constant _MD_XDIR_DTYPE_STATE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
161
        $arr[]            = array('title' => _MD_XDIR_DTYPE_ZIP, 'fieldtype' => _MD_XDIR_FIELDTYPE_TEXTBOX);
0 ignored issues
show
Bug introduced by
The constant _MD_XDIR_DTYPE_ZIP was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
162
        $arr[]            = array('title' => _MD_XDIR_DTYPE_COUNTRY, 'fieldtype' => _MD_XDIR_FIELDTYPE_TEXTBOX);
0 ignored issues
show
Bug introduced by
The constant _MD_XDIR_DTYPE_COUNTRY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
163
        $arr[]            = array('title' => _MD_XDIR_DTYPE_PHONE, 'fieldtype' => _MD_XDIR_FIELDTYPE_TEXTBOX);
0 ignored issues
show
Bug introduced by
The constant _MD_XDIR_DTYPE_PHONE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
164
        $arr[]            = array('title' => _MD_XDIR_DTYPE_FAX, 'fieldtype' => _MD_XDIR_FIELDTYPE_TEXTBOX);
0 ignored issues
show
Bug introduced by
The constant _MD_XDIR_DTYPE_FAX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
165
        $arr[]            = array('title' => _MD_XDIR_DTYPE_EMAIL, 'fieldtype' => _MD_XDIR_FIELDTYPE_EMAIL);
0 ignored issues
show
Bug introduced by
The constant _MD_XDIR_FIELDTYPE_EMAIL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant _MD_XDIR_DTYPE_EMAIL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
166
        $arr[]            = array('title' => _MD_XDIR_DTYPE_URL, 'fieldtype' => _MD_XDIR_FIELDTYPE_URL);
0 ignored issues
show
Bug introduced by
The constant _MD_XDIR_DTYPE_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant _MD_XDIR_FIELDTYPE_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
167
        foreach ($arr as $datatype) {
168
            $objDataType = new efqFieldType;
169
            $objDataType->setVar('title', $datatype['title']);
170
            $objDataType->setVar('section', 0);
171
            $objDataType->setVar('uid', $xoopsUser->getVar('uid'));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $xoopsUser seems to be never defined.
Loading history...
172
            $objDataType->setVar('defaultyn', 1);
173
            $objDataType->setVar('created', time());
174
            $objDataType->setVar('activeyn', 1);
175
            $objDataType->setVar('fieldtypeid', $this->fieldtypes[$datatype['fieldtype']]);
176
            $datatypeHandler->insertDataType($objDataType, true);
177
            $datatypes[$datatype['title']] = $objDataType->getVar('dtypeid');
178
        }
179
    }
180
181
    /**
182
     * Function createFieldTypes inserts field types into DB
183
     * @author    EFQ Consultancy <[email protected]>
184
     * @copyright EFQ Consultancy (c) 2008
185
     * @version   1.0.0
186
     * @return bool true if insertion is succesful, false if unsuccesful
187
     * @internal  param object $obj object
188
     *
189
     */
190
    public function createFieldTypes()
191
    {
192
        $fieldtypeHandler = new efqFieldTypeHandler();
193
        $arr[]             = array('title' => _MD_XDIR_FIELDTYPE_TEXTBOX, 'fieldtype' => 'textbox');
0 ignored issues
show
Bug introduced by
The constant _MD_XDIR_FIELDTYPE_TEXTBOX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Comprehensibility Best Practice introduced by
$arr was never initialized. Although not strictly required by PHP, it is generally a good practice to add $arr = array(); before regardless.
Loading history...
194
        $arr[]             = array('title' => _MD_XDIR_FIELDTYPE_EMAIL, 'fieldtype' => 'email');
0 ignored issues
show
Bug introduced by
The constant _MD_XDIR_FIELDTYPE_EMAIL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
195
        $arr[]             = array('title' => _MD_XDIR_FIELDTYPE_URL, 'fieldtype' => 'url');
0 ignored issues
show
Bug introduced by
The constant _MD_XDIR_FIELDTYPE_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
196
        foreach ($arr as $fieldtype) {
197
            $objFieldtype = new efqFieldType;
198
            $objFieldtype->setVar('title', $fieldtype['title']);
199
            $objFieldtype->setVar('fieldtype', $fieldtype['fieldtype']);
200
            $objFieldtype->setVar('dirid', $this->efq_dirid);
201
            $fieldtypeHandler->insertFieldType($objFieldtype, true);
202
            $fieldtypes[$fieldtype['title']] = $objFieldtype->getVar('typeid');
203
        }
204
    }
205
206
    /**
207
     * Function doMigrate migrates xDirectory to EFQ Directory
208
     * @author    EFQ Consultancy <[email protected]>
209
     * @copyright EFQ Consultancy (c) 2008
210
     * @version   1.0.0
211
     *
212
     * @param int $dirid
213
     * @return bool true if insertion is succesful, false if unsuccesful
214
     * @internal  param object $obj object
215
     *
216
     */
217
    public function doMigrate($dirid = 0)
218
    {
219
        $this->setCategories_xdir();
220
        $this->saveCategories($this->efq_dirid);
221
        $this->createFieldTypes();
222
        $this->createDataTypes();
223
    }
224
}
225