Completed
Push — work-fleets ( 1f2a61...18e75e )
by SuperNova.WS
07:59
created

Entity::fillPropertyToField()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 3
eloc 7
nc 3
nop 0
dl 0
loc 13
rs 9.4285
c 3
b 0
f 1
ccs 0
cts 10
cp 0
crap 12
1
<?php
2
3
/**
4
 * Class Entity
5
 *
6
 * @property int|float $dbId Buddy record DB ID
7
 */
8
class Entity implements \Common\IMagicProperties, \Common\IEntity {
9
  const ENTITY_DB_ID_INCLUDE = true;
10
  const ENTITY_DB_ID_EXCLUDE = true;
11
12
  /**
13
   * Link to DB which used by this Entity
14
   *
15
   * @var db_mysql $dbStatic
16
   * deprecated - replace with container ID like 'db' or 'dbAuth'
17
   */
18
  protected static $dbStatic;
19
  /**
20
   * Name of table for this entity
21
   *
22
   * @var string $tableName
23
   */
24
  protected static $tableName = '_table';
25
  /**
26
   * Name of key field field in this table
27
   *
28
   * @var string $idField
29
   */
30
  protected static $idField = 'id';
31
32
  /**
33
   * Container for property values
34
   *
35
   * @var \Common\IPropertyContainer $_container
36
   */
37
  protected $_container;
38
  protected static $_containerName = 'V2PropertyContainer';
39
40
  /**
41
   * Property list and description
42
   *
43
   * propertyName => array(
44
   *    P_DB_FIELD => 'dbFieldName', - directly converts property to field and vice versa
45
   * )
46
   *
47
   * @var array[]
48
   */
49
  protected static $_properties = array();
50
51
//  /**
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...
52
//   * @var array
53
//   */
54
//  protected static $_propertyToField = array();
55
//  protected static $_propertyImportExportIsBind = false;
56
57
  /**
58
   * Service to work with rows
59
   *
60
   * @var \DbRowDirectOperator $rowOperator
61
   */
62
  protected static $rowOperator;
63
64
//  protected function bindFieldExport(&$propertyData, $propertyName) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
51% 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
//    if (!empty($propertyData[P_DB_ROW_EXPORT])) {
66
//      return;
67
//    }
68
//
69
//    // Last resort - binding export function to DB field name
70
//    if (!empty($propertyData[P_DB_FIELD])) {
71
//      // Property is mapped 1-to-1 to field
72
//      /**
73
//       * @param static $that
74
//       */
75
//      // Alas! No bindTo() method in 5.3 closures! So we should use what we have
76
//      // Also no auto bound of $this until 5.4
77
//      $propertyData[P_DB_ROW_EXPORT] = function ($that, &$row) use ($propertyName, $propertyData) {
78
//        $row[$propertyData[P_DB_FIELD]] = $that->$propertyName;
79
//      };
80
//    }
81
//  }
82
83
//  protected function bindFieldImport(&$propertyData, $propertyName) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% 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
//    if (!empty($propertyData[P_DB_ROW_IMPORT])) {
85
//      return;
86
//    }
87
//
88
//    // Last resort - binding import function to DB field name
89
//    if (!empty($propertyData[P_DB_ROW_METHOD_IMPORT_V2])) {
90
////      $funcName = $propertyData[P_DB_ROW_METHOD_IMPORT_V2];
91
//      $propertyData[P_DB_ROW_IMPORT] = array($this, $propertyData[P_DB_ROW_METHOD_IMPORT_V2]);
92
////      function ($that, &$row) use ($propertyName, $propertyData, $funcName) {
93
////        $that->$propertyName = \Common\Types::castAs(
94
////          !empty($propertyData[P_DB_TYPE]) ? $propertyData[P_DB_TYPE] : TYPE_EMPTY,
95
////          $row[$propertyData[P_DB_FIELD]]
96
////        );
97
////      };
98
//    } elseif (!empty($propertyData[P_DB_FIELD])) {
99
//      // Property is mapped 1-to-1 to field
100
//      /**
101
//       * @param static $that
102
//       */
103
//      // Alas! No bindTo() method in 5.3 closures! So we should use what we have
104
//      // Also no auto bound of $this until 5.4
105
//      $propertyData[P_DB_ROW_IMPORT] = function ($that, &$row) use ($propertyName, $propertyData) {
106
//        $that->$propertyName = \Common\Types::castAs(
107
//          !empty($propertyData[P_DB_TYPE]) ? $propertyData[P_DB_TYPE] : TYPE_EMPTY,
108
//          $row[$propertyData[P_DB_FIELD]]
109
//        );
110
//      };
111
//    }
112
//  }
113
114
//  /**
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% 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
//   * Fills property-to-field table which used to generate result array
116
//   */
117
//  protected function fillPropertyToField() {
118
//    if (static::$_propertyImportExportIsBind) {
119
//      return;
120
//    }
121
//
122
//    // Filling property-to-field relation array
123
//    foreach (static::$_properties as $propertyName => &$propertyData) {
124
//      $this->bindFieldExport($propertyData, $propertyName);
125
//      $this->bindFieldImport($propertyData, $propertyName);
126
//    }
127
//
128
//    static::$_propertyImportExportIsBind = true;
129
//  }
130
131
  /**
132
   * Entity constructor.
133
   *
134
   * @param \Common\GlobalContainer $gc
135
   */
136
  public function __construct($gc) {
137
    empty(static::$dbStatic) && !empty($gc->db) ? static::$dbStatic = $gc->db : false;
0 ignored issues
show
Documentation Bug introduced by
It seems like $gc->db can also be of type object<Closure>. However, the property $dbStatic is declared as type object<db_mysql>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
138
    static::$rowOperator = $gc->dbRowOperator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $gc->dbRowOperator can also be of type object<Closure>. However, the property $rowOperator is declared as type object<DbRowDirectOperator>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
139
140
    $this->_container = new static::$_containerName();
141
    $this->_container->setProperties(static::$_properties);
142
143
//    $this->fillPropertyToField();
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...
144
  }
145
146
147
//  /**
148
//   * Invoke row transformation operation on object
149
//   *
150
//   * Uses in to save/load data from DB row into/from object
151
//   *
152
//   * @param array  $row
153
//   * @param string $operation
154
//   * @param string $desc
155
//   *
156
//   * @throws Exception
157
//   *
158
//   * @deprecated
159
//   */
160
//  protected function rowInvokeOperation(&$row, $operation, $desc) {
161
//    foreach (static::$_properties as $propertyName => $propertyData) {
162
//      if (is_callable($propertyData[$operation])) {
163
//        // Some black magic here
164
//        // Closure is a class - so have __invoke() magic method
165
//        // It means we can invoke it by directly call __invoke()
166
//        // $propertyData[$operation]->__invoke($this, $row);
167
//        // However for a sake of uniformity may be we should consider use call_user_func
168
//        call_user_func($propertyData[$operation], $this, $row);
169
//      } else {
170
//        throw new \Exception('There is no valid DB [' . $operation . '] row operation for ' . get_called_class() . '::' . $propertyName);
171
//      }
172
//    }
173
//  }
174
175
  /**
176
   * Import DB row state into object properties
177
   *
178
   * @param array $row
179
   */
180
  public function importRow($row) {
181
    $this->_container->importRow($row);
182
  }
183
184
  /**
185
   * Export data from object properties into DB row for further use
186
   *
187
   * @param bool $withDbId - Should dbId too be returned. Useful for INSERT statements
188
   *
189
   * @return array
190
   */
191
  protected function exportDbRow($withDbId = self::ENTITY_DB_ID_INCLUDE) {
192
    $row = $this->_container->exportRow();
193
194
    if ($withDbId == self::ENTITY_DB_ID_EXCLUDE) {
195
      unset($row[$this->getIdFieldName()]);
196
    }
197
198
    return $row;
199
  }
200
201
  /**
202
   * @return array
203
   */
204
  public function exportRowWithoutId() {
205
    return $this->exportDbRow(self::ENTITY_DB_ID_EXCLUDE);
206
  }
207
208
  /**
209
   * @return array
210
   */
211
  public function exportRowWithId() {
212
    return $this->exportDbRow(self::ENTITY_DB_ID_INCLUDE);
213
  }
214
215
  /**
216
   * Gets entity's table name
217
   *
218
   * @return string
219
   */
220
  public function getTableName() {
221
    return static::$tableName;
222
  }
223
224
  /**
225
   * @return string
226
   */
227
  public function getIdFieldName() {
228
    return static::$idField;
229
  }
230
231
  /**
232
   * @return bool
233
   */
234
  public function isContainerEmpty() {
235
    return $this->_container->isEmpty();
236
  }
237
238
  /**
239
   * @return bool
240
   */
241
  public function isNew() {
242
    return empty($this->dbId);
243
  }
244
245
  /**
246
   * @return db_mysql
247
   */
248
  public function getDbStatic() {
249
    return static::$dbStatic;
250
  }
251
252
  public function __get($name) {
253
    return $this->_container->$name;
254
  }
255
256
  public function __set($name, $value) {
257
    $this->_container->$name = $value;
258
  }
259
260
  public function __isset($name) {
261
    return isset($this->_container->$name);
262
  }
263
264
  public function __unset($name) {
265
    unset($this->_container->$name);
266
  }
267
268
}
269