Completed
Push — work-fleets ( 22b024...7f5906 )
by SuperNova.WS
06:37
created

DBStaticNote::processDelete()   D

Complexity

Conditions 10
Paths 8

Size

Total Lines 42
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 29
nc 8
nop 2
dl 0
loc 42
rs 4.8196
c 1
b 0
f 0
ccs 0
cts 0
cp 0
crap 110

How to fix   Complexity   

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
class DBStaticNote {
4
5
  public static function db_note_list_delete($where) {
6
    classSupernova::$gc->db->doDeleteDeprecated(TABLE_NOTES, $where);
0 ignored issues
show
Bug introduced by
The method doDeleteDeprecated 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...
7
  }
8
9
  public static function db_note_get_id_and_owner($note_id_edit) {
10
    return classSupernova::$db->doSelectFetch("SELECT `id`, `owner` FROM {{notes}} WHERE `id` = {$note_id_edit} LIMIT 1 FOR UPDATE");
11
  }
12
13
  /**
14
   * @param      $user_id
15
   * @param bool $sticky
16
   *
17
   * @return array|bool|mysqli_result|null
18
   */
19
  public static function db_note_list_by_owner($user_id, $sticky = false) {
20
    $sticky = $sticky ? ' AND `sticky` = 1' : '';
21
    $extra_sort = $sticky ? ' `galaxy` ASC, `system` ASC, `planet` ASC, `planet_type` ASC,' : '';
22
    $notes_query = classSupernova::$db->doSelect("SELECT * FROM `{{notes}}` WHERE `owner` = {$user_id} {$sticky} ORDER BY `priority` DESC, {$extra_sort} `time` DESC");
23
24
    return $notes_query;
25
  }
26
27
  /**
28
   * @param $note_priority
29
   * @param $note_title
30
   * @param $note_text
31
   * @param $note_galaxy
32
   * @param $note_system
33
   * @param $note_planet
34
   * @param $note_planet_type
35
   * @param $note_sticky
36
   * @param $note_id_edit
37
   */
38
  public static function db_note_update_by_id($note_priority, $note_title, $note_text, $note_galaxy, $note_system, $note_planet, $note_planet_type, $note_sticky, $note_id_edit) {
39
    classSupernova::$db->doUpdate("UPDATE {{notes}} SET `time` = " . SN_TIME_NOW . ", `priority` = {$note_priority}, `title` = '{$note_title}', `text` = '{$note_text}',
40
        `galaxy` = {$note_galaxy}, `system` = {$note_system}, `planet` = {$note_planet}, `planet_type` = {$note_planet_type}, `sticky` = {$note_sticky}
41
        WHERE `id` = {$note_id_edit} LIMIT 1;");
42
  }
43
44
  /**
45
   * @param $user
46
   * @param $note_priority
47
   * @param $note_title
48
   * @param $note_text
49
   * @param $note_galaxy
50
   * @param $note_system
51
   * @param $note_planet
52
   * @param $note_planet_type
53
   * @param $note_sticky
54
   */
55
  public static function db_note_insert($user, $note_priority, $note_title, $note_text, $note_galaxy, $note_system, $note_planet, $note_planet_type, $note_sticky) {
56
    classSupernova::$db->doInsert("INSERT INTO {{notes}} SET `owner` = {$user['id']}, `time` = " . SN_TIME_NOW . ", `priority` = {$note_priority}, `title` = '{$note_title}', `text` = '{$note_text}',
57
        `galaxy` = {$note_galaxy}, `system` = {$note_system}, `planet` = {$note_planet}, `planet_type` = {$note_planet_type}, `sticky` = {$note_sticky};");
58
  }
59
60
61
  /**
62
   * @param $user
63
   *
64
   * @return array|bool|mysqli_result|null
65
   */
66
  public static function db_note_list_select_by_owner_and_planet($user) {
67
    $query = classSupernova::$db->doSelect("SELECT * FROM {{notes}} WHERE `owner` = {$user['id']} AND `galaxy` <> 0 AND `system` <> 0 AND `planet` <> 0 ORDER BY `priority` DESC, `galaxy`, `system`, `planet`, `planet_type`;");
68
69
    return $query;
70
  }
71
72
  /**
73
   * @param array $user
74
   * @param int   $note_id_edit
75
   *
76
   * @throws Exception
77
   */
78
  public static function processDelete($user, $note_id_edit) {
79
    $not = '';
80
    $query_where = '';
81
    switch (sys_get_param_str('note_delete_range')) {
82
      case 'all':
83
      break;
84
85
      case 'marked_not':
86
        $not = 'NOT';
87
      case 'marked':
88
        if (!is_array($notes_marked = sys_get_param('note'))) {
89
          throw new Exception('note_err_none_selected', ERR_WARNING);
90
        }
91
92
        $notes_marked_filtered = array();
93
        foreach ($notes_marked as $note_id => $note_select) {
94
          if ($note_select == 'on' && $note_id = idval($note_id)) {
95
            $notes_marked_filtered[] = $note_id;
96
          }
97
        }
98
99
        if (empty($notes_marked_filtered)) {
100
          throw new Exception('note_err_none_selected', ERR_WARNING);
101
        }
102
103
        $notes_marked_filtered = implode(',', $notes_marked_filtered);
104
        $query_where = "AND `id` {$not} IN ({$notes_marked_filtered})";
105
      break;
106
107
      default:
108
        throw new Exception('note_warn_no_range', ERR_WARNING);
109
      break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
110
    }
111
112
    sn_db_transaction_start();
113
    $where['owner'] = $user['id'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$where was never initialized. Although not strictly required by PHP, it is generally a good practice to add $where = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
114
    $where[] = $query_where;
115
    DBStaticNote::db_note_list_delete($where);
116
    sn_db_transaction_commit();
117
118
    throw new Exception($note_id_edit ? 'note_err_none_changed' : 'note_err_none_added', ERR_NONE);
119
  }
120
121
}
122