Completed
Push — work-fleets ( fb09a7...a5ca82 )
by SuperNova.WS
05:51
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
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
    static::$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 = static::$dbStatic->doquery(
79
        (
80
          "SELECT {$fieldList}" .
81
          " FROM {{" . static::$_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 array       $where
95
   * @param mixed|array $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 = 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...
104
    $maxId = static::$dbStatic->fetchOne(
105
      DbSqlStatement::build(null, get_called_class())
106
        ->select()
107
        ->fields($fieldList)
108
        ->where($where)
109
        ->fetchOne()
110
    );
111
112
    return $maxId;
113
  }
114
115
  /**
116
   * Get maximum ID from table
117
   *
118
   * @return int
119
   */
120
  public static function getMaxId() {
121
    $maxId = static::$dbStatic->fetchOne(
122
      DbSqlStatement::build(null, get_called_class())
123
        ->select()
124
        ->fields(new DbSqlLiteral('MAX(id) AS maxId'))
0 ignored issues
show
Documentation introduced by
new \DbSqlLiteral('MAX(id) AS maxId') is of type object<DbSqlLiteral>, 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...
125
        ->fetchOne()
126
    );
127
128
    return !empty($maxId['maxId']) ? $maxId['maxId'] : 0;
129
  }
130
131
  /**
132
   * @param int|string  $recordId
133
   * @param mixed|array $fieldList
134
   * @param bool        $forUpdate
135
   *
136
   * @return array|null
137
   */
138
  public static function getRecordById($recordId, $fieldList = '*', $forUpdate = false) {
139
//    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...
140
    return static::getRecord(array(static::$_idField . '=' . $recordId), $fieldList, $forUpdate);
141
  }
142
143
144
  /**
145
   * @param array $idList
146
   *
147
   * @return mysqli_result|null
148
   */
149
  /**
150
   * @param array $idList
151
   *
152
   * @return mysqli_result|null
153
   */
154
  public static function queryExistsIdInList($idList) {
155
    $query = null;
156
    if (!empty($idList) && is_array($idList)) {
157
      $query = static::$dbStatic->execute(
158
        DbSqlStatement::build(null, get_called_class())
159
          ->select()
160
          ->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...
161
          ->where(array("`" . static::$_idField . "` IN (" . implode(',', $idList) . ")"))
162
      );
163
    }
164
165
    return !empty($query) ? $query : null;
166
  }
167
168
169
  /**
170
   * @param string $idList
171
   *
172
   * @return string
173
   */
174
  public static function filterIdListStringRepack($idList) {
175
    // TODO - remove HelperArray caller
176
    $idList = HelperArray::stringToArrayFilterEmpty($idList);
177
178
    $result = array();
179
    if (!empty($idList)) {
180
      $query = static::queryExistsIdInList($idList);
181
      while ($row = db_fetch($query)) {
182
        $result[] = $row[static::$_idField];
183
      }
184
    }
185
186
    // TODO - remove implode
187
    return implode(',', $result);
188
  }
189
190
}
191
192
DBStaticRecord::_init();
193