Completed
Push — work-fleets ( 3cd948...da4c88 )
by SuperNova.WS
07:01
created

DBStaticRecord::buildDBQ()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
ccs 0
cts 2
cp 0
crap 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
   * @return DbQueryConstructor
27
   */
28
  public static function buildDBQ() {
29
    return DbQueryConstructor::build(static::$dbStatic, get_called_class());
30
  }
31
32
  /**
33
   * Get maximum ID from table
34
   *
35
   * @return int
36
   */
37
  public static function getMaxId() {
38
    $stmt = static::buildDBQ()->fieldMax(static::$_idField);
39
40
    return idval(static::$dbStatic->doStmtSelectValue($stmt));
41
  }
42
43
  /**
44
   * @param int|string  $recordId
45
   * @param mixed|array $fieldList
46
   * @param bool   $forUpdate
47
   *
48
   * @return array
49
   */
50
  // TODO - protected. Code doesn't know about fields
51
  public static function getRecordById($recordId, $fieldList = '*', $forUpdate = false) {
52
    $stmt =
53
      static::buildDBQ()
54
        ->fields($fieldList)
55
        ->where(static::$_idField . '=' . $recordId);
0 ignored issues
show
Documentation introduced by
static::$_idField . '=' . $recordId 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...
56
57
    if($forUpdate) {
58
      $stmt->setForUpdate();
59
    }
60
61
    $result = static::$dbStatic->doStmtSelectRow($stmt);
62
63
    return $result;
64
  }
65
66
  /**
67
   * @param array $idList
68
   *
69
   * @return DbResultIterator
70
   */
71
  public static function queryExistsIdInList($idList) {
72
    if (!empty($idList) && is_array($idList)) {
73
      $query = static::$dbStatic->doStmtSelectIterator(
74
        static::buildDBQ()
75
          ->fields(static::$_idField)
76
          ->where(array("`" . static::$_idField . "` IN (" . implode(',', $idList) . ")"))
77
      );
78
    } else {
79
      $query = new DbEmptyIterator();
80
    }
81
82
    return $query;
83
  }
84
85
86
  /**
87
   * Filter list of ID by only existed IDs in table
88
   *
89
   * @param string $idList
90
   *
91
   * @return string
92
   */
93
  public static function filterIdListStringRepack($idList) {
94
    // TODO - remove HelperArray caller
95
    $idList = HelperArray::stringToArrayFilterEmpty($idList);
96
97
    $result = array();
98
    if (!empty($idList)) {
99
      foreach(static::queryExistsIdInList($idList) as $row) {
100
        $result[] = $row[static::$_idField];
101
      }
102
    }
103
104
    // TODO - remove implode
105
    return implode(',', $result);
106
  }
107
108
  /**
109
   *
110
   */
111
  public static function lockAllRecords() {
112
    static::$dbStatic->doStmtLockAll(static::buildDBQ());
113
  }
114
115
}
116
117
DBStaticRecord::_init();
118