assessment_respostas   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 86
Duplicated Lines 55.81 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 48
loc 86
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 21 3
A load() 9 9 2
D getAllassessment_respostass() 30 30 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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 34 and the first side effect is on line 27.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
// $Id: assessment_respostas.php,v 1.2 2007/03/17 03:12:34 marcellobrandao Exp $
3
//  ------------------------------------------------------------------------ //
4
//                XOOPS - PHP Content Management System                      //
5
//                    Copyright (c) 2000 XOOPS.org                           //
6
//                       <http://www.xoops.org/>                             //
7
//  ------------------------------------------------------------------------ //
8
//  This program is free software; you can redistribute it and/or modify     //
9
//  it under the terms of the GNU General Public License as published by     //
10
//  the Free Software Foundation; either version 2 of the License, or        //
11
//  (at your option) any later version.                                      //
12
//                                                                           //
13
//  You may not change or alter any portion of this comment or credits       //
14
//  of supporting developers from this source code or any supporting         //
15
//  source code which is considered copyrighted (c) material of the          //
16
//  original comment or credit authors.                                      //
17
//                                                                           //
18
//  This program is distributed in the hope that it will be useful,          //
19
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21
//  GNU General Public License for more details.                             //
22
//                                                                           //
23
//  You should have received a copy of the GNU General Public License        //
24
//  along with this program; if not, write to the Free Software              //
25
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26
//  ------------------------------------------------------------------------ //
27
include_once XOOPS_ROOT_PATH . '/kernel/object.php';
28
29
/**
30
 * assessment_respostas class.
31
 * $this class is responsible for providing data access mechanisms to the data source
32
 * of XOOPS user class objects.
33
 */
34
class assessment_respostas extends XoopsObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
35
{
36
    public $db;
37
38
    // constructor
39
40
    /**
41
     * @param null $id
42
     * @return assessment_respostas
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
43
     */
44
    public function __construct($id = null)
45
    {
46
        $this->db = XoopsDatabaseFactory::getDatabaseConnection();
47
        $this->initVar('cod_resposta', XOBJ_DTYPE_INT, null, false, 10);
48
        $this->initVar('cod_pergunta', XOBJ_DTYPE_INT, null, false, 10);
49
        $this->initVar('titulo', XOBJ_DTYPE_TXTBOX, null, false);
50
        $this->initVar('iscerta', XOBJ_DTYPE_INT, null, false, 10);
51
        $this->initVar('data_criacao', XOBJ_DTYPE_TXTBOX, null, false);
52
        $this->initVar('data_update', XOBJ_DTYPE_TXTBOX, null, false);
53
        $this->initVar('uid_elaboradores', XOBJ_DTYPE_TXTBOX, null, false);
54
        $this->initVar('isativa', XOBJ_DTYPE_INT, null, false, 10);
55 View Code Duplication
        if (!empty($id)) {
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...
56
            if (is_array($id)) {
57
                $this->assignVars($id);
58
            } else {
59
                $this->load((int)$id);
60
            }
61
        } else {
62
            $this->setNew();
63
        }
64
    }
65
66
    /**
67
     * @param $id
68
     */
69 View Code Duplication
    public function load($id)
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...
70
    {
71
        $sql   = 'SELECT * FROM ' . $this->db->prefix('assessment_respostas') . ' WHERE cod_resposta=' . $id;
72
        $myrow = $this->db->fetchArray($this->db->query($sql));
73
        $this->assignVars($myrow);
74
        if (!$myrow) {
75
            $this->setNew();
76
        }
77
    }
78
79
    /**
80
     * @param array  $criteria
81
     * @param bool   $asobject
82
     * @param string $sort
83
     * @param string $order
84
     * @param int    $limit
85
     * @param int    $start
86
     *
87
     * @return array
88
     */
89 View Code Duplication
    public function getAllassessment_respostass($criteria = array(), $asobject = false, $sort = 'cod_resposta', $order = 'ASC', $limit = 0, $start = 0)
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...
90
    {
91
        $db          = XoopsDatabaseFactory::getDatabaseConnection();
92
        $ret         = array();
93
        $where_query = '';
94
        if (is_array($criteria) && count($criteria) > 0) {
95
            $where_query = ' WHERE';
96
            foreach ($criteria as $c) {
97
                $where_query .= " $c AND";
98
            }
99
            $where_query = substr($where_query, 0, -4);
100
        } elseif (!is_array($criteria) && $criteria) {
101
            $where_query = ' WHERE ' . $criteria;
102
        }
103
        if (!$asobject) {
104
            $sql    = 'SELECT cod_resposta FROM ' . $db->prefix('assessment_respostas') . "$where_query ORDER BY $sort $order";
105
            $result = $db->query($sql, $limit, $start);
106
            while ($myrow = $db->fetchArray($result)) {
107
                $ret[] = $myrow['assessment_respostas_id'];
108
            }
109
        } else {
110
            $sql    = 'SELECT * FROM ' . $db->prefix('assessment_respostas') . "$where_query ORDER BY $sort $order";
111
            $result = $db->query($sql, $limit, $start);
112
            while ($myrow = $db->fetchArray($result)) {
113
                $ret[] = new assessment_respostas($myrow);
114
            }
115
        }
116
117
        return $ret;
118
    }
119
}
120
121
// -------------------------------------------------------------------------
122
// ------------------assessment_respostas user handler class -------------------
123
// -------------------------------------------------------------------------
124
125
/**
126
 * assessment_respostashandler class.
127
 * This class provides simple mecanisme for assessment_respostas object
128
 */
129
class Xoopsassessment_respostasHandler extends XoopsPersistableObjectHandler
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
130
{
131
    /**
132
     * create a new assessment_respostas
133
     *
134
     * @param bool $isNew flag the new objects as "new"?
135
     *
136
     * @return object assessment_respostas
137
     */
138
    public function &create($isNew = true)
139
    {
140
        $assessment_respostas = new assessment_respostas();
141
        if ($isNew) {
142
            $assessment_respostas->setNew();
143
        } //hack consertando
144
        else {
145
            $assessment_respostas->unsetNew();
146
        }
147
148
        //fim do hack para consertar
149
        return $assessment_respostas;
150
    }
151
152
    /**
153
     * retrieve a assessment_respostas
154
     *
155
     * @param  mixed $id     ID
156
     * @param  array $fields fields to fetch
0 ignored issues
show
Documentation introduced by
Should the type for parameter $fields not be array|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
157
     * @return XoopsObject {@link XoopsObject}
0 ignored issues
show
Documentation introduced by
Should the return type not be false|assessment_respostas?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
158
     */
159 View Code Duplication
    public function get($id = null, $fields = null)
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...
160
    {
161
        $sql = 'SELECT * FROM ' . $this->db->prefix('assessment_respostas') . ' WHERE cod_resposta=' . $id;
162
        if (!$result = $this->db->query($sql)) {
163
            return false;
164
        }
165
        $numrows = $this->db->getRowsNum($result);
166
        if ($numrows == 1) {
167
            $assessment_respostas = new assessment_respostas();
168
            $assessment_respostas->assignVars($this->db->fetchArray($result));
169
170
            return $assessment_respostas;
171
        }
172
173
        return false;
174
    }
175
176
    /**
177
     * insert a new assessment_respostas in the database
178
     *
179
     * @param XoopsObject $assessment_respostas reference to the {@link assessment_respostas} object
180
     * @param bool        $force
181
     *
182
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
183
     */
184 View Code Duplication
    public function insert(XoopsObject $assessment_respostas, $force = 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...
185
    {
186
        global $xoopsConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
187
        if (get_class($assessment_respostas) != 'assessment_respostas') {
188
            return false;
189
        }
190
        if (!$assessment_respostas->isDirty()) {
191
            return true;
192
        }
193
        if (!$assessment_respostas->cleanVars()) {
194
            return false;
195
        }
196
        foreach ($assessment_respostas->cleanVars as $k => $v) {
197
            ${$k} = $v;
198
        }
199
        $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)';
200
        if ($assessment_respostas->isNew()) {
201
            // ajout/modification d'un assessment_respostas
202
            $assessment_respostas = new assessment_respostas();
203
            $format               = 'INSERT INTO %s (cod_resposta, cod_pergunta, titulo, iscerta, data_criacao, data_update, uid_elaboradores, isativa)';
204
            $format               .= 'VALUES (%u, %u, %s, %u, %s, %s, %s, %u)';
205
            $sql                  = sprintf($format, $this->db->prefix('assessment_respostas'), $cod_resposta, $cod_pergunta, $this->db->quoteString($titulo), $iscerta, $now, $now, $this->db->quoteString($uid_elaboradores), $isativa);
0 ignored issues
show
Bug introduced by
The variable $cod_resposta seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Bug introduced by
The variable $cod_pergunta does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $titulo does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $iscerta does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $uid_elaboradores does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $isativa does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
206
            $force                = true;
207
        } else {
208
            $format = 'UPDATE %s SET ';
209
            $format .= 'cod_resposta=%u, cod_pergunta=%u, titulo=%s, iscerta=%u, data_criacao=%s, data_update=%s, uid_elaboradores=%s, isativa=%u';
210
            $format .= ' WHERE cod_resposta = %u';
211
            $sql    = sprintf($format, $this->db->prefix('assessment_respostas'), $cod_resposta, $cod_pergunta, $this->db->quoteString($titulo), $iscerta, $now, $now, $this->db->quoteString($uid_elaboradores), $isativa, $cod_resposta);
0 ignored issues
show
Bug introduced by
The variable $cod_resposta seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
212
        }
213
        if (false != $force) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
214
            $result = $this->db->queryF($sql);
215
        } else {
216
            $result = $this->db->query($sql);
217
        }
218
        if (!$result) {
219
            return false;
220
        }
221
        if (empty($cod_resposta)) {
0 ignored issues
show
Bug introduced by
The variable $cod_resposta seems only to be defined at a later point. As such the call to empty() seems to always evaluate to true.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
222
            $cod_resposta = $this->db->getInsertId();
223
        }
224
        $assessment_respostas->assignVar('cod_resposta', $cod_resposta);
225
226
        return true;
227
    }
228
229
    /**
230
     * delete a assessment_respostas from the database
231
     *
232
     * @param XoopsObject $assessment_respostas reference to the assessment_respostas to delete
233
     * @param bool        $force
234
     *
235
     * @return bool FALSE if failed.
236
     */
237 View Code Duplication
    public function delete(XoopsObject $assessment_respostas, $force = 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...
238
    {
239
        if (get_class($assessment_respostas) != 'assessment_respostas') {
240
            return false;
241
        }
242
        $sql = sprintf('DELETE FROM %s WHERE cod_resposta = %u', $this->db->prefix('assessment_respostas'), $assessment_respostas->getVar('cod_resposta'));
243
        if (false != $force) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
244
            $result = $this->db->queryF($sql);
245
        } else {
246
            $result = $this->db->query($sql);
247
        }
248
        if (!$result) {
249
            return false;
250
        }
251
252
        return true;
253
    }
254
255
    /**
256
     * retrieve assessment_respostass from the database
257
     *
258
     * @param CriteriaElement $criteria  {@link CriteriaElement} conditions to be met
0 ignored issues
show
Documentation introduced by
Should the type for parameter $criteria not be null|CriteriaElement?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
259
     * @param bool            $id_as_key use the UID as key for the array?
260
     *
261
     * @param bool            $as_object
262
     * @return array array of <a href='psi_element://$assessment_respostas'>$assessment_respostas</a> objects
263
     *                                   objects
264
     */
265 View Code Duplication
    public function &getObjects(CriteriaElement $criteria = null, $id_as_key = false, $as_object = true)
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...
266
    {
267
        $ret   = array();
268
        $limit = $start = 0;
269
        $sql   = 'SELECT * FROM ' . $this->db->prefix('assessment_respostas');
270
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
271
            $sql .= ' ' . $criteria->renderWhere();
272
            if ($criteria->getSort() != '') {
273
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
274
            }
275
            $limit = $criteria->getLimit();
276
            $start = $criteria->getStart();
277
        }
278
        $result = $this->db->query($sql, $limit, $start);
279
        if (!$result) {
280
            return $ret;
281
        }
282
        while ($myrow = $this->db->fetchArray($result)) {
283
            $assessment_respostas = new assessment_respostas();
284
            $assessment_respostas->assignVars($myrow);
285
            if (!$id_as_key) {
286
                $ret[] = $assessment_respostas;
287
            } else {
288
                $ret[$myrow['cod_resposta']] = $assessment_respostas;
289
            }
290
            unset($assessment_respostas);
291
        }
292
293
        return $ret;
294
    }
295
296
    /**
297
     * count assessment_respostass matching a condition
298
     *
299
     * @param CriteriaElement $criteria {@link CriteriaElement} to match
0 ignored issues
show
Documentation introduced by
Should the type for parameter $criteria not be null|CriteriaElement?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
300
     *
301
     * @return int count of assessment_perguntass
302
     */
303 View Code Duplication
    public function getCount(CriteriaElement $criteria = null)
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...
304
    {
305
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('assessment_respostas');
306
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
307
            $sql .= ' ' . $criteria->renderWhere();
308
        }
309
        $result = $this->db->query($sql);
310
        if (!$result) {
311
            return 0;
312
        }
313
        list($count) = $this->db->fetchRow($result);
314
315
        return $count;
316
    }
317
318
    /**
319
     * delete assessment_respostass matching a set of conditions
320
     *
321
     * @param CriteriaElement $criteria {@link CriteriaElement}
0 ignored issues
show
Documentation introduced by
Should the type for parameter $criteria not be null|CriteriaElement?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
322
     *
323
     * @param bool            $force
324
     * @param bool            $asObject
325
     * @return bool FALSE if deletion failed
326
     */
327 View Code Duplication
    public function deleteAll(CriteriaElement $criteria = null, $force = true, $asObject = 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...
328
    {
329
        $sql = 'DELETE FROM ' . $this->db->prefix('assessment_respostas');
330
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
331
            $sql .= ' ' . $criteria->renderWhere();
332
        }
333
        if (!$result = $this->db->query($sql)) {
334
            return false;
335
        }
336
337
        return true;
338
    }
339
}
340