Completed
Push — work-fleets ( 094cef...4ec5b3 )
by SuperNova.WS
06:12
created

V2UnitList::load()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 4
eloc 9
nc 4
nop 2
dl 0
loc 31
rs 8.5806
c 2
b 0
f 2
ccs 0
cts 12
cp 0
crap 20
1
<?php
2
3
namespace V2Unit;
4
use DBStatic\DBStaticUnit;
5
6
/**
7
 * Class V2UnitList
8
 *
9
 * @method V2UnitContainer current()
10
 *
11
 * @package V2Unit
12
 */
13
class V2UnitList extends \SplObjectStorage {
14
15
  /**
16
   * @var array
17
   */
18
  protected $unitBySnId;
19
20
  /**
21
   */
22
  public function load($locationType, $locationId) {
23
24
    if(!($unitRows = DBStaticUnit::db_get_unit_list_by_location(0, $locationType, $locationId))) {
25
      return null;
26
    }
27
28
    $model = \classSupernova::$gc->unitModel;
29
    foreach($unitRows as $dbId => $unitRow) {
0 ignored issues
show
Bug introduced by
The expression $unitRows of type array|boolean 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...
30
      $unit = $model->fromArray($unitRow);
0 ignored issues
show
Bug introduced by
The method fromArray does only exist in V2Unit\V2UnitModel, 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...
31
      $this->attach($unit);
32
      if($unit->snId) {
33
        $this->unitBySnId[$unit->snId] = $unit;
34
      }
35
    }
36
37
    /**
38
     *
39
     *
40
41
42
     foreach(unitsInLocation($location) as $unit_row) {
43
       $unit_obj = new V2Unit()->load($unit_row);
44
        $this->attach($unit_obj, $unit_obj->dbId);
45
     }
46
47
48
49
50
     *
51
     */
52
  }
53
54
}
55