Completed
Push — work-fleets ( 125b09...7aaf68 )
by SuperNova.WS
06:43
created

Entity::getRow()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 4
b 0
f 0
nc 4
nop 1
dl 0
loc 14
ccs 0
cts 11
cp 0
crap 12
rs 9.4285
1
<?php
2
3
/**
4
 * Class Entity
5
 *
6
 * @property int|float $dbId Buddy record DB ID
7
 */
8
class Entity {
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 db_mysql|null $dbStatic
38
   */
39
  public static $dbStatic = null;
40
41
  /**
42
   * @var array $row
43
   */
44
  protected $row = array();
45
46
  /**
47
   * Buddy\Buddy constructor.
48
   *
49
   * @param \Pimple\GlobalContainer $c
50
   */
51
  public function __construct($c) {
52
    empty(static::$dbStatic) && !empty($c->db) ? static::$dbStatic = $c->db : false;
0 ignored issues
show
Documentation Bug introduced by
It seems like $c->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...
53
54
    $this->_container = new static::$_containerName();
55
    $this->_container->setProperties(static::$_properties);
56
  }
57
58
  public function getTableName() {
59
    return static::$tableName;
60
  }
61
62
  public function getIdFieldName() {
63
    return static::$idField;
64
  }
65
66
  public function load($buddyId) {
67
    classSupernova::$gc->dbRowOperator->getById($this, $buddyId);
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...
68
  }
69
70
  // 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...
71
  public function delete() {
72
    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...
73
  }
74
75
  /**
76
   * @return int|string
77
   */
78
  // 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...
79
  public function insert() {
80
    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...
81
  }
82
83
  public function isEmpty() {
84
    return empty($this->row);
85
  }
86
87
  public function isNew() {
88
    return empty($this->row[$this->getIdFieldName()]);
89
  }
90
91
  /**
92
   * @param array $row
93
   */
94
  public function setRow($row) {
95
//    $this->row = $row;
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% 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...
96
    // TODO - $row can be empty
97
    if ($this->getIdFieldName() != '') {
98
      $this->dbId = $row[$this->getIdFieldName()];
99
      unset($row[$this->getIdFieldName()]);
100
    }
101
    foreach($row as $fieldName => $fieldValue) {
102
      $this->$fieldName = $fieldValue;
103
    }
104
  }
105
106
  /**
107
   * Compiles object data into db row
108
   *
109
   * @param bool $withDbId - Should dbId too be returned. Useful for INSERT statements
110
   *
111
   * @return array
112
   */
113
  public function getRow($withDbId = true) {
114
//    $row = $this->row;
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% 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
    $row = array();
116
    foreach($this->_container->getProperties() as $fieldName => $cork) {
117
      $row[$fieldName] = $this->$fieldName;
118
    }
119
120
    if (!$withDbId) {
121
      unset($row[$this->getIdFieldName()]);
122
      unset($row['dbId']);
123
    }
124
125
    return $row;
126
  }
127
128
  public function __get($name) {
129
    return $this->_container->$name;
130
  }
131
132
  public function __set($name, $value) {
133
    $this->_container->$name = $value;
134
  }
135
136
  public function __isset($name) {
137
    return isset($this->_container->$name);
138
  }
139
140
  public function __unset($name) {
141
    unset($this->_container->$name);
142
  }
143
144
}
145