Completed
Push — work-fleets ( de8005...be60df )
by SuperNova.WS
05:54
created

Entity::getTableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Class Entity
5
 *
6
 * @property int|float $dbId Buddy record DB ID
7
 */
8
class Entity {
9
  public static $tableName = '_table';
10
  /**
11
   * Name of key field field in this table
12
   *
13
   * @var string $idField
14
   */
15
  public static $idField = 'id';
16
  /**
17
   * @var PropertyHider
18
   */
19
  protected $_container;
20
  protected static $_containerName = 'PropertyHiderInArray';
21
22
  /**
23
   * Property list
24
   *
25
   * @var array
26
   */
27
  protected static $_properties = array();
28
29
  /**
30
   * @var db_mysql|null $dbStatic
31
   */
32
  public static $dbStatic = null;
33
34
  /**
35
   * @var array $row
36
   */
37
  protected $row = array();
38
39
40
  /**
41
   * @var int|float|string $dbId
42
   */
43
  protected $dbId = 0;
44
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[static::$idField]);
89
  }
90
91
  /**
92
   * @param array $row
93
   */
94
  public function setRow($row) {
95
    $this->row = $row;
96
    // TODO - $row can be empty
97
    if (!empty(static::$idField)) {
98
      $this->setDbId($row[static::$idField]);
99
    }
100
  }
101
102
  /**
103
   * Compiles object data into db row
104
   *
105
   * @param bool $withDbId - Should dbId too be returned. Usefull for INSERT statements
106
   *
107
   * @return array
108
   */
109
  public function getRow($withDbId = true) {
110
    $row = $this->row;
111
    if (!$withDbId) {
112
      unset($row[static::$idField]);
113
    }
114
115
    return $row;
116
  }
117
118
  public function setDbId($value) {
119
    $this->dbId = $value;
120
  }
121
122
  /**
123
   * @return int|float|string
124
   */
125
  public function getDbId() {
126
    return $this->dbId;
127
  }
128
129
130
131
  public function __get($name) {
132
    return $this->_container->$name;
133
  }
134
135
  public function __set($name, $value) {
136
    $this->_container->$name = $value;
137
  }
138
139
  public function __isset($name) {
140
    return isset($this->_container->$name);
141
  }
142
143
  public function __unset($name) {
144
    unset($this->_container->$name);
145
  }
146
147
}
148