Completed
Push — work-fleets ( a5ca82...ab0738 )
by SuperNova.WS
05:46
created

DBStaticRecord::fieldsToString()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 21
nc 3
nop 1
dl 0
loc 29
rs 8.439
c 2
b 0
f 0
ccs 22
cts 22
cp 1
crap 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A DBStaticRecord::getRecord() 0 10 1
A DBStaticRecord::getMaxId() 0 5 2
1
<?php
2
3
/**
4
 * Class DBStaticRecord
5
 */
6
class DBStaticRecord {
7
8
  public static $_table = '_table';
9
  public static $_idField = 'id';
10
11
  /**
12
   * @var db_mysql $dbStatic
13
   */
14
  protected static $dbStatic;
15
16
  /**
17
   * DBStaticRecord constructor.
18
   *
19
   * @param db_mysql|null $db
20
   */
21
  public static function _init($db = null) {
22
    static::$dbStatic = (!empty($db) && $db instanceof db_mysql) || !class_exists('classSupernova', false) ? $db : classSupernova::$db;
23
  }
24
25
  /**
26
   * @param array       $where
27
   * @param mixed|array $fieldList
28
   *     Field list can be scalar - it would be converted to array and used as field name
29
   * @param bool        $for_update
30
   *
31
   * @return array|null
32
   *
33
   * @see static::getRecordList
34
   */
35
  protected static function getRecord($where = array(), $fieldList = '*', $for_update = false) {
0 ignored issues
show
Unused Code introduced by
The parameter $for_update is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
    $result = static::$dbStatic->fetchOne(
37
      DbSqlStatement::build(null, get_called_class())
38
        ->select()
39
        ->fields($fieldList)
40
        ->where($where)
41
    );
42
43
    return $result;
44
  }
45
46
  /**
47
   * Get maximum ID from table
48
   *
49
   * @return int
50
   */
51
  public static function getMaxId() {
52
    $maxId = static::getRecord(array(), new DbSqlLiteral('MAX(id) AS maxId'));
53
54
    return !empty($maxId['maxId']) ? $maxId['maxId'] : 0;
55
  }
56
57
  /**
58
   * @param int|string  $recordId
59
   * @param mixed|array $fieldList
60
   * @param bool        $forUpdate
61
   *
62
   * @return array|null
63
   */
64
  public static function getRecordById($recordId, $fieldList = '*', $forUpdate = false) {
65
//    return static::getRecord(array(static::$_idField => $recordId), $fieldList, $forUpdate);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
66
    return static::getRecord(array(static::$_idField . '=' . $recordId), $fieldList, $forUpdate);
67
  }
68
69
70
  /**
71
   * @param array $idList
72
   *
73
   * @return mysqli_result|null
74
   */
75
  /**
76
   * @param array $idList
77
   *
78
   * @return mysqli_result|null
79
   */
80
  public static function queryExistsIdInList($idList) {
81
    $query = null;
82
    if (!empty($idList) && is_array($idList)) {
83
      $query = static::$dbStatic->execute(
84
        DbSqlStatement::build(null, get_called_class())
85
          ->select()
86
          ->fields(static::$_idField)
0 ignored issues
show
Documentation introduced by
static::$_idField is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
87
          ->where(array("`" . static::$_idField . "` IN (" . implode(',', $idList) . ")"))
88
      );
89
    }
90
91
    return !empty($query) ? $query : null;
92
  }
93
94
95
  /**
96
   * @param string $idList
97
   *
98
   * @return string
99
   */
100
  public static function filterIdListStringRepack($idList) {
101
    // TODO - remove HelperArray caller
102
    $idList = HelperArray::stringToArrayFilterEmpty($idList);
103
104
    $result = array();
105
    if (!empty($idList)) {
106
      $query = static::queryExistsIdInList($idList);
107
      while ($row = db_fetch($query)) {
108
        $result[] = $row[static::$_idField];
109
      }
110
    }
111
112
    // TODO - remove implode
113
    return implode(',', $result);
114
  }
115
116
}
117
118
DBStaticRecord::_init();
119