Completed
Push — work-fleets ( e0e753...5ee2f8 )
by SuperNova.WS
05:10
created

DBStaticRecord::getMaxId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 5
rs 9.4285
1
<?php
2
3
class DBStaticRecord {
4
5
  public static $_table = '_table';
6
  public static $_idField = 'id';
7
8
  /**
9
   * Converts fields array to string
10
   * Scalar values or empty arrays would be converted to wildcard '*"
11
   * null array members would be converted to field NULL
12
   * All other values would be enquoted by `
13
   *
14
   * @param array $fieldList
15
   *
16
   * @return string
17
   */
18
  protected static function fieldsToString($fieldList) {
19
    $fieldList = HelperArray::makeArray($fieldList);
20
21
    $result = array();
22
    if (!empty($fieldList)) {
23
      foreach ($fieldList as $fieldName) {
24
        switch (true) {
25
          case is_int($fieldName):
26
            $result[] = $fieldName;
27
          break;
28
          case is_null($fieldName):
29
            $result[] = 'NULL';
30
          break;
31
          default:
32
            $result[] = '`' . (string)$fieldName . '`';
33
        }
34
      }
35
    } else {
36
      $result = array('*');
37
    }
38
39
    $result = implode(',', $result);
40
41
    return $result;
42
  }
43
44
  /**
45
   * @param string $where
46
   * @param mixed  $fieldList
47
   *     Field list is scalar it would be converted to array and used as field name
48
   * @param bool   $for_update
49
   * @param bool   $returnFirst
50
   *
51
   * @return array|null
52
   */
53
  protected static function getRecordList($where = '', $fieldList = array(), $for_update = false, $returnFirst = false) {
54
    $fieldList = static::fieldsToString($fieldList);
55
56
    $user_record = null;
57
    if (!empty($fieldList)) {
58
      $user_record = doquery(
59
        (
60
          "SELECT {$fieldList}" .
61
          " FROM {{" . self::$_table . "}}" .
62
          (!empty($where) ? " WHERE {$where}" : '') .
63
          (!empty($for_update) ? " FOR UPDATE" : '') .
64
          ($returnFirst ? ' LIMIT 1' : '')
65
        ),
66
        $returnFirst
0 ignored issues
show
Documentation introduced by
$returnFirst is of type boolean, but the function expects a string.

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...
67
      );
68
    }
69
70
    return !empty($user_record) ? $user_record : null;
71
  }
72
73
  /**
74
   * @param string $where
75
   * @param mixed  $fieldList
76
   *     Field list can be scalar - it would be converted to array and used as field name
77
   * @param bool   $for_update
78
   *
79
   * @return array|null
80
   *
81
   * @see static::getRecordList
82
   */
83
  protected static function getRecord($where = '', $fieldList = array(), $for_update = false) {
84
    return static::getRecordList($where, $fieldList, $for_update, true);
85
  }
86
87
  /**
88
   * Get maximum ID from table
89
   *
90
   * @return int
91
   */
92
  public static function getMaxId() {
93
    $maxId = classSupernova::$db->doquery("SELECT MAX(`" . static::$_idField . "`) AS `maxId` FROM `{{" . static::$_table . "}}`", true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

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...
94
95
    return !empty($maxId['maxId']) ? $maxId['maxId'] : 0;
96
  }
97
98
  /**
99
   * @param int|string $user_id
100
   * @param array      $fieldList
101
   * @param bool       $for_update
102
   *
103
   * @return array|null
104
   */
105
  public static function getRecordById($user_id, $fieldList = array(), $for_update = false) {
106
    return static::getRecord(static::$_idField . ' = ' . $user_id, $fieldList, $for_update);
107
  }
108
109
110
  /**
111
   * @param array $idList
112
   *
113
   * @return mysqli_result|null
114
   */
115
  public static function queryExistsIdInList($idList) {
116
    $query = null;
117
    if (!empty($idList)) {
118
      $query = doquery("SELECT `" . static::$_idField . "` FROM `{{" . static::$_table . "}}` WHERE `" . static::$_idField . "` IN (" . implode(',', $idList) . ")");
119
    }
120
121
    return !empty($query) ? $query : null;
122
  }
123
124
125
  /**
126
   * @param string $idList
127
   *
128
   * @return string
129
   */
130
  public static function filterIdListStringRepack($idList) {
131
    $idList = HelperArray::stringToArrayFilterEmpty($idList);
132
133
    $result = array();
134
    if (!empty($idList)) {
135
      $query = static::queryExistsIdInList($idList);
136
      while ($row = db_fetch($query)) {
137
        $result[] = $row[static::$_idField];
138
      }
139
    }
140
141
    return implode(',', $result);
142
  }
143
144
}
145