Completed
Push — work-fleets ( bf14b2...c62b27 )
by SuperNova.WS
06:15
created

DBStaticNote   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 146
Duplicated Lines 10.27 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 15
loc 146
rs 10
ccs 0
cts 86
cp 0
wmc 17
lcom 0
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A db_note_get_id_and_owner() 0 3 1
A db_note_list_by_owner() 0 7 3
A db_note_update_by_id() 0 19 1
A db_note_insert() 15 15 1
A db_note_list_select_by_owner_and_planet() 0 5 1
C processDelete() 0 48 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
class DBStaticNote {
4
5
  public static function db_note_get_id_and_owner($note_id_edit) {
6
    return classSupernova::$db->doSelectFetch("SELECT `id`, `owner` FROM {{notes}} WHERE `id` = {$note_id_edit} LIMIT 1 FOR UPDATE");
7
  }
8
9
  /**
10
   * @param      $user_id
11
   * @param bool $sticky
12
   *
13
   * @return array|bool|mysqli_result|null
14
   */
15
  public static function db_note_list_by_owner($user_id, $sticky = false) {
16
    $sticky = $sticky ? ' AND `sticky` = 1' : '';
17
    $extra_sort = $sticky ? ' `galaxy` ASC, `system` ASC, `planet` ASC, `planet_type` ASC,' : '';
18
    $notes_query = classSupernova::$db->doSelect("SELECT * FROM `{{notes}}` WHERE `owner` = {$user_id} {$sticky} ORDER BY `priority` DESC, {$extra_sort} `time` DESC");
19
20
    return $notes_query;
21
  }
22
23
  /**
24
   * @param $note_priority
25
   * @param $note_title_unsafe
26
   * @param $note_text_unsafe
27
   * @param $note_galaxy
28
   * @param $note_system
29
   * @param $note_planet
30
   * @param $note_planet_type
31
   * @param $note_sticky
32
   * @param $note_id_edit
33
   */
34
  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) {
35
    classSupernova::$db->doUpdateRowSet(
36
      TABLE_NOTES,
37
      array(
38
        'time'        => SN_TIME_NOW,
39
        'priority'    => $note_priority,
40
        'title'       => $note_title_unsafe,
41
        'text'        => $note_text_unsafe,
42
        'galaxy'      => $note_galaxy,
43
        'system'      => $note_system,
44
        'planet'      => $note_planet,
45
        'planet_type' => $note_planet_type,
46
        'sticky'      => $note_sticky,
47
      ),
48
      array(
49
        'id' => $note_id_edit,
50
      )
51
    );
52
  }
53
54
  /**
55
   * @param $userId
56
   * @param $note_priority
57
   * @param $note_title_unsafe
58
   * @param $note_text_unsafe
59
   * @param $note_galaxy
60
   * @param $note_system
61
   * @param $note_planet
62
   * @param $note_planet_type
63
   * @param $note_sticky
64
   */
65 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...
66
    $userId, $note_priority, $note_title_unsafe, $note_text_unsafe, $note_galaxy, $note_system, $note_planet, $note_planet_type, $note_sticky) {
67
    classSupernova::$db->doInsertSet(TABLE_NOTES, array(
68
      'owner'       => $userId,
69
      'time'        => SN_TIME_NOW,
70
      'priority'    => $note_priority,
71
      'title'       => $note_title_unsafe,
72
      'text'        => $note_text_unsafe,
73
      'galaxy'      => $note_galaxy,
74
      'system'      => $note_system,
75
      'planet'      => $note_planet,
76
      'planet_type' => $note_planet_type,
77
      'sticky'      => $note_sticky,
78
    ));
79
  }
80
81
82
  /**
83
   * @param $user
84
   *
85
   * @return array|bool|mysqli_result|null
86
   */
87
  public static function db_note_list_select_by_owner_and_planet($user) {
88
    $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`;");
89
90
    return $query;
91
  }
92
93
  /**
94
   * @param array $user
95
   * @param int   $note_id_edit
96
   *
97
   * @throws Exception
98
   */
99
  public static function processDelete($user, $note_id_edit) {
100
    $not = '';
101
    $whereDanger = array();
102
    switch (sys_get_param_str('note_delete_range')) {
103
      case 'all':
104
      break;
105
106
      /** @noinspection PhpMissingBreakStatementInspection */
107
      case 'marked_not':
108
        $not = 'NOT';
109
      case 'marked':
110
        if (!is_array($notes_marked = sys_get_param('note'))) {
111
          throw new Exception('note_err_none_selected', ERR_WARNING);
112
        }
113
114
        $notes_marked_filtered = array();
115
        foreach ($notes_marked as $note_id => $note_select) {
116
          if ($note_select == 'on' && $note_id = idval($note_id)) {
117
            $notes_marked_filtered[] = $note_id;
118
          }
119
        }
120
121
        if (empty($notes_marked_filtered)) {
122
          throw new Exception('note_err_none_selected', ERR_WARNING);
123
        }
124
125
        $notes_marked_filtered = implode(',', $notes_marked_filtered);
126
        $whereDanger[] = "`id` {$not} IN ({$notes_marked_filtered})";
127
      break;
128
129
      default:
130
        throw new Exception('note_warn_no_range', ERR_WARNING);
131
      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...
132
    }
133
134
    sn_db_transaction_start();
135
136
    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...
137
      TABLE_NOTES,
138
      array(
139
        'owner' => $user['id'],
140
      ),
141
      $whereDanger
142
    );
143
    sn_db_transaction_commit();
144
145
    throw new Exception($note_id_edit ? 'note_err_none_changed' : 'note_err_none_added', ERR_NONE);
146
  }
147
148
}
149