Completed
Push — work-fleets ( 33857b...22a48f )
by SuperNova.WS
05:55
created

db.php ➔ db_error()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @version 2015-04-11 11:47:49 39b14.2
5
 * @copyright 2008-2015 Gorlum for Project "SuperNova.WS"
6
 */
7
8
defined('INSIDE') || die();
9
10
require_once('db/db_queries.php');
11
12
function db_change_units_perform($query, $tablename, $object_id) {
13
  $query = implode(',', $query);
14
  if($query && $object_id) {
15
    return classSupernova::db_upd_record_by_id($tablename == 'users' ? LOC_USER : LOC_PLANET, $object_id, $query);
16
    // return doquery("UPDATE {{{$tablename}}} SET {$query} WHERE `id` = '{$object_id}' LIMIT 1;");
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
17
  }
18
}
19
20
// TODO: THIS FUNCTION IS OBSOLETE AND SHOULD BE REPLACED!
21
// TODO - ТОЛЬКО ДЛЯ РЕСУРСОВ
22
// $unit_list should have unique entrances! Recompress non-uniq entrances before pass param!
23
function db_change_units(&$user, &$planet, $unit_list = array(), $query = null) {
24
  $query = is_array($query) ? $query : array(
25
    LOC_USER => array(),
26
    LOC_PLANET => array(),
27
  );
28
29
  $group = sn_get_groups('resources_loot');
30
31
  foreach($unit_list as $unit_id => $unit_amount) {
32
    if(!in_array($unit_id, $group)) {
33
      // TODO - remove later
34
      print('<h1>СООБЩИТЕ ЭТО АДМИНУ: db_change_units() вызван для не-ресурсов!</h1>');
35
      pdump(debug_backtrace());
36
      die('db_change_units() вызван для не-ресурсов!');
37
    }
38
39
    if(!$unit_amount) {
40
      continue;
41
    }
42
43
    $unit_db_name = pname_resource_name($unit_id);
44
45
    $unit_location = sys_get_unit_location($user, $planet, $unit_id);
46
47
    // Changing value in object
48
    switch($unit_location) {
49
      case LOC_USER:
50
        $user[$unit_db_name] += $unit_amount;
51
        break;
52
      case LOC_PLANET:
53
        $planet[$unit_db_name] += $unit_amount;
54
        break;
55
    }
56
57
    $unit_amount = $unit_amount < 0 ? $unit_amount : "+{$unit_amount}"; // Converting positive unit_amount to string '+unit_amount'
58
    $query[$unit_location][$unit_id] = "`{$unit_db_name}`=`{$unit_db_name}`{$unit_amount}";
59
  }
60
61
  db_change_units_perform($query[LOC_USER], 'users', $user['id']);
62
  db_change_units_perform($query[LOC_PLANET], 'planets', $planet['id']);
63
}
64
function sn_db_perform($table, $values, $type = 'insert', $options = false) {
65
  $mass_perform = false;
0 ignored issues
show
Unused Code introduced by
$mass_perform is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
66
67
  $field_set = '';
68
  $value_set = '';
0 ignored issues
show
Unused Code introduced by
$value_set is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
69
70
  switch($type) {
71
    case 'delete':
72
      $query = 'DELETE FROM';
73
      break;
74
75
    case 'insert':
76
      $query = 'INSERT INTO';
77
      if(isset($options['__multi'])) {
78
        // Here we generate mass-insert set
79
        break;
80
      }
81
    case 'update':
82
      if(!$query) {
0 ignored issues
show
Bug introduced by
The variable $query does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
83
        $query = 'UPDATE';
84
      }
85
86
      foreach($values as $field => &$value) {
87
        $value_type = gettype($value);
88
        if ($value_type == 'string') {
89
          $value = "'" . db_escape($value) . "'";
90
        }
91
        $value = "`{$field}` = {$value}";
92
      }
93
      $field_set = 'SET ' . implode(', ', $values);
94
      break;
95
96
  };
97
98
  $query .= " {$table} {$field_set}";
99
  return doquery($query);
100
}
101
102
103
104
function sn_db_field_set_is_safe(&$field_set) {
105
  return !empty($field_set['__IS_SAFE']);
106
}
107
function sn_db_field_set_safe_flag_clear(&$field_set) {
108
  unset($field_set['__IS_SAFE']);
109
}
110
function sn_db_field_set_safe_flag_set(&$field_set) {
111
  $field_set['__IS_SAFE'] = true;
112
}
113
function sn_db_field_set_make_safe($field_set, $serialize = false) {
114
  if(!is_array($field_set)) {
115
    die('$field_set is not an array!');
116
  }
117
118
  $result = array();
119
  foreach($field_set as $field => $value) {
120
    $field = db_escape(trim($field));
121
    switch (true) {
122
      case is_int($value):
123
      case is_double($value):
124
        break;
125
126
      case is_bool($value):
127
        $value = intval($value);
128
        break;
129
130
      case is_array($value):
131
      case is_object($value):
132
        $serialize ? $value = serialize($value) : die('$value is object or array with no $serialize');
133
134
      case is_string($value):
135
        $value = '"' . db_escape($value) . '"';
136
        break;
137
138
      case is_null($value):
139
        $value = 'NULL';
140
        break;
141
142
      default:
143
        die('unsupported operand type');
144
    }
145
    $result[$field] = $value;
146
  }
147
148
  sn_db_field_set_safe_flag_set($field_set);
149
150
  return $result;
151
}
152
153
154
function sn_db_unit_changeset_prepare($unit_id, $unit_value, $user, $planet_id = null) {
155
  return classSupernova::db_changeset_prepare_unit($unit_id, $unit_value, $user, $planet_id);
156
}
157
/**
158
 * Функция проверяет статус транзакции
159
 *
160
 * @param null|true|false $status Должна ли быть запущена транзакция в момент проверки
0 ignored issues
show
Bug introduced by
There is no parameter named $status. 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...
161
 *   <p>null - транзакция НЕ должна быть запущена</p>
162
 *   <p>true - транзакция должна быть запущена - для совместимости с $for_update</p>
163
 *   <p>false - всё равно - для совместимости с $for_update</p>
164
 * @return bool Текущий статус транзакции
165
 */
166
function sn_db_transaction_check($transaction_should_be_started = null) {
167
  return classSupernova::db_transaction_check($transaction_should_be_started);
168
}
169
function sn_db_transaction_start($level = '') {
170
  return classSupernova::db_transaction_start($level);
171
}
172
function sn_db_transaction_commit() {
173
  return classSupernova::db_transaction_commit();
174
}
175
function sn_db_transaction_rollback() {
176
  return classSupernova::db_transaction_rollback();
177
}
178
179
180
181
182
function doquery($query, $table = '', $fetch = false, $skip_query_check = false) {
183
  return classSupernova::$db->doquery($query, $table, $fetch, $skip_query_check);
184
}
185
function db_fetch(&$query) {
186
  return classSupernova::$db->db_fetch($query);
187
}
188
function db_escape($unescaped_string) {
189
  return classSupernova::$db->db_escape($unescaped_string);
190
}
191