Completed
Push — work-fleets ( d7065d...1ee481 )
by SuperNova.WS
08:22
created

DBStaticNote::processDelete()   C

Complexity

Conditions 10
Paths 8

Size

Total Lines 48
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 31
c 1
b 0
f 0
nc 8
nop 2
dl 0
loc 48
ccs 0
cts 39
cp 0
crap 110
rs 5.3454

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
namespace DBStatic;
4
use classSupernova;
5
use Exception;
6
use mysqli_result;
7
8
class DBStaticNote {
9
10
  public static function db_note_get_id_and_owner($note_id_edit) {
11
    return classSupernova::$db->doSelectFetch("SELECT `id`, `owner` FROM {{notes}} WHERE `id` = {$note_id_edit} LIMIT 1 FOR UPDATE");
12
  }
13
14
  /**
15
   * @param      $user_id
16
   * @param bool $sticky
17
   *
18
   * @return array|bool|mysqli_result|null
19
   */
20
  public static function db_note_list_by_owner($user_id, $sticky = false) {
21
    $sticky = $sticky ? ' AND `sticky` = 1' : '';
22
    $extra_sort = $sticky ? ' `galaxy` ASC, `system` ASC, `planet` ASC, `planet_type` ASC,' : '';
23
    $notes_query = classSupernova::$db->doSelect("SELECT * FROM `{{notes}}` WHERE `owner` = {$user_id} {$sticky} ORDER BY `priority` DESC, {$extra_sort} `time` DESC");
24
25
    return $notes_query;
26
  }
27
28
  /**
29
   * @param $note_priority
30
   * @param $note_title_unsafe
31
   * @param $note_text_unsafe
32
   * @param $note_galaxy
33
   * @param $note_system
34
   * @param $note_planet
35
   * @param $note_planet_type
36
   * @param $note_sticky
37
   * @param $note_id_edit
38
   */
39
  public static function db_note_update_by_id($note_priority, $note_title_unsafe, $note_text_unsafe, $note_galaxy, $note_system, $note_planet, $note_planet_type, $note_sticky, $note_id_edit) {
40
    classSupernova::$db->doUpdateRowSet(
41
      TABLE_NOTES,
42
      array(
43
        'time'        => SN_TIME_NOW,
44
        'priority'    => $note_priority,
45
        'title'       => $note_title_unsafe,
46
        'text'        => $note_text_unsafe,
47
        'galaxy'      => $note_galaxy,
48
        'system'      => $note_system,
49
        'planet'      => $note_planet,
50
        'planet_type' => $note_planet_type,
51
        'sticky'      => $note_sticky,
52
      ),
53
      array(
54
        'id' => $note_id_edit,
55
      )
56
    );
57
  }
58
59
  /**
60
   * @param $userId
61
   * @param $note_priority
62
   * @param $note_title_unsafe
63
   * @param $note_text_unsafe
64
   * @param $note_galaxy
65
   * @param $note_system
66
   * @param $note_planet
67
   * @param $note_planet_type
68
   * @param $note_sticky
69
   */
70 View Code Duplication
  public static function db_note_insert(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    $userId, $note_priority, $note_title_unsafe, $note_text_unsafe, $note_galaxy, $note_system, $note_planet, $note_planet_type, $note_sticky) {
72
    classSupernova::$db->doInsertSet(TABLE_NOTES, array(
73
      'owner'       => $userId,
74
      'time'        => SN_TIME_NOW,
75
      'priority'    => $note_priority,
76
      'title'       => $note_title_unsafe,
77
      'text'        => $note_text_unsafe,
78
      'galaxy'      => $note_galaxy,
79
      'system'      => $note_system,
80
      'planet'      => $note_planet,
81
      'planet_type' => $note_planet_type,
82
      'sticky'      => $note_sticky,
83
    ));
84
  }
85
86
87
  /**
88
   * @param $user
89
   *
90
   * @return array|bool|mysqli_result|null
91
   */
92
  public static function db_note_list_select_by_owner_and_planet($user) {
93
    $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`;");
94
95
    return $query;
96
  }
97
98
  /**
99
   * @param array $user
100
   * @param int   $note_id_edit
101
   *
102
   * @throws Exception
103
   */
104
  public static function processDelete($user, $note_id_edit) {
105
    $not = '';
106
    $whereDanger = array();
107
    switch (sys_get_param_str('note_delete_range')) {
108
      case 'all':
109
      break;
110
111
      /** @noinspection PhpMissingBreakStatementInspection */
112
      case 'marked_not':
113
        $not = 'NOT';
114
      case 'marked':
115
        if (!is_array($notes_marked = sys_get_param('note'))) {
116
          throw new Exception('note_err_none_selected', ERR_WARNING);
117
        }
118
119
        $notes_marked_filtered = array();
120
        foreach ($notes_marked as $note_id => $note_select) {
121
          if ($note_select == 'on' && $note_id = idval($note_id)) {
122
            $notes_marked_filtered[] = $note_id;
123
          }
124
        }
125
126
        if (empty($notes_marked_filtered)) {
127
          throw new Exception('note_err_none_selected', ERR_WARNING);
128
        }
129
130
        $notes_marked_filtered = implode(',', $notes_marked_filtered);
131
        $whereDanger[] = "`id` {$not} IN ({$notes_marked_filtered})";
132
      break;
133
134
      default:
135
        throw new Exception('note_warn_no_range', ERR_WARNING);
136
      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...
137
    }
138
139
    sn_db_transaction_start();
140
141
    classSupernova::$gc->db->doDeleteDanger(
0 ignored issues
show
Bug introduced by
The method doDeleteDanger 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...
142
      TABLE_NOTES,
143
      array(
144
        'owner' => $user['id'],
145
      ),
146
      $whereDanger
147
    );
148
    sn_db_transaction_commit();
149
150
    throw new Exception($note_id_edit ? 'note_err_none_changed' : 'note_err_none_added', ERR_NONE);
151
  }
152
153
}
154