Completed
Push — master ( ac25c9...83f604 )
by Lars
03:30
created

Helper::copyTableRow()   C

Complexity

Conditions 10
Paths 8

Size

Total Lines 62
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 10

Importance

Changes 3
Bugs 1 Features 2
Metric Value
c 3
b 1
f 2
dl 0
loc 62
ccs 32
cts 32
cp 1
rs 6.4192
cc 10
eloc 31
nc 8
nop 5
crap 10

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace voku\db;
4
5
/**
6
 * Helper: this handles extra functions that use the "DB"-Class
7
 *
8
 * @package   voku\db
9
 */
10
class Helper
11
{
12
  /**
13
   * Check if "mysqlnd"-driver is used.
14
   *
15
   * @return bool
16
   */
17 28
  public static function isMysqlndIsUsed()
18
  {
19 28
    static $_mysqlnd_is_used = null;
20
21 28
    if ($_mysqlnd_is_used === null) {
22 1
      $_mysqlnd_is_used = (extension_loaded('mysqlnd') && function_exists('mysqli_fetch_all'));
23
    }
24
25 28
    return $_mysqlnd_is_used;
26
  }
27
28
  /**
29
   * return all db-fields from a table
30
   *
31
   * @param string  $table
32
   * @param bool    $useStaticCache
33
   * @param DB|null $db
34
   *
35
   * @return array
36
   */
37 1
  public static function getDbFields($table, $useStaticCache = true, DB $db = null)
38
  {
39 1
    static $dbFieldsCache = array();
40
41
    // use the cache
42
    if (
43 1
        $useStaticCache === true
44
        &&
45 1
        isset($dbFieldsCache[$table])
46
    ) {
47 1
      return $dbFieldsCache[$table];
48
    }
49
50
    // init
51 1
    $dbFields = array();
52
53 1
    if ($db === null) {
54 1
      $db = DB::getInstance();
55
    }
56
57 1
    $sql = 'SHOW COLUMNS FROM `' . $db->escape($table) . '`';
58 1
    $result = $db->query($sql);
59
60 1
    if ($result && $result->num_rows > 0) {
61 1
      foreach ($result->fetchAllArray() as $tmpResult) {
62 1
        $dbFields[] = $tmpResult['Field'];
63
      }
64
    }
65
66
    // add to static cache
67 1
    $dbFieldsCache[$table] = $dbFields;
68
69 1
    return $dbFields;
70
  }
71
72
  /**
73
   * copy row within a DB table and making updates to columns
74
   *
75
   * @param string  $table
76
   * @param array   $whereArray
77
   * @param array   $updateArray
78
   * @param array   $ignoreArray
79
   * @param DB|null $db
80
   *
81
   * @return bool|int "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br />
82
   *                   "false" on error
83
   */
84 1
  public static function copyTableRow($table, array $whereArray, array $updateArray = array(), array $ignoreArray = array(), DB $db = null)
85
  {
86
    // init
87 1
    $whereSQL = '';
88 1
    $return = false;
89
90 1
    if ($db === null) {
91 1
      $db = DB::getInstance();
92
    }
93
94 1
    $table = $db->escape($table);
95
96 1
    foreach ($whereArray as $key => $value) {
97 1
      $whereSQL = ' AND ' . $db->escape($key) . ' = ' . $db->escape($value);
98
    }
99
100
    // get the row
101 1
    $query = 'SELECT * FROM ' . $table . '
102
      WHERE 1 = 1
103 1
      ' . $whereSQL . '
104 1
    ';
105 1
    $result = $db->query($query);
106
107
    // make sure the row exists
108 1
    if ($result->num_rows > 0) {
109
110 1
      foreach ($result->fetchAllArray() as $tmpArray) {
111
112
        // re-build a new DB query and ignore some field-names
113 1
        $bindings = array();
114 1
        $insert_keys = '';
115 1
        $insert_values = '';
116
117 1
        foreach ($tmpArray as $fieldName => $value) {
118
119 1
          if (!in_array($fieldName, $ignoreArray, true)) {
120 1
            if (array_key_exists($fieldName, $updateArray)) {
121
122 1
              if ($updateArray[$fieldName] || $updateArray[$fieldName] == 0) {
123 1
                $insert_keys .= ',' . $fieldName;
124 1
                $insert_values .= ',?';
125 1
                $bindings[] = $updateArray[$fieldName];
126
              }
127
128
            } else {
129 1
              $insert_keys .= ',' . $fieldName;
130 1
              $insert_values .= ',?';
131 1
              $bindings[] = $value;
132
            }
133
          }
134
        }
135
136
        // insert the "copied" row
137 1
        $new_query = 'INSERT INTO `' . $table . '` (' . ltrim($insert_keys, ',') . ')
138 1
          VALUES (' . ltrim($insert_values, ',') . ')
139 1
        ';
140 1
        $return = $db->query($new_query, $bindings);
141
      }
142
    }
143
144 1
    return $return;
145
  }
146
}
147