Completed
Push — work-fleets ( 8d34ed...b1376e )
by SuperNova.WS
06:48
created

V2FleetModel::__construct()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 81
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 7
Bugs 1 Features 1
Metric Value
cc 3
eloc 41
c 7
b 1
f 1
nc 1
nop 1
dl 0
loc 81
ccs 0
cts 47
cp 0
crap 12
rs 8.8076

How to fix   Long Method   

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
 * Created by Gorlum 17.08.2016 22:07
4
 */
5
6
namespace V2Fleet;
7
8
use Common\V2Location;
9
use V2Unit\V2UnitList;
10
use Vector\Vector;
11
use Entity\KeyedModel;
12
13
/**
14
 * Class V2FleetModel
15
 *
16
 * @method V2FleetContainer buildContainer()
17
 * @method V2FleetContainer loadById(mixed $dbId)
18
 *
19
 * @package V2Fleet
20
 */
21
class V2FleetModel extends KeyedModel {
22
  protected $location;
23
24
  /**
25
   * Name of table for this entity
26
   *
27
   * @var string $tableName
28
   */
29
  protected $tableName = 'fleets';
30
  /**
31
   * Name of key field field in this table
32
   *
33
   * @var string $idFieldName
34
   */
35
  protected $idFieldName = 'fleet_id';
36
37
  protected $exceptionClass = 'Entity\EntityException';
38
  protected $entityContainerClass = 'V2Fleet\V2FleetContainer';
39
40
  public function __construct(\Common\GlobalContainer $gc) {
41
    parent::__construct($gc);
42
43
    $this->extendProperties(array(
44
        'ownerId'     => array(P_DB_FIELD => 'fleet_owner',),
45
        'missionType' => array(P_DB_FIELD => 'fleet_mission'),
46
47
        'fleet_amount'          => array(P_DB_FIELD => 'fleet_amount'),
48
        'fleet_start_planet_id' => array(P_DB_FIELD => 'fleet_start_planet_id'),
49
50
51
        'fleet_end_planet_id' => array(P_DB_FIELD => 'fleet_end_planet_id'),
52
        'fleet_target_owner'  => array(P_DB_FIELD => 'fleet_target_owner'),
53
54
//    'fleet_start_galaxy'       => array(P_DB_FIELD => 'fleet_start_galaxy'),
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
55
//    'fleet_start_system'       => array(P_DB_FIELD => 'fleet_start_system'),
56
//    'fleet_start_planet'       => array(P_DB_FIELD => 'fleet_start_planet'),
57
//    'fleet_start_type'         => array(P_DB_FIELD => 'fleet_start_type'),
58
//    'fleet_end_galaxy'         => array(P_DB_FIELD => 'fleet_end_galaxy'),
59
//    'fleet_end_system'         => array(P_DB_FIELD => 'fleet_end_system'),
60
//    'fleet_end_planet'         => array(P_DB_FIELD => 'fleet_end_planet'),
61
//    'fleet_end_type'           => array(P_DB_FIELD => 'fleet_end_type'),
62
//    'fleet_resource_metal'     => array(P_DB_FIELD => 'fleet_resource_metal'),
63
//    'fleet_resource_crystal'   => array(P_DB_FIELD => 'fleet_resource_crystal'),
64
//    'fleet_resource_deuterium' => array(P_DB_FIELD => 'fleet_resource_deuterium'),
65
66
        'groupId' => array(P_DB_FIELD => 'fleet_group'),
67
        'status'  => array(P_DB_FIELD => 'fleet_mess'),
68
69
        'vectorDeparture' => array(P_DB_FIELD => 'fleet_start_galaxy'),
70
        'vectorArrive'    => array(P_DB_FIELD => 'fleet_end_galaxy'),
71
72
        'timeDeparture' => array(P_DB_FIELD => 'start_time'),
73
        'timeArrive'    => array(P_DB_FIELD => 'fleet_start_time'),
74
        'timeComplete'  => array(P_DB_FIELD => 'fleet_end_stay'),
75
        'timeReturn'    => array(P_DB_FIELD => 'fleet_end_time'),
76
77
        'units'       => array(),
78
        'isReturning' => array(),
79
      )
80
    );
81
82
    $this->accessors->setAccessor('location', P_CONTAINER_GET, function (V2FleetContainer $that) {
83
      if (is_null($location = $that->getDirect('location'))) {
84
        $location = new V2Location(LOC_FLEET);
85
        $that->setDirect('location', $location);
86
      }
87
88
      return $location;
89
    });
90
91
    $this->accessors->setAccessor('dbId', P_CONTAINER_SET, function (V2FleetContainer $that, $value) {
92
      $that->setDirect('dbId', $value);
93
      $that->location->setLocationId($value);
94
    });
95
96
    $this->accessors->setAccessor('ownerId', P_CONTAINER_SET, function (V2FleetContainer $that, $value) {
97
      $that->setDirect('ownerId', $value);
98
      $that->location->setLocationPlayerId($value);
99
    });
100
101
    $this->accessors->setAccessor('vectorDeparture', P_CONTAINER_IMPORT, array($this, 'importVector'));
102
    $this->accessors->setAccessor('vectorDeparture', P_CONTAINER_EXPORT, array($this, 'exportVector'));
103
    $this->accessors->setAccessor('vectorArrive', P_CONTAINER_IMPORT, array($this, 'importVector'));
104
    $this->accessors->setAccessor('vectorArrive', P_CONTAINER_EXPORT, array($this, 'exportVector'));
105
106
107
    $this->accessors->setAccessor('units', P_CONTAINER_GET, function (V2FleetContainer $that) {
108
      if (is_null($units = $that->getDirect('units'))) {
109
        $units = new V2UnitList();
110
        $that->setDirect('units', $units);
111
      }
112
113
      return $units;
114
    });
115
116
    $this->accessors->setAccessor('isReturning', P_CONTAINER_GET, function (V2FleetContainer $that) {
117
      return $that->status == FLEET_FLAG_RETURNING;
118
    });
119
120
  }
121
122
  public function importVector(V2FleetContainer $that, $propertyName, $fieldName) {
0 ignored issues
show
Unused Code introduced by
The parameter $fieldName 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...
123
    $prefix = $propertyName == 'vectorDeparture' ? 'fleet_start_' : 'fleet_end_';
124
    $that->$propertyName = new Vector(
125
      $that->row[$prefix . 'galaxy'],
126
      $that->row[$prefix . 'system'],
127
      $that->row[$prefix . 'planet'],
128
      $that->row[$prefix . 'type']
129
    );
130
  }
131
132
  public function exportVector(V2FleetContainer $that, $propertyName, $fieldName) {
0 ignored issues
show
Unused Code introduced by
The parameter $fieldName 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...
133
    $prefix = $propertyName == 'vectorDeparture' ? 'fleet_start_' : 'fleet_end_';
134
    $that->row[$prefix . 'galaxy'] = $that->$propertyName->galaxy;
135
    $that->row[$prefix . 'system'] = $that->$propertyName->system;
136
    $that->row[$prefix . 'planet'] = $that->$propertyName->planet;
137
    $that->row[$prefix . 'type'] = $that->$propertyName->type;
138
  }
139
140
  /**
141
   * @param array $array
142
   *
143
   * @return V2FleetContainer
144
   */
145
  public function fromArray($array) {
146
    /**
147
     * @var V2FleetContainer $cFleet
148
     */
149
    $cFleet = parent::fromArray($array);
150
151
    $cFleet->units->load($cFleet->location);
152
153
    foreach (array(
154
      RES_METAL     => 'fleet_resource_metal',
155
      RES_CRYSTAL   => 'fleet_resource_crystal',
156
      RES_DEUTERIUM => 'fleet_resource_deuterium',
157
    ) as $resourceId => $fieldName) {
158
      $unit = \classSupernova::$gc->unitModel->buildContainer();
0 ignored issues
show
Bug introduced by
The method buildContainer 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...
159
      $unit->snId = $resourceId;
160
      $unit->level = $array[$fieldName];
161
      $cFleet->units->attach($unit, $resourceId);
162
    }
163
164
    return $cFleet;
165
  }
166
167
}
168