Completed
Push — work-fleets ( ab0738...1c4183 )
by SuperNova.WS
05:52
created

DBStaticRecord::buildSelect()   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
ccs 0
cts 2
cp 0
crap 2
rs 10
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 DbSqlStatement
27
   */
28
  public static function buildSelect() {
29
    return DbSqlStatement::build(null, get_called_class())->select();
30
  }
31
32
  /**
33
   * @param array       $where
34
   * @param mixed|array $fieldList
35
   *     Field list can be scalar - it would be converted to array and used as field name
36
   * @param bool        $for_update
37
   *
38
   * @return array|null
39
   *
40
   * @see static::getRecordList
41
   */
42
  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...
43
    $result = static::$dbStatic->fetchOne(
44
      static::buildSelect()
45
        ->fields($fieldList)
46
        ->where($where)
47
    );
48
49
    return $result;
50
  }
51
52
  /**
53
   * Get maximum ID from table
54
   *
55
   * @return int
56
   */
57
  public static function getMaxId() {
58
    $maxId = static::getRecord(array(), new DbSqlLiteral('MAX(id) AS maxId'));
59
60
    return !empty($maxId['maxId']) ? $maxId['maxId'] : 0;
61
  }
62
63
  /**
64
   * @param int|string  $recordId
65
   * @param mixed|array $fieldList
66
   * @param bool        $forUpdate
67
   *
68
   * @return array|null
69
   */
70
  public static function getRecordById($recordId, $fieldList = '*', $forUpdate = false) {
71
//    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...
72
    return static::getRecord(array(static::$_idField . '=' . $recordId), $fieldList, $forUpdate);
73
  }
74
75
76
  /**
77
   * @param array $idList
78
   *
79
   * @return mysqli_result|null
80
   */
81
  /**
82
   * @param array $idList
83
   *
84
   * @return mysqli_result|null
85
   */
86
  public static function queryExistsIdInList($idList) {
87
    $query = null;
88
    if (!empty($idList) && is_array($idList)) {
89
      $query = static::$dbStatic->execute(
90
        static::buildSelect()
91
          ->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...
92
          ->where(array("`" . static::$_idField . "` IN (" . implode(',', $idList) . ")"))
93
      );
94
    }
95
96
    return !empty($query) ? $query : null;
97
  }
98
99
100
  /**
101
   * @param string $idList
102
   *
103
   * @return string
104
   */
105
  public static function filterIdListStringRepack($idList) {
106
    // TODO - remove HelperArray caller
107
    $idList = HelperArray::stringToArrayFilterEmpty($idList);
108
109
    $result = array();
110
    if (!empty($idList)) {
111
      $query = static::queryExistsIdInList($idList);
112
      while ($row = db_fetch($query)) {
113
        $result[] = $row[static::$_idField];
114
      }
115
    }
116
117
    // TODO - remove implode
118
    return implode(',', $result);
119
  }
120
121
}
122
123
DBStaticRecord::_init();
124