Completed
Push — work-fleets ( 961997...006942 )
by SuperNova.WS
06:22
created

V2UnitList::onObjectIndexDuplicated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 2
rs 9.4285
1
<?php
2
3
namespace V2Unit;
4
5
use Common\GlobalContainer;
6
use Common\ObjectCollection;
7
use Common\V2Location;
8
use DBStatic\DBStaticUnit;
9
10
/**
11
 * Class V2UnitList
12
 *
13
 * @method V2UnitContainer current()
14
 *
15
 * @package V2Unit
16
 */
17
class V2UnitList extends ObjectCollection {
18
  /**
19
   * @var V2UnitModel $unitModel;
20
   */
21
  protected $unitModel;
22
23
  protected $locatedAt;
24
25
  protected $playerOwner;
26
27
  public function __construct(GlobalContainer $gc) {
28
    $this->unitModel = $gc->unitModel;
0 ignored issues
show
Documentation Bug introduced by
It seems like $gc->unitModel can also be of type object<Closure>. However, the property $unitModel is declared as type object<V2Unit\V2UnitModel>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
29
  }
30
31
  public function load(V2Location $location) {
32
    if (!($unitRows = DBStaticUnit::getUnitListByV2Location($location))) {
33
      return;
34
    }
35
36
    foreach ($unitRows as $dbId => $unitRow) {
37
      $unit = $this->unitModel->fromArray($unitRow);
38
      $this[$unit->snId] = $unit;
39
    }
40
  }
41
42
  public function unitAdd($snId, $level) {
43
    $unit = $this->unitModel->buildContainer();
44
    $unit->snId = $snId;
45
    $unit->level = $level;
46
    $this[$snId] = $unit;
47
  }
48
49
  /**
50
   * Function called when index already exists
51
   *
52
   * Can be used by child object
53
   *
54
   * @param V2UnitContainer $newUnit
55
   * @param int             $snId
56
   *
57
   * @throws \Exception
58
   */
59
  protected function onObjectIndexDuplicated($newUnit, $snId) {
60
    // TODO - error if not stackable
61
    $this[$snId]->level += $newUnit->level;
62
63
    return false;
64
  }
65
66
  public function isEmpty() {
67
    // TODO - sum of unit count
68
69
    return $this->count() > 0;
70
  }
71
72
}
73