Completed
Push — work-fleets ( 4ec5b3...fe2ede )
by SuperNova.WS
10:06
created

V2FleetModel::exportVector()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 3
dl 0
loc 7
rs 9.4285
c 3
b 0
f 0
ccs 0
cts 7
cp 0
crap 6
1
<?php
2
/**
3
 * Created by Gorlum 17.08.2016 22:07
4
 */
5
6
namespace V2Fleet;
7
8
use V2Unit\V2UnitContainer;
9
use V2Unit\V2UnitList;
10
use V2Unit\V2UnitModel;
11
use Vector\Vector;
12
13
/**
14
 * Class V2FleetModel
15
 *
16
 * @method V2FleetContainer buildContainer()
17
 * method V2FleetContainer fromArray(array $array)
18
 * @method V2FleetContainer loadById(mixed $dbId)
19
 *
20
 * @package V2Fleet
21
 */
22
class V2FleetModel extends \Entity\KeyedModel {
23
  protected $locationType = LOC_FLEET;
24
25
  /**
26
   * Return location type
27
   *
28
   * @param \Entity\EntityContainer $cEntity
29
   *
30
   * @return int
31
   */
32
  public function getLocationType($cEntity) {
0 ignored issues
show
Unused Code introduced by
The parameter $cEntity 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...
33
    return $this->locationType;
34
  }
35
36
  /**
37
   * Return location ID
38
   *
39
   * @param \Entity\EntityContainer $cEntity
40
   *
41
   * @return mixed
42
   */
43
  public function getLocationId($cEntity) {
44
    return $cEntity->dbId;
0 ignored issues
show
Documentation introduced by
The property dbId does not exist on object<Entity\EntityContainer>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
45
  }
46
47
48
  /**
49
   * Name of table for this entity
50
   *
51
   * @var string $tableName
52
   */
53
  protected $tableName = 'fleets';
54
  /**
55
   * Name of key field field in this table
56
   *
57
   * @var string $idFieldName
58
   */
59
  protected $idFieldName = 'fleet_id';
60
61
//  protected $exceptionClass = 'Entity\EntityException';
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% 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...
62
  protected $entityContainerClass = 'V2Fleet\V2FleetContainer';
63
64
//  protected $properties = array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
44% 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...
65
//    'dbId'        => array(P_DB_FIELD => 'fleet_id',),
66
//    'ownerId'     => array(P_DB_FIELD => 'fleet_owner',),
67
//    'missionType' => array(P_DB_FIELD => 'fleet_mission'),
68
//
69
//    'fleet_amount'          => array(P_DB_FIELD => 'fleet_amount'),
70
//    'fleet_start_planet_id' => array(P_DB_FIELD => 'fleet_start_planet_id'),
71
//
72
////    'fleet_start_galaxy'       => array(P_DB_FIELD => 'fleet_start_galaxy'),
73
////    'fleet_start_system'       => array(P_DB_FIELD => 'fleet_start_system'),
74
////    'fleet_start_planet'       => array(P_DB_FIELD => 'fleet_start_planet'),
75
////    'fleet_start_type'         => array(P_DB_FIELD => 'fleet_start_type'),
76
//
77
//    'fleet_end_planet_id' => array(P_DB_FIELD => 'fleet_end_planet_id'),
78
//    'fleet_target_owner'  => array(P_DB_FIELD => 'fleet_target_owner'),
79
//
80
////    'fleet_end_galaxy'         => array(P_DB_FIELD => 'fleet_end_galaxy'),
81
////    'fleet_end_system'         => array(P_DB_FIELD => 'fleet_end_system'),
82
////    'fleet_end_planet'         => array(P_DB_FIELD => 'fleet_end_planet'),
83
////    'fleet_end_type'           => array(P_DB_FIELD => 'fleet_end_type'),
84
//
85
////    'fleet_resource_metal'     => array(P_DB_FIELD => 'fleet_resource_metal'),
86
////    'fleet_resource_crystal'   => array(P_DB_FIELD => 'fleet_resource_crystal'),
87
////    'fleet_resource_deuterium' => array(P_DB_FIELD => 'fleet_resource_deuterium'),
88
//
89
//    'groupId' => array(P_DB_FIELD => 'fleet_group'),
90
//    'status'  => array(P_DB_FIELD => 'fleet_mess'),
91
//
92
//    'vectorDeparture' => array(P_DB_FIELD => 'fleet_start_galaxy'),
93
//    'vectorArrive'    => array(P_DB_FIELD => 'fleet_end_galaxy'),
94
//
95
//    'timeDeparture' => array(P_DB_FIELD => 'start_time'),
96
//    'timeArrive'    => array(P_DB_FIELD => 'fleet_start_time'),
97
//    'timeComplete'  => array(P_DB_FIELD => 'fleet_end_stay'),
98
//    'timeReturn'    => array(P_DB_FIELD => 'fleet_end_time'),
99
//
100
//    'units'       => array(),
101
//    'isReturning' => array(),
102
//  );
103
104
  public function __construct(\Common\GlobalContainer $gc) {
105
    parent::__construct($gc);
106
107
    $this->extendProperties(array(
108
        'ownerId'     => array(P_DB_FIELD => 'fleet_owner',),
109
        'missionType' => array(P_DB_FIELD => 'fleet_mission'),
110
111
        'fleet_amount'          => array(P_DB_FIELD => 'fleet_amount'),
112
        'fleet_start_planet_id' => array(P_DB_FIELD => 'fleet_start_planet_id'),
113
114
//    'fleet_start_galaxy'       => array(P_DB_FIELD => 'fleet_start_galaxy'),
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% 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...
115
//    'fleet_start_system'       => array(P_DB_FIELD => 'fleet_start_system'),
116
//    'fleet_start_planet'       => array(P_DB_FIELD => 'fleet_start_planet'),
117
//    'fleet_start_type'         => array(P_DB_FIELD => 'fleet_start_type'),
118
119
        'fleet_end_planet_id' => array(P_DB_FIELD => 'fleet_end_planet_id'),
120
        'fleet_target_owner'  => array(P_DB_FIELD => 'fleet_target_owner'),
121
122
//    'fleet_end_galaxy'         => array(P_DB_FIELD => 'fleet_end_galaxy'),
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% 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...
123
//    'fleet_end_system'         => array(P_DB_FIELD => 'fleet_end_system'),
124
//    'fleet_end_planet'         => array(P_DB_FIELD => 'fleet_end_planet'),
125
//    'fleet_end_type'           => array(P_DB_FIELD => 'fleet_end_type'),
126
127
//    'fleet_resource_metal'     => array(P_DB_FIELD => 'fleet_resource_metal'),
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% 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...
128
//    'fleet_resource_crystal'   => array(P_DB_FIELD => 'fleet_resource_crystal'),
129
//    'fleet_resource_deuterium' => array(P_DB_FIELD => 'fleet_resource_deuterium'),
130
131
        'groupId' => array(P_DB_FIELD => 'fleet_group'),
132
        'status'  => array(P_DB_FIELD => 'fleet_mess'),
133
134
        'vectorDeparture' => array(P_DB_FIELD => 'fleet_start_galaxy'),
135
        'vectorArrive'    => array(P_DB_FIELD => 'fleet_end_galaxy'),
136
137
        'timeDeparture' => array(P_DB_FIELD => 'start_time'),
138
        'timeArrive'    => array(P_DB_FIELD => 'fleet_start_time'),
139
        'timeComplete'  => array(P_DB_FIELD => 'fleet_end_stay'),
140
        'timeReturn'    => array(P_DB_FIELD => 'fleet_end_time'),
141
142
        'units'       => array(),
143
        'isReturning' => array(),
144
      )
145
    );
146
147
    $this->accessors->setAccessor('units', P_CONTAINER_GET, function (V2FleetContainer $that) {
148
      if (is_null($units = $that->getDirect('units'))) {
149
        $units = new V2UnitList();
150
        $that->setDirect('units', $units);
151
      }
152
153
      return $units;
154
    });
155
156
    $this->accessors->setAccessor('isReturning', P_CONTAINER_GET, function (V2FleetContainer $that) {
157
      return $that->status == 1;
158
    });
159
160
    $this->accessors->setAccessor('vectorDeparture', P_CONTAINER_IMPORT, array($this, 'importVector'));
161
    $this->accessors->setAccessor('vectorDeparture', P_CONTAINER_EXPORT, array($this, 'exportVector'));
162
    $this->accessors->setAccessor('vectorArrive', P_CONTAINER_IMPORT, array($this, 'importVector'));
163
    $this->accessors->setAccessor('vectorArrive', P_CONTAINER_EXPORT, array($this, 'exportVector'));
164
  }
165
166
  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...
167
    $prefix = $propertyName == 'vectorDeparture' ? 'fleet_start_' : 'fleet_end_';
168
    $that->$propertyName = new Vector(
169
      $that->row[$prefix . 'galaxy'],
170
      $that->row[$prefix . 'system'],
171
      $that->row[$prefix . 'planet'],
172
      $that->row[$prefix . 'type']
173
    );
174
  }
175
176
  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...
177
    $prefix = $propertyName == 'vectorDeparture' ? 'fleet_start_' : 'fleet_end_';
178
    $that->row[$prefix . 'galaxy'] = $that->$propertyName->galaxy;
179
    $that->row[$prefix . 'system'] = $that->$propertyName->system;
180
    $that->row[$prefix . 'planet'] = $that->$propertyName->planet;
181
    $that->row[$prefix . 'type'] = $that->$propertyName->type;
182
  }
183
184
  /**
185
   * @param array $array
186
   *
187
   * @return V2FleetContainer
188
   */
189
  public function fromArray($array) {
190
    /**
191
     * @var V2FleetContainer $cFleet
192
     */
193
    $cFleet = parent::fromArray($array);
194
195
    $cFleet->units->load($this->getLocationType($cFleet), $this->getLocationId($cFleet));
196
197
    foreach (array(
198
      RES_METAL     => 'fleet_resource_metal',
199
      RES_CRYSTAL   => 'fleet_resource_crystal',
200
      RES_DEUTERIUM => 'fleet_resource_deuterium',
201
    ) as $resourceId => $fieldName) {
202
      $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...
203
      $unit->snId = $resourceId;
204
      $unit->level = $array[$fieldName];
205
      $cFleet->units->attach($unit, $resourceId);
206
    }
207
208
    return $cFleet;
209
  }
210
211
}
212