Completed
Push — work-fleets ( 13a233...1735a9 )
by SuperNova.WS
05:19
created

DBStaticRecord::_init()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 2
nc 6
nop 1
dl 0
loc 3
rs 9.2
c 1
b 0
f 0
ccs 0
cts 3
cp 0
crap 20
1
<?php
2
3
class DBStaticRecord {
4
5
  public static $_table = '_table';
6
  public static $_idField = 'id';
7
8
  /**
9
   * @var db_mysql $dbStatic
10
   */
11
  protected static $dbStatic;
12
13
  /**
14
   * DBStaticRecord constructor.
15
   *
16
   * @param db_mysql|null $db
17
   */
18
  public static function _init($db = null) {
19
    self::$dbStatic = (!empty($db) && $db instanceof db_mysql) || !class_exists('classSupernova', false) ? $db : classSupernova::$db;
20
  }
21
22
  /**
23
   * Converts fields array to string
24
   * Scalar values or empty arrays would be converted to wildcard '*'
25
   * null array members would be converted to field NULL
26
   * All other values would be enquoted by `
27
   *
28
   * @param array $fieldList
29
   *
30
   * @return string
31
   *
32
   * @throws ExceptionDBFieldEmpty
33
   */
34 2
  protected static function fieldsToString($fieldList) {
35 2
    $fieldList = HelperArray::makeArray($fieldList);
36
37 2
    $result = array();
38 2
    if (!empty($fieldList)) {
39 2
      foreach ($fieldList as $fieldName) {
40
        switch (true) {
41 2
          case is_int($fieldName):
42 1
            $result[] = $fieldName;
43 1
          break;
44 2
          case is_null($fieldName):
45 1
            $result[] = 'NULL';
46 1
          break;
47 2
          default:
48 2
            $string = (string)$fieldName;
49 2
            if($string == '') {
50 1
              throw new ExceptionDBFieldEmpty();
51
            }
52 1
            $result[] = '`' . $string . '`';
53 2
        }
54 1
      }
55 1
    } else {
56 1
      $result = array('*');
57
    }
58
59 1
    $result = implode(',', $result);
60
61 1
    return $result;
62
  }
63
64
  /**
65
   * @param string $where
66
   * @param mixed  $fieldList
67
   *     Field list is scalar it would be converted to array and used as field name
68
   * @param bool   $for_update
69
   * @param bool   $returnFirst
70
   *
71
   * @return array|null
72
   */
73
  protected static function getRecordList($where = '', $fieldList = array(), $for_update = false, $returnFirst = false) {
74
    $fieldList = static::fieldsToString($fieldList);
75
76
    $user_record = null;
77
    if (!empty($fieldList)) {
78
      $user_record = self::$dbStatic->doquery(
79
        (
80
          "SELECT {$fieldList}" .
81
          " FROM {{" . self::$_table . "}}" .
82
          (!empty($where) ? " WHERE {$where}" : '') .
83
          (!empty($for_update) ? " FOR UPDATE" : '') .
84
          ($returnFirst ? ' LIMIT 1' : '')
85
        ),
86
        $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...
87
      );
88
    }
89
90
    return !empty($user_record) ? $user_record : null;
91
  }
92
93
  /**
94
   * @param string $where
95
   * @param mixed  $fieldList
96
   *     Field list can be scalar - it would be converted to array and used as field name
97
   * @param bool   $for_update
98
   *
99
   * @return array|null
100
   *
101
   * @see static::getRecordList
102
   */
103
  protected static function getRecord($where = '', $fieldList = array(), $for_update = false) {
104
    return static::getRecordList($where, $fieldList, $for_update, true);
105
  }
106
107
  /**
108
   * Get maximum ID from table
109
   *
110
   * @return int
111
   */
112
  public static function getMaxId() {
113
    $maxId = self::$dbStatic->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...
114
115
    return !empty($maxId['maxId']) ? $maxId['maxId'] : 0;
116
  }
117
118
  /**
119
   * @param int|string $user_id
120
   * @param array      $fieldList
121
   * @param bool       $for_update
122
   *
123
   * @return array|null
124
   */
125
  public static function getRecordById($user_id, $fieldList = array(), $for_update = false) {
126
    return static::getRecord(static::$_idField . ' = ' . $user_id, $fieldList, $for_update);
127
  }
128
129
130
  /**
131
   * @param array $idList
132
   *
133
   * @return mysqli_result|null
134
   */
135
  public static function queryExistsIdInList($idList) {
136
    $query = null;
137
    if (!empty($idList)) {
138
      $query = self::$dbStatic->doquery("SELECT `" . static::$_idField . "` FROM `{{" . static::$_table . "}}` WHERE `" . static::$_idField . "` IN (" . implode(',', $idList) . ")");
139
    }
140
141
    return !empty($query) ? $query : null;
142
  }
143
144
145
  /**
146
   * @param string $idList
147
   *
148
   * @return string
149
   */
150
  public static function filterIdListStringRepack($idList) {
151
    $idList = HelperArray::stringToArrayFilterEmpty($idList);
152
153
    $result = array();
154
    if (!empty($idList)) {
155
      $query = static::queryExistsIdInList($idList);
156
      while ($row = db_fetch($query)) {
157
        $result[] = $row[static::$_idField];
158
      }
159
    }
160
161
    return implode(',', $result);
162
  }
163
164
}
165
166
DBStaticRecord::_init();
167