Completed
Push — master ( 3b2975...5480a6 )
by Dimas
15:02
created

schema::interpolateQuery()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 18
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DB;
4
5
use PDO as GlobalPDO;
6
7
class schema
8
{
9
  /**
10
   * Get ENUM or SET values.
11
   *
12
   * @return array
13
   */
14
  public static function get_enumset_values(\DB\pdo $pdo, string $table, string $field)
15
  {
16
    $type = $pdo->query("SHOW COLUMNS FROM {$table} WHERE Field = '{$field}'")->row_array()['Type'];
17
    if (preg_match("/^enum\(\'(.*)\'\)$/", $type, $matches)) {
18
      $enum = explode("','", $matches[1]);
19
20
      return $enum;
21
    } elseif (preg_match("/^set\(\'(.*)\'\)$/", $type, $matches)) {
22
      $set = explode("','", $matches[1]);
23
24
      return $set;
25
    }
26
27
    return [];
28
  }
29
30
  /**
31
   * Modify ENUM or SET values.
32
   *
33
   * @return array
34
   */
35
  public static function modify_enumset_values(\DB\pdo $pdo, string $table, string $field, array $newData)
0 ignored issues
show
Unused Code introduced by
The parameter $table is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

35
  public static function modify_enumset_values(\DB\pdo $pdo, /** @scrutinizer ignore-unused */ string $table, string $field, array $newData)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $field is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

35
  public static function modify_enumset_values(\DB\pdo $pdo, string $table, /** @scrutinizer ignore-unused */ string $field, array $newData)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
  {
37
    //$sql = "ALTER TABLE `$table` MODIFY COLUMN `$field` SET(:value) NOT NULL";
38
    $sql = "SELECT table_name FROM information_schema.tables
39
    WHERE table_schema = 'userdata';";
40
    $prepare = $pdo->pdo()->prepare($sql);
41
    foreach ($newData as $data) {
42
      //$prepare->bindValue('value', $data, GlobalPDO::PARAM_STR);
43
    }
44
    $prepare->bindValue('value', 'dataeee', GlobalPDO::PARAM_STR);
45
    return $prepare;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $prepare returns the type PDOStatement|boolean which is incompatible with the documented return type array.
Loading history...
46
    //implode(', ', $newData)
47
    //return $pdo->query($sql)->exec();
48
  }
49
50
  /**
51
   * Replaces any parameter placeholders in a query with the value of that
52
   * parameter. Useful for debugging. Assumes anonymous parameters from
53
   * $params are are in the same order as specified in $query
54
   *
55
   * @param string $query The sql query with parameter placeholders
56
   * @param array $params The array of substitution parameters
57
   * @return string The interpolated query
58
   */
59
  public static function interpolateQuery($query, $params)
60
  {
61
    $keys = array();
62
63
    # build a regular expression for each parameter
64
    foreach ($params as $key => $value) {
65
      if (is_string($key)) {
66
        $keys[] = '/:' . $key . '/';
67
      } else {
68
        $keys[] = '/[?]/';
69
      }
70
    }
71
72
    $query = preg_replace($keys, $params, $query, 1, $count);
73
74
    #trigger_error('replaced '.$count.' keys');
75
76
    return $query;
77
  }
78
}
79