Completed
Push — work-fleets ( 7b3f52...8cb23c )
by SuperNova.WS
06:12
created

Vector   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 66.18%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 180
ccs 45
cts 68
cp 0.6618
rs 8.6
wmc 37
lcom 1
cbo 1

13 Methods

Rating   Name   Duplication   Size   Complexity  
A _staticInit() 0 11 2
A readFromVector() 0 6 1
A getParamInt() 0 5 2
A readFromParamFleets() 0 6 1
B __construct() 0 14 8
B distance() 0 16 6
B convertToVector() 0 10 6
A distanceFromCoordinates() 0 3 1
A distanceBetweenCoordinates() 0 3 1
A isSameLocation() 0 3 1
A isInUniverse() 0 5 4
A isInSystem() 0 3 2
A isInKnownSpace() 0 3 2
1
<?php
2
3
class Vector {
4
5
  const READ_VECTOR = 'readVector';
6
  const READ_PARAMS_FLEET = 'readParamsFleet';
7
8
  public static $knownGalaxies = 0;
9
  public static $knownSystems = 0;
10
  public static $knownPlanets = 0;
11
  public static $galaxyDistance = 20000;
12
  protected static $_isStaticInit = false;
13
14
  public $galaxy = 0;
15
  public $system = 0;
16
  public $planet = 0;
17
  public $type = PT_NONE;
18
19
  /**
20
   * @param classConfig $config
21
   */
22 1
  public static function _staticInit($config) {
23 1
    if (static::$_isStaticInit) {
24
      return;
25
    }
26
27 1
    static::$knownGalaxies = intval($config->game_maxGalaxy);
28 1
    static::$knownSystems = intval($config->game_maxSystem);
29 1
    static::$knownPlanets = intval($config->game_maxPlanet);
30 1
    static::$galaxyDistance = intval($config->uni_galaxy_distance);
31 1
    static::$_isStaticInit = true;
32 1
  }
33
34
  /**
35
   * @param Vector $vector
36
   */
37 1
  public function readFromVector($vector) {
38 1
    $this->galaxy = $vector->galaxy;
39 1
    $this->system = $vector->system;
40 1
    $this->planet = $vector->planet;
41 1
    $this->type = $vector->type;
42 1
  }
43
44
  /**
45
   * @param string $paramName
46
   * @param array  $planetRow
47
   *
48
   * @return int
49
   */
50
  protected function getParamInt($paramName, $planetRow) {
51
    $default = empty($planetRow[$paramName]) ? 0 : $planetRow[$paramName];
52
53
    return sys_get_param_int($paramName, $default);
54
  }
55
56
  /**
57
   * @param array $planetRow
58
   */
59
  public function readFromParamFleets($planetRow = array()) {
60
    $this->galaxy = $this->getParamInt('galaxy', $planetRow);
61
    $this->system = $this->getParamInt('system', $planetRow);
62
    $this->planet = $this->getParamInt('planet', $planetRow);
63
    $this->type = $this->getParamInt('planet_type', $planetRow);
64
  }
65
66
  /**
67
   * Vector constructor.
68
   *
69
   * @param int|string       $galaxy
70
   * @param int|Vector|array $system
71
   * @param int              $planet
72
   * @param int              $type
73
   */
74 2
  public function __construct($galaxy = 0, $system = 0, $planet = 0, $type = PT_NONE) {
75
    // static::_staticInit();
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
76
77 2
    if (is_string($galaxy) && $galaxy == Vector::READ_VECTOR && is_object($system) && $system instanceof Vector) {
78 1
      $this->readFromVector($system);
79 2
    } elseif (is_string($galaxy) && $galaxy == Vector::READ_PARAMS_FLEET && is_array($system)) {
80
      $this->readFromParamFleets($system);
81
    } else {
82 2
      $this->galaxy = intval($galaxy);
83 2
      $this->system = intval($system);
84 2
      $this->planet = intval($planet);
85 2
      $this->type = intval($type);
86
    }
87 2
  }
88
89
  /**
90
   * @param Vector $vector
91
   * @param bool   $returnZero
92
   *
93
   * @return int|number
94
   */
95 60
  public function distance($vector, $returnZero = false) {
96 60
    if ($this->galaxy != $vector->galaxy) {
97 18
      $distance = abs($this->galaxy - $vector->galaxy) * static::$galaxyDistance;
98 60
    } elseif ($this->system != $vector->system) {
99 18
      $distance = abs($this->system - $vector->system) * 5 * 19 + 2700;
100 42
    } elseif ($this->planet != $vector->planet) {
101 18
      $distance = abs($this->planet - $vector->planet) * 5 + 1000;
102 24
    } elseif ($returnZero && $this->type == $vector->type) {
103
      // && $this->type != PT_NONE && $vector->type != PT_NONE
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% 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...
104 1
      $distance = 0;
105 1
    } else {
106 5
      $distance = 5;
107
    }
108
109 60
    return $distance;
110
  }
111
112
  /**
113
   * @param array  $coordinates
114
   * @param string $prefix
115
   *
116
   * @return static
117
   */
118 8
  public static function convertToVector($coordinates, $prefix = '') {
119 8
    $galaxy = !empty($coordinates[$prefix . 'galaxy']) ? intval($coordinates[$prefix . 'galaxy']) : 0;
120 8
    $system = !empty($coordinates[$prefix . 'system']) ? intval($coordinates[$prefix . 'system']) : 0;
121 8
    $planet = !empty($coordinates[$prefix . 'planet']) ? intval($coordinates[$prefix . 'planet']) : 0;
122 8
    $type = !empty($coordinates[$prefix . 'type'])
123 8
      ? intval($coordinates[$prefix . 'type'])
124 8
      : (!empty($coordinates[$prefix . 'planet_type']) ? intval($coordinates[$prefix . 'planet_type']) : 0);
125
126 8
    return new static($galaxy, $system, $planet, $type);
127
  }
128
129
  /**
130
   * @param array $coordinates
131
   * @param bool  $returnZero
132
   *
133
   * @return int|number
134
   */
135 8
  public function distanceFromCoordinates($coordinates, $returnZero = false) {
136 8
    return $this->distance(static::convertToVector($coordinates), $returnZero);
137
  }
138
139
  /**
140
   * @param array $from
141
   * @param array $to
142
   * @param bool  $returnZero
143
   *
144
   * @return int|number
145
   */
146
  public static function distanceBetweenCoordinates($from, $to, $returnZero = false) {
147
    return static::convertToVector($from)->distanceFromCoordinates($to, $returnZero);
148
  }
149
150
  /**
151
   * @param array $planetRow
152
   *
153
   * @return bool
154
   */
155
  public function isSameLocation($planetRow) {
156
    return $this->distanceFromCoordinates($planetRow, true) == 0;
157
  }
158
159
  /**
160
   * @return bool
161
   */
162
  public function isInUniverse() {
163
    return
164
      $this->galaxy >= 1 && $this->galaxy <= static::$knownGalaxies &&
165
      $this->system >= 1 && $this->system <= static::$knownSystems;
166
  }
167
168
  /**
169
   * @return bool
170
   */
171
  public function isInSystem() {
172
    return $this->planet >= 1 && $this->planet <= static::$knownPlanets;
173
  }
174
175
  /**
176
   * @return bool
177
   */
178
  public function isInKnownSpace() {
179
    return $this->isInUniverse() && $this->isInSystem();
180
  }
181
182
}
183