Completed
Push — work-fleets ( 7aaf68...e155c0 )
by SuperNova.WS
06:30
created

Entity::isEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * Class Entity
5
 *
6
 * @property int|float $dbId Buddy record DB ID
7
 */
8
class Entity implements \Common\IMagicProperties {
9
  /**
10
   * Name of table for this entity
11
   *
12
   * @var string $tableName
13
   */
14
  protected static $tableName = '_table';
15
  /**
16
   * Name of key field field in this table
17
   *
18
   * @var string $idField
19
   */
20
  protected static $idField = 'id';
21
  /**
22
   * Container for property values
23
   *
24
   * @var PropertyHider $_container
25
   */
26
  protected $_container;
27
  protected static $_containerName = 'PropertyHiderInArray';
28
29
  /**
30
   * Property list
31
   *
32
   * @var array
33
   */
34
  protected static $_properties = array();
35
36
  /**
37
   * @var array
38
   */
39
  protected static $_propertyToField = array();
40
41
  /**
42
   * @var db_mysql|null $dbStatic
43
   */
44
  public static $dbStatic = null;
45
46
  /**
47
   * Fills property-to-field table which used to generate result array
48
   */
49
  protected function fillPropertyToField() {
50
    if (!empty(static::$_propertyToField)) {
51
      return;
52
    }
53
54
    // Filling property-to-filed relation array
55
    foreach (static::$_properties as $propertyName => &$propertyData) {
56
      // Property is mapped 1-to-1 to field
57
      if (!empty($propertyData[P_DB_FIELD])) {
58
        $fieldName = $propertyData[P_DB_FIELD];
59
        /**
60
         * @param static $that
61
         */
62
        // Alas! No bindTo() method in 5.3 closures! So we should use what we have
63
        $propertyData[P_DB_ROW_EXPORT] = function ($that, &$row) use ($propertyName, $fieldName) {
64
          $row[$fieldName] = $that->$propertyName;
65
        };
66
        $propertyData[P_DB_ROW_IMPORT] = function ($that, &$row) use ($propertyName, $fieldName) {
67
          // TODO: Here should be some conversions to property type
68
          $that->$propertyName = $row[$fieldName];
69
        };
70
      }
71
72
    }
73
  }
74
75
  /**
76
   * Entity constructor.
77
   *
78
   * @param \Pimple\GlobalContainer $gc
79
   */
80
  public function __construct($gc) {
81
    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>|null. 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...
82
83
    $this->_container = new static::$_containerName();
84
    $this->_container->setProperties(static::$_properties);
85
86
    $this->fillPropertyToField();
87
  }
88
89
90
  public function getTableName() {
91
    return static::$tableName;
92
  }
93
94
  public function getIdFieldName() {
95
    return static::$idField;
96
  }
97
98
  public function load($recordId) {
99
    classSupernova::$gc->dbRowOperator->getById($this, $recordId);
0 ignored issues
show
Bug introduced by
The method getById does only exist in DbRowSimple, 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...
100
  }
101
102
  // TODO - move to reader ????????
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
103
  public function delete() {
104
    return classSupernova::$gc->dbRowOperator->deleteById($this);
0 ignored issues
show
Bug introduced by
The method deleteById does only exist in DbRowSimple, 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...
105
  }
106
107
  /**
108
   * @return int|string
109
   */
110
  // TODO - move to reader ????????
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
111
  public function insert() {
112
    return classSupernova::$gc->dbRowOperator->insert($this);
0 ignored issues
show
Bug introduced by
The method insert does only exist in DbRowSimple, 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...
113
  }
114
115
  public function isContainerEmpty() {
116
    return $this->_container->isContainerEmpty();
117
  }
118
119
  public function isNew() {
120
    return $this->getIdFieldName() != 0;
121
  }
122
123
  /**
124
   * Invoke row transformation operation on object
125
   *
126
   * Uses in to save/load data from DB row into/from object
127
   *
128
   * @param array  $row
129
   * @param string $operation
130
   * @param string $desc
131
   *
132
   * @throws Exception
133
   */
134
  protected function rowInvokeOperation(&$row, $operation, $desc) {
135
    foreach (static::$_properties as $propertyName => $propertyData) {
136
      if (is_callable($propertyData[$operation])) {
137
        // Some black magic here
138
        // Closure is a class - so have __invoke() magic method
139
        // It means we can invoke it by directly call __invoke()
140
        $propertyData[$operation]->__invoke($this, $row);
141
        // TODO - however for a sake of uniformity may be we should consider use call_user_func
142
//      call_user_func($propertyData[P_DB_ROW_EXPORT], $this);
143
      } else {
144
        throw new \Exception('There is no valid DB row ' . $desc . ' for ' . get_called_class() . '::' . $propertyName);
145
      }
146
    }
147
  }
148
149
  /**
150
   * Import DB row state into object properties
151
   *
152
   * @param array $row
153
   */
154
  public function importDbRow($row) {
155
    $this->rowInvokeOperation($row, P_DB_ROW_IMPORT, 'IMPORTER');
156
  }
157
158
  /**
159
   * Export data from object properties into DB row for further use
160
   *
161
   * @param bool $withDbId - Should dbId too be returned. Useful for INSERT statements
162
   *
163
   * @return array
164
   */
165
  public function exportDbRow($withDbId = true) {
166
    $row = array();
167
168
    $this->rowInvokeOperation($row, P_DB_ROW_EXPORT, 'EXPORTER');
169
170
    if (!$withDbId) {
171
      unset($row[$this->getIdFieldName()]);
172
    }
173
174
    return $row;
175
  }
176
177
178
  public function __get($name) {
179
    return $this->_container->$name;
180
  }
181
182
  public function __set($name, $value) {
183
    $this->_container->$name = $value;
184
  }
185
186
  public function __isset($name) {
187
    return isset($this->_container->$name);
188
  }
189
190
  public function __unset($name) {
191
    unset($this->_container->$name);
192
  }
193
194
}
195