Completed
Push — master ( 14f1c2...1cc6cb )
by Lars
02:38
created

Helper   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 15
c 1
b 0
f 1
lcom 0
cbo 1
dl 0
loc 114
ccs 55
cts 55
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getDbFields() 0 31 6
C copyTableRow() 0 59 9
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
   * return all db-fields from a table
14
   *
15
   * @param string $database
0 ignored issues
show
Bug introduced by
There is no parameter named $database. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
16
   * @param string $table
17
   * @param bool   $useStaticCache
18
   *
19
   * @return array
20
   */
21 1
  public static function getDbFields($table, $useStaticCache = true)
22
  {
23 1
    static $dbFieldsCache = array();
24
25
    // use the cache
26
    if (
27
        $useStaticCache === true
28 1
        &&
29 1
        isset($dbFieldsCache[$table])
30 1
    ) {
31 1
      return $dbFieldsCache[$table];
32
    }
33
34
    // init
35 1
    $dbFields = array();
36 1
    $db = DB::getInstance();
37
38 1
    $sql = "SHOW COLUMNS FROM `" . $db->escape($table) . "`";
39 1
    $result = $db->query($sql);
40
41 1
    if ($result && $result->num_rows > 0) {
42 1
      foreach ($result->fetchAllArray() as $tmpResult) {
43 1
        $dbFields[] = $tmpResult['Field'];
44 1
      }
45 1
    }
46
47
    // add to static cache
48 1
    $dbFieldsCache[$table] = $dbFields;
49
50 1
    return $dbFields;
51
  }
52
53
  /**
54
   * copy row within a DB table and making updates to columns
55
   *
56
   * @param string $table
57
   * @param array  $whereArray
58
   * @param array  $updateArray
59
   * @param array  $ignoreArray
60
   *
61
   * @return bool|int "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br />
62
   *                   "false" on error
63
   */
64 1
  public static function copyTableRow($table, array $whereArray, array $updateArray = array(), array $ignoreArray = array())
65
  {
66
    // init
67 1
    $whereSQL = '';
68 1
    $db = DB::getInstance();
69 1
    $return = false;
70
71 1
    $table = $db->escape($table);
72
73 1
    foreach ($whereArray as $key => $value) {
74 1
      $whereSQL = ' AND ' . $db->escape($key) . ' = ' . $db->escape($value);
75 1
    }
76
77
    // get the row
78 1
    $query = "SELECT * FROM " . $table . "
79
      WHERE 1 = 1
80 1
      " . $whereSQL . "
81 1
    ";
82 1
    $result = $db->query($query);
83
84
    // make sure the row exists
85 1
    if ($result->num_rows > 0) {
86
87 1
      foreach ($result->fetchAllArray() as $tmpArray) {
88
89
        // re-build a new DB query and ignore some field-names
90 1
        $bindings = array();
91 1
        $insert_keys = '';
92 1
        $insert_values = '';
93
94 1
        foreach ($tmpArray as $fieldName => $value) {
95
96 1
          if (!in_array($fieldName, $ignoreArray, true)) {
97 1
            if (array_key_exists($fieldName, $updateArray)) {
98
99 1
              if ($updateArray[$fieldName] || $updateArray[$fieldName] == 0) {
100 1
                $insert_keys .= ',' . $fieldName;
101 1
                $insert_values .= ',?';
102 1
                $bindings[] = $updateArray[$fieldName];
103 1
              }
104
105 1
            } else {
106 1
              $insert_keys .= ',' . $fieldName;
107 1
              $insert_values .= ',?';
108 1
              $bindings[] = $value;
109
            }
110 1
          }
111 1
        }
112
113
        // insert the "copied" row
114 1
        $new_query = "INSERT INTO `" . $table . "` (" . ltrim($insert_keys, ',') . ")
115 1
          VALUES (" . ltrim($insert_values, ',') . ")
116 1
        ";
117 1
        $return = $db->query($new_query, $bindings);
118 1
      }
119 1
    }
120
121 1
    return $return;
122
  }
123
}
124