Completed
Push — work-fleets ( d28adc...7496c9 )
by SuperNova.WS
06:01
created

V2UnitModel   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 118
Duplicated Lines 27.12 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 32
loc 118
rs 10
ccs 0
cts 58
cp 0
wmc 8
lcom 0
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
C __construct() 32 71 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Created by Gorlum 29.07.2016 13:18
4
 */
5
6
namespace V2Unit;
7
8
/**
9
 * Class V2UnitModel
10
 *
11
 * Second iteration of revised Unit
12
 *
13
 * @property int       $playerOwnerId
14
 * @property int       $locationType
15
 * @property int       $locationId
16
 * @property int       $type
17
 * @property int       $snId
18
 * @property int       $level - level of unit for DB: $count for stackable units, $level - fon unstackable units
19
 * property int $count // TODO
20
 * @property \DateTime $timeStart // TODO
21
 * @property \DateTime $timeFinish  // TODO
22
 * @property bool      $isStackable
23
 * @property string    $locationDefaultType
24
 * @property int       $bonusType // TODO - Optional?
25
 * @property array     $unitInfo - full info about unit
26
 *
27
 * @package V2Unit
28
 *
29
 */
30
class V2UnitModel extends \Entity {
31
32
  protected static $tableName = 'unit';
33
  protected static $idField = 'unit_id';
34
  protected static $exceptionClass = 'EntityException';
35
36
  protected static $_properties = array(
37
    'dbId'                => array(
38
      P_DB_FIELD => 'unit_id',
39
    ),
40
    'playerOwnerId'       => array(
41
      P_DB_FIELD => 'unit_player_id',
42
    ),
43
    'locationType'        => array(
44
      P_DB_FIELD => 'unit_location_type',
45
    ),
46
    'locationId'          => array(
47
      P_DB_FIELD => 'unit_location_id',
48
    ),
49
    'type'                => array(
50
      P_DB_FIELD => 'unit_type',
51
    ),
52
    'snId'                => array(
53
      P_DB_FIELD => 'unit_snid',
54
    ),
55
    // Order is important!
56
    // TODO - split dbLevel to level and count
57
    'level'               => array(
58
      P_DB_FIELD => 'unit_level',
59
    ),
60
    'count'               => array(),
61
    // TODO - move to child class
62
    'timeStart'           => array(
63
      P_DB_FIELD => 'unit_time_start',
64
    ),
65
    'timeFinish'          => array(
66
      P_DB_FIELD => 'unit_time_finish',
67
    ),
68
    // Do we need it? Or internal no info/getters/setters should be ignored?
69
    'unitInfo'            => array(),
70
    'isStackable'         => array(),
71
    'locationDefaultType' => array(),
72
    'bonusType'           => array(),
73
  );
74
75
  public function __construct(\Common\GlobalContainer $gc) {
76
    parent::__construct($gc);
77
78
    $that = $this;
79
80
    $this->_container->assignAccessor('type', P_CONTAINER_GETTER,
81
      function () use ($that) {
82
        return $that->type;
83
      }
84
    );
85
    $this->_container->assignAccessor('type', P_CONTAINER_SETTER,
86
      function ($value) use ($that) {
87
        $that->type = $value;
88
        $array = get_unit_param($value);
89
        $that->unitInfo = $array;
90
        // Mandatory
91
        $that->isStackable = empty($array[P_STACKABLE]) ? false : true;
92
        $that->locationDefaultType = empty($array[P_LOCATION_DEFAULT]) ? LOC_NONE : $array[P_LOCATION_DEFAULT];
93
        // Optional
94
        $that->bonusType = empty($array[P_BONUS_TYPE]) ? BONUS_NONE : $array[P_BONUS_TYPE];
95
      }
96
    );
97
98
    // This crap code is until php 5.4+. There we can use $this binding for lambdas
99
    $propertyName = 'timeStart';
100
    $fieldName = self::$_properties[$propertyName][P_DB_FIELD];
101
    $this->_container->assignAccessor($propertyName, P_CONTAINER_IMPORTER,
102 View Code Duplication
      function (&$row) use ($that, $fieldName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
        if (isset($row[$fieldName])) {
104
          $dateTime = new \DateTime($row[$fieldName]);
105
        } else {
106
          $dateTime = null;
107
        }
108
        $that->timeStart = $dateTime;
109
      }
110
    );
111
    $this->_container->assignAccessor($propertyName, P_CONTAINER_EXPORTER,
112 View Code Duplication
      function (&$row) use ($that, $fieldName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
        $dateTime = $that->timeStart;
114
        if ($dateTime instanceof \DateTime) {
115
          $row[$fieldName] = $dateTime->format(FMT_DATE_TIME_SQL);
116
        } else {
117
          $row[$fieldName] = null;
118
        }
119
      }
120
    );
121
122
    $propertyName = 'timeFinish';
123
    $fieldName = self::$_properties[$propertyName][P_DB_FIELD];
124
    $this->_container->assignAccessor($propertyName, P_CONTAINER_IMPORTER,
125 View Code Duplication
      function (&$row) use ($that, $fieldName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
        if (isset($row[$fieldName])) {
127
          $dateTime = new \DateTime($row[$fieldName]);
128
        } else {
129
          $dateTime = null;
130
        }
131
        $that->timeFinish = $dateTime;
132
      }
133
    );
134
    $this->_container->assignAccessor($propertyName, P_CONTAINER_EXPORTER,
135 View Code Duplication
      function (&$row) use ($that, $fieldName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
        $dateTime = $that->timeFinish;
137
        if ($dateTime instanceof \DateTime) {
138
          $row[$fieldName] = $dateTime->format(FMT_DATE_TIME_SQL);
139
        } else {
140
          $row[$fieldName] = null;
141
        }
142
      }
143
    );
144
145
  }
146
147
}
148