Completed
Push — work-fleets ( e7900f...9522d3 )
by SuperNova.WS
05:39
created

Vector::convertToVector()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 8.8571
cc 6
eloc 8
nc 32
nop 2
1
<?php
2
3
class Vector {
4
5
  public static $knownGalaxies = 0;
6
  public static $knownSystems = 0;
7
  public static $knownPlanets = 0;
8
  protected static $_isStaticInit = false;
9
10
  public $galaxy = 0;
11
  public $system = 0;
12
  public $planet = 0;
13
  public $type = PT_NONE;
14
15
  public static function _staticInit() {
16
    if(static::$_isStaticInit) {
17
      return;
18
    }
19
    static::$knownGalaxies = intval(classSupernova::$config->game_maxGalaxy);
20
    static::$knownSystems = intval(classSupernova::$config->game_maxSystem);
21
    static::$knownPlanets = intval(classSupernova::$config->game_maxPlanet);
22
    static::$_isStaticInit = true;
23
  }
24
25
  /**
26
   * UniverseVector constructor.
27
   *
28
   * @param int|string       $galaxy
29
   * @param int|Vector|array $system
30
   * @param int              $planet
31
   * @param int              $type
32
   */
33
  public function __construct($galaxy = 0, $system = 0, $planet = 0, $type = PT_NONE) {
34
    // 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...
35
36
    if(is_string($galaxy) && $galaxy == VECTOR_READ_VECTOR && is_object($system) && $system instanceof Vector) {
37
      $this->readFromVector($system);
38
    } elseif(is_string($galaxy) && $galaxy == VECTOR_READ_PARAMS && is_array($system)) {
39
      $this->readFromParamFleets($system);
40
    } else {
41
      $this->galaxy = intval($galaxy);
42
      $this->system = intval($system);
43
      $this->planet = intval($planet);
44
      $this->type = intval($type);
45
    }
46
  }
47
48
49
  public function readFromParamFleets($planetrow = array()) {
50
    $this->galaxy = sys_get_param_int('galaxy', $planetrow['galaxy']);
51
    $this->system = sys_get_param_int('system', $planetrow['system']);
52
    $this->planet = sys_get_param_int('planet', $planetrow['planet']);
53
    $this->type = sys_get_param_int('planet_type', $planetrow['planet_type']);
54
  }
55
56
  /**
57
   * @param Vector $vector
58
   */
59
  public function readFromVector($vector) {
60
    $this->galaxy = $vector->galaxy;
61
    $this->system = $vector->system;
62
    $this->planet = $vector->planet;
63
    $this->type = $vector->type;
64
  }
65
66
  /**
67
   * @param array $planetRow
68
   */
69
  public function isEqualToPlanet($planetRow) {
70
    return $this->distanceFromCoordinates($planetRow) == 0;
71
  }
72
73
  /**
74
   * @param Vector $vector
75
   */
76
  public function distance($vector) {
77
    if($this->galaxy != $vector->galaxy) {
78
      $distance = abs($this->galaxy - $vector->galaxy) * classSupernova::$config->uni_galaxy_distance;
0 ignored issues
show
Documentation introduced by
The property uni_galaxy_distance does not exist on object<classConfig>. 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...
79
    } elseif($this->system != $vector->system) {
80
      $distance = abs($this->system - $vector->system) * 5 * 19 + 2700;
81
    } elseif($this->planet != $vector->planet) {
82
      $distance = abs($this->planet - $vector->planet) * 5 + 1000;
83
      // TODO - uncomment
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% 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...
84
//    } elseif($this->type != PT_NONE && $vector->type != PT_NONE && $this->type == $vector->type) {
85
//      $distance = 0;
86
    } else {
87
      $distance = 5;
88
    }
89
90
    return $distance;
91
  }
92
93
  /**
94
   * @param array $coordinates
95
   */
96
  public function distanceFromCoordinates($coordinates) {
97
    return $this->distance(static::convertToVector($coordinates));
98
  }
99
100
  /**
101
   * @param array $coordinates
102
   */
103
  public static function convertToVector($coordinates, $prefix = '') {
104
    $galaxy = !empty($coordinates[$prefix . 'galaxy']) ? intval($coordinates[$prefix . 'galaxy']) : 0;
105
    $system = !empty($coordinates[$prefix . 'system']) ? intval($coordinates[$prefix . 'system']) : 0;
106
    $planet = !empty($coordinates[$prefix . 'planet']) ? intval($coordinates[$prefix . 'planet']) : 0;
107
    $type = !empty($coordinates[$prefix . 'type'])
108
      ? intval($coordinates[$prefix . 'type'])
109
      : (!empty($coordinates[$prefix . 'planet_type']) ? intval($coordinates[$prefix . 'planet_type']) : 0);
110
111
    return new static($galaxy, $system, $planet, $type);
112
  }
113
114
  public static function distanceBetweenCoordinates($from, $to) {
115
    $fromVector = static::convertToVector($from);
116
117
    return $fromVector->distanceFromCoordinates($to);
118
  }
119
120
  /**
121
   * @return bool
122
   */
123
  public function isInUniverse() {
124
    return
125
      $this->galaxy > 0 && $this->galaxy <= static::$knownGalaxies &&
126
      $this->system > 0 && $this->system <= static::$knownSystems;
127
  }
128
129
  /**
130
   * @return bool
131
   */
132
  public function isInSystem() {
133
    return $this->planet > 0 && $this->planet <= static::$knownPlanets;
134
  }
135
136
  /**
137
   * @return bool
138
   */
139
  public function isInKnownSpace() {
140
    return $this->isInUniverse() && $this->isInSystem();
141
  }
142
143
}
144
145
Vector::_staticInit();
146