Completed
Push — work-fleets ( fc0000...961997 )
by SuperNova.WS
59:17
created

PlanetRenderer::renderPlanetShortcuts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 9.4285
ccs 0
cts 8
cp 0
crap 6
1
<?php
2
/**
3
 * Created by Gorlum 03.09.2016 15:41
4
 */
5
6
namespace Planet;
7
8
use \classSupernova;
9
use \classLocale;
10
use Common\GlobalContainer;
11
use DBStatic\DBStaticFleetACS;
12
use DBStatic\DBStaticNote;
13
use DBStatic\DBStaticPlanet;
14
15
class PlanetRenderer {
16
17
  public function __construct(GlobalContainer $c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
18
19
  }
20
21
  /**
22
   * @param array $playerRow
23
   * @param array $planetRow
24
   *
25
   * @return array
26
   */
27
  // TODO - redo to unit/unitlist renderer
28
  public function renderAvailableShips($playerRow, $planetRow) {
29
    $record_index = 0;
30
    $ship_list = array();
31
    foreach (classSupernova::$gc->groupFleet as $n => $unit_id) {
0 ignored issues
show
Bug introduced by
The expression \classSupernova::$gc->groupFleet of type array|object<Closure> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
32
      $unit_level = mrc_get_level($playerRow, $planetRow, $unit_id, false, true);
33
      if ($unit_level <= 0) {
34
        continue;
35
      }
36
      $ship_data = get_ship_data($unit_id, $playerRow);
37
      $ship_list[$unit_id] = array(
38
        '__INDEX'          => $record_index++,
39
        'ID'               => $unit_id,
40
        'NAME'             => classLocale::$lang['tech'][$unit_id],
41
        'AMOUNT'           => $unit_level,
42
        'AMOUNT_TEXT'      => pretty_number($unit_level),
43
        'CONSUMPTION'      => $ship_data['consumption'],
44
        'CONSUMPTION_TEXT' => pretty_number($ship_data['consumption']),
45
        'SPEED'            => $ship_data['speed'],
46
        'SPEED_TEXT'       => pretty_number($ship_data['speed']),
47
        'CAPACITY'         => $ship_data['capacity'],
48
        'CAPACITY_TEXT'    => pretty_number($ship_data['capacity']),
49
      );
50
    }
51
52
    sortUnitRenderedList($ship_list, classSupernova::$user_options[PLAYER_OPTION_FLEET_SHIP_SORT], classSupernova::$user_options[PLAYER_OPTION_FLEET_SHIP_SORT_INVERSE]);
53
54
    return $ship_list;
55
  }
56
57
  public function renderPlanetButton(&$planetRow) {
58
    global $note_priority_classes;
59
60
    $result = array(
61
      'NAME'       => $planetRow['name'],
62
      'GALAXY'     => $planetRow['galaxy'],
63
      'SYSTEM'     => $planetRow['system'],
64
      'PLANET'     => $planetRow['planet'],
65
      'TYPE'       => $planetRow['planet_type'],
66
      'TYPE_PRINT' => classLocale::$lang['fl_shrtcup'][$planetRow['planet_type']],
67
    );
68
69
    if (isset($planetRow['priority'])) {
70
      $result += array(
71
        'PRIORITY'       => $planetRow['priority'],
72
        'PRIORITY_CLASS' => $note_priority_classes[$planetRow['priority']],
73
      );
74
    }
75
76
    if (isset($planetRow['id'])) {
77
      $result += array(
78
        'ID' => $planetRow['id'],
79
      );
80
    }
81
82
    return $result;
83
  }
84
85
  /**
86
   * @return array
87
   */
88
  public function renderACSList(array $dbOwnerRow) {
89
    $result = array();
90
91
    $query = DBStaticFleetACS::db_acs_get_list();
92
    while ($row = db_fetch($query)) {
93
      $members = explode(',', $row['eingeladen']);
94
      foreach ($members as $memberId) {
95
        if ($memberId == $dbOwnerRow['id']) {
96
          $result[] = classSupernova::$gc->planetRenderer->renderPlanetButton($row);
0 ignored issues
show
Bug introduced by
The method renderPlanetButton does only exist in Planet\PlanetRenderer, 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...
97
        }
98
      }
99
    }
100
101
    return $result;
102
  }
103
104
  /**
105
   * @return array
106
   */
107
  // TODO - move to Notes
108
  public function renderPlanetShortcuts(array $dbOwnerRow) {
109
    $result = array();
110
111
    // Building list of shortcuts
112
    $query = DBStaticNote::db_note_list_select_by_owner_and_planet($dbOwnerRow);
113
    while ($planetRow = db_fetch($query)) {
114
      $result[] = classSupernova::$gc->planetRenderer->renderPlanetButton($planetRow);
0 ignored issues
show
Bug introduced by
The method renderPlanetButton does only exist in Planet\PlanetRenderer, 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...
115
    }
116
117
    return $result;
118
  }
119
120
  /**
121
   * Building list of own planets & moons
122
   *
123
   * @return array
124
   */
125
  public function renderOwnPlanets(array $dbOwnerRow, array $dbSourcePlanetRow) {
126
    $result = array();
127
128
    $colonies = DBStaticPlanet::db_planet_list_sorted($dbOwnerRow);
129
    if (count($colonies) <= 1) {
130
      return $result;
131
    }
132
133
    foreach ($colonies as $planetRow) {
134
      if ($planetRow['id'] == $dbSourcePlanetRow['id']) {
135
        continue;
136
      }
137
138
      $result[] = classSupernova::$gc->planetRenderer->renderPlanetButton($planetRow);
0 ignored issues
show
Bug introduced by
The method renderPlanetButton does only exist in Planet\PlanetRenderer, 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...
139
    }
140
141
    return $result;
142
  }
143
144
}
145