Passed
Push — master ( 2ecc51...c1b656 )
by Michael
04:26 queued 02:02
created

efqdirectoryCouponHandler   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 285
Duplicated Lines 10.88 %

Importance

Changes 0
Metric Value
dl 31
loc 285
rs 8.6
c 0
b 0
f 0
wmc 37

10 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 18 18 4
A create() 0 9 2
B prepare2show() 0 26 3
A getLinkedCoupon() 0 17 3
A getCountByLink() 11 11 2
A increment() 0 5 1
C insert() 0 42 8
C getObjects() 0 35 8
A delete() 0 8 2
A getByLink() 0 20 4

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
 * 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
 * @copyright    {@link https://xoops.org/ XOOPS Project}
14
 * @license      {@link http://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @package      efqdirectory
16
 * @since
17
 * @author       Martijn Hertog (aka wtravel)
18
 * @author       XOOPS Development Team,
19
 */
20
21
if (!class_exists('Coupon')) {
22
    class Coupon 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...
23
    {
24
        //Constructor
25
        /**
26
         * @param mixed $coupid int for coupon id or array with name->value pairs of properties
27
         */
28
        public function __construct($coupid = false)
29
        {
30
            global $moddir;
31
            $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...
32
            $this->initVar('couponid', XOBJ_DTYPE_INT, null, 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...
33
            $this->initVar('itemid', XOBJ_DTYPE_INT, null, true);
34
            $this->initVar('description', XOBJ_DTYPE_TXTAREA);
0 ignored issues
show
Bug introduced by
The constant XOBJ_DTYPE_TXTAREA was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
35
            $this->initVar('image', XOBJ_DTYPE_TXTBOX);
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...
36
            $this->initVar('publish', XOBJ_DTYPE_INT, 0, false);
37
            $this->initVar('expire', XOBJ_DTYPE_INT, 0, false);
38
            $this->initVar('heading', XOBJ_DTYPE_TXTBOX);
39
            $this->initVar('counter', XOBJ_DTYPE_INT, 0, false);
40
            $this->initVar('lbr', XOBJ_DTYPE_INT, 0, false);
41 View Code Duplication
            if ($coupid !== false) {
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...
42
                if (is_array($coupid)) {
43
                    $this->assignVars($coupid);
44
                } else {
45
                    $couponHandler = xoops_getModuleHandler('coupon', $moddir);
0 ignored issues
show
Bug introduced by
The function xoops_getModuleHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

45
                    $couponHandler = /** @scrutinizer ignore-call */ xoops_getModuleHandler('coupon', $moddir);
Loading history...
46
                    $coupon        = $couponHandler->get($coupid);
47
                    foreach ($coupon->vars as $k => $v) {
48
                        $this->assignVar($k, $v['value']);
49
                    }
50
                    unset($coupon);
51
                }
52
            }
53
        }
54
55
        public function toArray()
56
        {
57
            $ret = array();
58
            foreach ($this->vars as $k => $v) {
59
                $ret[$k] = $v['value'];
60
            }
61
62
            return $ret;
63
        }
64
    }
65
}
66
67
// Change the class name below to enable custom directory (Capitolize first letter YourdirectoryCouponHandler)
68
class efqdirectoryCouponHandler 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...
69
{
70
    /**
71
     * create a new coupon object
72
     *
73
     * @param bool $isNew flag the new objects as "new"?
74
     * @return object {@link Coupon}
75
     */
76
    //var $coupon;
77
    //var $db;
78
79
    public function &create($isNew = true)
80
    {
81
        //$this->db = XoopsDatabaseFactory::getDatabaseConnection();
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...
82
        $coupon = new Coupon();
83
        if ($isNew) {
84
            $coupon->setNew();
85
        }
86
87
        return $coupon;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $coupon returns the type Coupon which is incompatible with the documented return type object.
Loading history...
88
    }
89
90
    /**
91
     * retrieve a coupon
92
     *
93
     * @param bool|int $coupid ID of the coupon
94
     * @return mixed reference to the <a href='psi_element://Coupon'>Coupon</a> object, FALSE if failed
95
     *                         object, FALSE if failed
96
     */
97 View Code Duplication
    public function &get($coupid = 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...
98
    {
99
        if ($coupid === false) {
100
            return false;
101
        }
102
        $coupid = (int)$coupid;
103
        if ($coupid > 0) {
104
            $sql = 'SELECT * FROM ' . $this->db->prefix($module->getVar('dirname', 'n') . '_coupon') . ' WHERE couponid=' . $coupid;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $module seems to be never defined.
Loading history...
105
            if (!$result = $this->db->query($sql)) {
106
                return false;
107
            }
108
            $coupon =& $this->create(false);
109
            $coupon->assignVars($this->db->fetchArray($result));
110
111
            return $coupon;
112
        }
113
114
        return false;
115
    }
116
117
    /**
118
     * Save coupon in database
119
     * @param object|XoopsObject $coupon reference to the {@link Coupon}
120
     *                                   object
121
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
122
     * @internal param bool $force
123
     */
124
    public function insert(XoopsObject $coupon)
125
    {
126
        global $eh, $xoopsDB, $description, $image, $heading, $couponid;
127
        if (get_class($coupon) != 'Coupon') {
128
            echo ' class not coupon ';
129
130
            return false;
131
        }
132
        if (!$coupon->isDirty()) {
133
            echo ' coupon not dirty ';
134
135
            return true;
136
        }
137
        if (!$coupon->cleanVars()) {
138
            echo ' coupon not cleanvars ';
139
140
            return false;
141
        }
142
        foreach ($coupon->cleanVars as $k => $v) {
143
            ${$k} = $v;
144
        }
145
        if ($coupon->_isNew) {
146
            $sql = 'INSERT INTO ' . $this->db->prefix($module->getVar('dirname', 'n') . '_coupon') . "
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $module seems to be never defined.
Loading history...
147
                (itemid, description, image, publish, expire, heading, lbr) VALUES
148
                ($itemid, " . $this->db->quoteString($description) . ', ' . $this->db->quoteString($image) . ", $publish, $expire, " . $this->db->quoteString($heading) . ", $lbr)";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $lbr seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $itemid seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $publish seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $expire seems to be never defined.
Loading history...
149
        } else {
150
            $sql = 'UPDATE ' . $this->db->prefix($module->getVar('dirname', 'n') . '_coupon') . " SET
151
                itemid = $itemid,
152
                description = " . $this->db->quoteString($description) . ',
153
                image = ' . $this->db->quoteString($image) . ",
154
                publish = $publish,
155
                lbr = $lbr,
156
                heading = " . $this->db->quoteString($heading) . ",
157
                expire = $expire WHERE couponid = " . $couponid;
158
        }
159
        $xoopsDB->query($sql) or $eh->show('0013');
160
        if ($coupon->_isNew) {
161
            $coupon->setVar('couponid', $this->db->getInsertId());
162
            $coupon->_isNew = false;
163
        }
164
165
        return true;
166
    }
167
168
    /**
169
     * delete a coupon from the database
170
     *
171
     * @param object|XoopsObject $coupon reference to the {@link Coupon}
172
     *                                   to delete
173
     * @return bool FALSE if failed.
174
     * @internal param bool $force
175
     */
176
    public function delete(XoopsObject $coupon)
177
    {
178
        $sql = 'DELETE FROM ' . $this->db->prefix($module->getVar('dirname', 'n') . '_coupon') . ' WHERE couponid = ' . (int)$coupon->getVar('couponid');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $module seems to be never defined.
Loading history...
179
        if (!$this->db->query($sql)) {
180
            return false;
181
        }
182
183
        return true;
184
    }
185
186
    /**
187
     * get {@link Coupon} objects from criteria
188
     *
189
     * @param object $criteria   reference to a {@link Criteria} or {@link CriteriaCompo} object
190
     * @param bool   $as_objects if true, the returned array will be {@link Coupon} objects
191
     * @param bool   $id_as_key  if true, the returned array will have the coupon ids as key
192
     *
193
     * @return array array of {@link Coupon} objects
194
     */
195
    public function &getObjects($criteria = null, $as_objects = true, $id_as_key = false)
196
    {
197
        $ret   = array();
198
        $limit = $start = 0;
199
        $sql   = 'SELECT l.title AS listingTitle, coup.couponid, coup.heading, coup.counter, l.itemid, l.logourl, coup.description, coup.image, coup.lbr, coup.publish, coup.expire
200
            FROM ' . $this->db->prefix($module->getVar('dirname', 'n') . '_coupon') . ' coup, ' . $this->db->prefix($module->getVar('dirname', 'n') . '_items') . ' l
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $module seems to be never defined.
Loading history...
201
            WHERE coup.itemid=l.itemid AND ';
202
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
203
            $sql .= ' ' . $criteria->render();
204
            if ($criteria->getSort() != '') {
205
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
206
            }
207
            $limit = $criteria->getLimit();
208
            $start = $criteria->getStart();
209
        }
210
        $result = $this->db->query($sql, $limit, $start);
211
        if (!$result) {
212
            return $ret;
213
        }
214
        while ($myrow = $this->db->fetchArray($result)) {
215
            if ($as_objects) {
216
                $coupon = new Coupon();
217
                $coupon->assignVars($myrow);
218
                if (!$id_as_key) {
219
                    $ret[] =& $coupon;
220
                } else {
221
                    $ret[$myrow['couponid']] =& $coupon;
222
                }
223
                unset($coupon);
224
            } else {
225
                $ret[] = $myrow;
226
            }
227
        }
228
229
        return $ret;
230
    }
231
232
    /**
233
     * get {@link Coupon} objects by listing
234
     *
235
     * @param int $itemid listing id
236
     *
237
     * @return array array of {@link Coupon} objects
238
     */
239
    public function &getByLink($itemid)
240
    {
241
        global $eh;
242
        $ret    = array();
243
        $limit  = $start = 0;
244
        $now    = time();
245
        $sql    = 'SELECT l.title AS listingTitle, coup.couponid, coup.heading, coup.counter, l.itemid, l.logourl, coup.description, coup.image, coup.lbr, coup.publish, coup.expire
246
            FROM ' . $this->db->prefix($module->getVar('dirname', 'n') . '_coupon') . ' coup, ' . $this->db->prefix($module->getVar('dirname', 'n') . '_items') . ' l';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $module seems to be never defined.
Loading history...
247
        $sql    .= ' WHERE coup.itemid = l.itemid AND coup.itemid=' . (int)$itemid . ' AND coup.publish < ' . $now . ' AND (coup.expire = 0 OR coup.expire > ' . $now . ')';
248
        $sql    .= ' ORDER BY listingTitle ASC';
249
        $result = $this->db->query($sql, $limit, $start) or $eh->show('0013');
250
251
        if (!$result) {
252
            return $ret;
253
        }
254
        while ($myrow = $this->db->fetchArray($result)) {
255
            $ret[] = $myrow;
256
        }
257
258
        return $ret;
259
    }
260
261
    /**
262
     * Returns number of coupons for a listing
263
     *
264
     * @param int $itemid listing id
265
     * @return bool|int
266
     */
267 View Code Duplication
    public function getCountByLink($itemid)
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...
268
    {
269
        $ret = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
270
        $now = time();
271
        $sql = 'SELECT count(*) FROM ' . $this->db->prefix($module->getVar('dirname', 'n') . '_coupon') . ' WHERE itemid=' . (int)$itemid . ' AND publish < ' . $now . ' AND (expire = 0 OR expire > ' . $now . ')';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $module seems to be never defined.
Loading history...
272
        if (!$result = $this->db->query($sql)) {
273
            return false;
274
        }
275
        list($ret) = $this->db->fetchRow($result);
276
277
        return $ret;
278
    }
279
280
    /**
281
     * get {@link Coupon} object with listing info
282
     *
283
     * @param int $coupid Coupon ID
284
     *
285
     * @return array|object
286
     */
287
    public function &getLinkedCoupon($coupid)
288
    {
289
        $ret    = array();
290
        $limit  = $start = 0;
291
        $now    = time();
0 ignored issues
show
Unused Code introduced by
The assignment to $now is dead and can be removed.
Loading history...
292
        $sql    = 'SELECT l.title AS listingTitle, coup.couponid, coup.heading, coup.counter, l.itemid, l.logourl, coup.description,  coup.image, coup.lbr, coup.publish, coup.expire
293
            FROM ' . $this->db->prefix($module->getVar('dirname', 'n') . '_coupon') . ' coup, ' . $this->db->prefix($module->getVar('dirname', 'n') . '_items') . ' l';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $module seems to be never defined.
Loading history...
294
        $sql    .= ' WHERE coup.itemid = l.itemid AND coup.couponid=' . (int)$coupid;
295
        $result = $this->db->query($sql, $limit, $start);
296
        if (!$result) {
297
            return $ret;
298
        }
299
        while ($myrow = $this->db->fetchArray($result)) {
300
            $ret[] = $myrow;
301
        }
302
303
        return $ret;
304
    }
305
306
    /**
307
     * Prepares rows from getByLink and getByCategory to be displayed
308
     *
309
     * @param array $array rows to be prepared
310
     *
311
     * @return array
312
     */
313
    public function prepare2show($array)
314
    {
315
        $myts = MyTextSanitizer::getInstance();
0 ignored issues
show
Bug introduced by
The type MyTextSanitizer 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...
316
        $ret  = array();
317
        foreach ($array as $key => $myrow) {
318
            $description = $myts->displayTarea($myrow['description'], 1, 1, 1, 1, $myrow['lbr']);
319
            if ($myrow['expire'] > 0) {
320
                $expire = formatTimestamp($myrow['expire'], 's');
0 ignored issues
show
Bug introduced by
The function formatTimestamp was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

320
                $expire = /** @scrutinizer ignore-call */ formatTimestamp($myrow['expire'], 's');
Loading history...
321
            } else {
322
                $expire = 0;
323
            }
324
            $ret ['items']['coupons'][]    = array(
325
                'itemid'      => $myrow['itemid'],
326
                'couponid'    => $myrow['couponid'],
327
                'heading'     => $myts->htmlSpecialChars($myrow['heading']),
328
                'description' => $description,
329
                'logourl'     => $myts->displayTarea($myrow['logourl']),
330
                'publish'     => formatTimestamp($myrow['publish'], 's'),
331
                'expire'      => $expire,
332
                'counter'     => (int)$myrow['counter']
333
            );
334
            $ret ['items']['listingTitle'] = $myts->displayTarea($myrow['listingTitle']);
335
            $ret ['items']['itemid']       = $myrow['itemid'];
336
        }
337
338
        return $ret;
339
    }
340
341
    /**
342
     * Increment coupon counter
343
     *
344
     * @param int $couponid
345
     *
346
     * @return bool
347
     */
348
    public function increment($couponid)
349
    {
350
        $sql = 'UPDATE ' . $this->db->prefix($module->getVar('dirname', 'n') . '_coupon') . ' SET counter=counter+1 WHERE couponid=' . (int)$couponid;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $module seems to be never defined.
Loading history...
351
352
        return $this->db->queryF($sql);
353
    }
354
}
355