Completed
Push — master ( a637a0...44fe92 )
by Lars
04:29 queued 01:28
created

Helper::copyTableRow()   C

Complexity

Conditions 9
Paths 26

Size

Total Lines 66
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 9.1274

Importance

Changes 4
Bugs 1 Features 3
Metric Value
c 4
b 1
f 3
dl 0
loc 66
ccs 38
cts 43
cp 0.8837
rs 6.4099
cc 9
eloc 35
nc 26
nop 5
crap 9.1274

How to fix   Long Method   

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 29
  public static function isMysqlndIsUsed()
18
  {
19 29
    static $_mysqlnd_is_used = null;
20
21 29
    if ($_mysqlnd_is_used === null) {
22 1
      $_mysqlnd_is_used = (extension_loaded('mysqlnd') && function_exists('mysqli_fetch_all'));
23 1
    }
24
25 29
    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 static cache
42
    if (
43
        $useStaticCache === true
44 1
        &&
45 1
        isset($dbFieldsCache[$table])
46 1
    ) {
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 1
    }
56
57 1
    $debug = new Debug($db);
58 1
    if ($table === '') {
59
      $debug->displayError('invalid table name');
60
61
      return array();
62
    }
63
64 1
    $sql = 'SHOW COLUMNS FROM ' . $db->quote_string($table);
65 1
    $result = $db->query($sql);
66
67 1
    if ($result && $result->num_rows > 0) {
68 1
      foreach ($result->fetchAllArray() as $tmpResult) {
69 1
        $dbFields[] = $tmpResult['Field'];
70 1
      }
71 1
    }
72
73
    // add to static cache
74 1
    $dbFieldsCache[$table] = $dbFields;
75
76 1
    return $dbFields;
77
  }
78
79
  /**
80
   * Copy row within a DB table and making updates to the columns.
81
   *
82
   * @param string  $table
83
   * @param array   $whereArray
84
   * @param array   $updateArray
85
   * @param array   $ignoreArray
86
   * @param DB|null $db           <p>Use <strong>null</strong> 
87
   *
88
   * @return bool|int "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br />
89
   *                   "false" on error
90
   */
91 1
  public static function copyTableRow($table, array $whereArray, array $updateArray = array(), array $ignoreArray = array(), DB $db = null)
92
  {
93
    // init
94 1
    $table = trim($table);
95
96 1
    if ($db === null) {
97 1
      $db = DB::getInstance();
98 1
    }
99
100 1
    $debug = new Debug($db);
101 1
    if ($table === '') {
102
      $debug->displayError('invalid table name');
103
104
      return false;
105
    }
106
107 1
    $whereSQL = '';
108 1
    foreach ($whereArray as $key => $value) {
109 1
      $whereSQL .= ' AND ' . $db->escape($key) . ' = ' . $db->escape($value);
110 1
    }
111
112
    // get the row
113 1
    $query = 'SELECT * FROM ' . $db->quote_string($table) . '
114
      WHERE 1 = 1
115 1
      ' . $whereSQL . '
116 1
    ';
117 1
    $result = $db->query($query);
118
119
    // make sure the row exists
120 1
    if ($result->num_rows > 0) {
121
122 1
      foreach ($result->fetchAllArray() as $tmpArray) {
123
124
        // re-build a new DB query and ignore some field-names
125 1
        $bindings = array();
126 1
        $insert_keys = '';
127 1
        $insert_values = '';
128
129 1
        foreach ($tmpArray as $fieldName => $value) {
130
131 1
          if (!in_array($fieldName, $ignoreArray, true)) {
132 1
            if (array_key_exists($fieldName, $updateArray)) {
133 1
              $insert_keys .= ',' . $fieldName;
134 1
              $insert_values .= ',?';
135 1
              $bindings[] = $db->escape($updateArray[$fieldName]);
136 1
            } else {
137 1
              $insert_keys .= ',' . $fieldName;
138 1
              $insert_values .= ',?';
139 1
              $bindings[] = $db->escape($value);
140
            }
141 1
          }
142 1
        }
143
144 1
        $insert_keys = ltrim($insert_keys, ',');
145 1
        $insert_values = ltrim($insert_values, ',');
146
147
        // insert the "copied" row
148 1
        $new_query = 'INSERT INTO ' . $db->quote_string($table) . ' (' . $insert_keys . ')
149 1
          VALUES (' . $insert_values . ')
150 1
        ';
151 1
        return $db->query($new_query, $bindings);
152
      }
153
    }
154
155
    return false;
156
  }
157
}
158