Completed
Push — work-fleets ( 874fb8...41da5d )
by SuperNova.WS
06:44
created

db.php ➔ db_change_units()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 24
c 1
b 0
f 0
nc 6
nop 3
dl 0
loc 40
rs 8.439
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
/**
13
 * @param $adjust
14
 * @param $location
15
 * @param $object_id
16
 */
17
function db_change_units_perform($adjust, $location, $object_id) {
18
  if (!empty($object_id) && !empty($adjust)) {
19
    classSupernova::$gc->cacheOperator->db_upd_record_by_id(
0 ignored issues
show
Bug introduced by
The method db_upd_record_by_id does only exist in SnDbCachedOperator, but not in Closure.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
20
      $location,
21
      $object_id,
22
      array(),
23
      $adjust
24
    );
25
  }
26
}
27
28
// TODO: THIS FUNCTION IS OBSOLETE AND SHOULD BE REPLACED!
29
// TODO - ТОЛЬКО ДЛЯ РЕСУРСОВ
30
// $unit_list should have unique entrances! Recompress non-uniq entrances before pass param!
31
/**
32
 * @param       $user
33
 * @param       $planet
34
 * @param array $unit_list
35
 * @param null  $query
0 ignored issues
show
Bug introduced by
There is no parameter named $query. 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...
36
 */
37
function db_change_units(&$user, &$planet, $unit_list) {
38
  $query = array(
39
    LOC_USER => array(),
40
    LOC_PLANET => array(),
41
  );
42
43
  $group = sn_get_groups('resources_loot');
44
45
  foreach($unit_list as $unit_id => $unit_amount) {
46
    if(!in_array($unit_id, $group)) {
47
      // TODO - remove later
48
      print('<h1>СООБЩИТЕ ЭТО АДМИНУ: db_change_units() вызван для не-ресурсов!</h1>');
49
      pdump(debug_backtrace());
50
      die('db_change_units() вызван для не-ресурсов!');
51
    }
52
53
    if(empty($unit_amount)) {
54
      continue;
55
    }
56
57
    $unit_db_name = pname_resource_name($unit_id);
58
59
    $unit_location = sys_get_unit_location($user, $planet, $unit_id);
60
61
    // Changing value in object
62
    switch($unit_location) {
63
      case LOC_USER:
64
        $user[$unit_db_name] += $unit_amount;
65
        break;
66
      case LOC_PLANET:
67
        $planet[$unit_db_name] += $unit_amount;
68
        break;
69
    }
70
71
    $query[$unit_location][$unit_db_name] = $unit_amount;
72
  }
73
74
  db_change_units_perform($query[LOC_USER], LOC_USER, $user['id']);
75
  db_change_units_perform($query[LOC_PLANET], LOC_PLANET, $planet['id']);
76
}
77
function sn_db_perform($table, $values, $type = 'insert', $options = false) {
78
  $field_set = '';
79
80
  switch($type) {
81
    case 'delete':
82
      $query = 'DELETE FROM';
83
      break;
84
85
    case 'insert':
86
      $query = 'INSERT INTO';
87
      if(isset($options['__multi'])) {
88
        // Here we generate mass-insert set
89
        break;
90
      }
91
    case 'update':
92
      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...
93
        $query = 'UPDATE';
94
      }
95
96
      foreach($values as $field => &$value) {
97
        $value_type = gettype($value);
98
        if ($value_type == TYPE_STRING) {
99
          $value = "'" . db_escape($value) . "'";
100
        }
101
        $value = "`{$field}` = {$value}";
102
      }
103
      $field_set = 'SET ' . implode(', ', $values);
104
      break;
105
106
  };
107
108
  $query .= " {$table} {$field_set}";
109
  return classSupernova::$db->doExecute($query);
110
}
111
112
113
114
function sn_db_field_set_is_safe(&$field_set) {
115
  return !empty($field_set['__IS_SAFE']);
116
}
117
function sn_db_field_set_safe_flag_clear(&$field_set) {
118
  unset($field_set['__IS_SAFE']);
119
}
120
function sn_db_field_set_safe_flag_set(&$field_set) {
121
  $field_set['__IS_SAFE'] = true;
122
}
123
function sn_db_field_set_make_safe($field_set, $serialize = false) {
124
  if(!is_array($field_set)) {
125
    die('$field_set is not an array!');
126
  }
127
128
  $result = array();
129
  foreach($field_set as $field => $value) {
130
    $field = db_escape(trim($field));
131
    switch (true) {
132
      case is_int($value):
133
      case is_double($value):
134
        break;
135
136
      case is_bool($value):
137
        $value = intval($value);
138
        break;
139
140
      case is_array($value):
141
      case is_object($value):
142
        $serialize ? $value = serialize($value) : die('$value is object or array with no $serialize');
143
144
      case is_string($value):
145
        $value = '"' . db_escape($value) . '"';
146
        break;
147
148
      case is_null($value):
149
        $value = 'NULL';
150
        break;
151
152
      default:
153
        die('unsupported operand type');
154
    }
155
    $result[$field] = $value;
156
  }
157
158
  sn_db_field_set_safe_flag_set($field_set);
159
160
  return $result;
161
}
162
163
164
165
166
function sn_db_transaction_check($transaction_should_be_started = null) {
167
  return classSupernova::$gc->db->getTransaction()->check($transaction_should_be_started);
0 ignored issues
show
Bug introduced by
The method getTransaction does only exist in db_mysql, but not in Closure.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
168
}
169
function sn_db_transaction_start($level = '') {
170
  return classSupernova::$gc->db->getTransaction()->start($level);
0 ignored issues
show
Bug introduced by
The method getTransaction does only exist in db_mysql, but not in Closure.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
171
}
172
function sn_db_transaction_commit() {
173
  return classSupernova::$gc->db->getTransaction()->commit();
0 ignored issues
show
Bug introduced by
The method getTransaction does only exist in db_mysql, but not in Closure.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
174
}
175
function sn_db_transaction_rollback() {
176
  return classSupernova::$gc->db->getTransaction()->rollback();
0 ignored issues
show
Bug introduced by
The method getTransaction does only exist in db_mysql, but not in Closure.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
177
}
178
179
180
181
function db_fetch(&$query) {
182
  return classSupernova::$gc->db->db_fetch($query);
0 ignored issues
show
Bug introduced by
The method db_fetch does only exist in db_mysql, but not in Closure.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
183
}
184
function db_escape($unescaped_string) {
185
  return classSupernova::$gc->db->db_escape($unescaped_string);
0 ignored issues
show
Bug introduced by
The method db_escape does only exist in db_mysql, but not in Closure.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
186
}
187