Completed
Push — work-fleets ( ce5b3a...e12dae )
by SuperNova.WS
05:40
created

Entity::insert()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
nc 4
nop 0
dl 0
loc 18
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
class Entity {
4
5
  /**
6
   * @var db_mysql|null $dbStatic
7
   */
8
  public static $dbStatic = null;
9
  public static $tableName = '_table';
10
  public static $idField = 'id';
11
12
  /**
13
   * @var array $row
14
   */
15
  public $row = array();
16
17
18
  /**
19
   * Buddy constructor.
20
   *
21
   * @param \Pimple\GlobalContainer $c
22
   */
23
  public function __construct($c) {
24
    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...
25
  }
26
27
  // 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...
28
  public function delete() {
29
    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...
30
  }
31
32
  /**
33
   * @return int|string
34
   */
35
  // 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...
36
  public function insert() {
37
    $query = array();
38
    foreach($this->row as $fieldName => $fieldValue) {
39
      $fieldValue = self::$dbStatic->db_escape($fieldValue);
40
      $query[] = "`{$fieldName}` = '{$fieldValue}'";
41
    }
42
43
    $query = implode(',', $query);
44
    if(empty($query)) {
45
      // TODO Exceptiion
46
      return 0;
47
    }
48
49
    self::$dbStatic->doquery("INSERT INTO `{{" . static::$tableName . "}}` SET " . $query);
50
51
    // TODO Exceptiion if result is empty
52
    return $this->row[static::$idField] = self::$dbStatic->db_insert_id();
53
  }
54
55
  public function isEmpty() {
56
    return empty($this->row);
57
  }
58
59
  public function isNew() {
60
    return empty($this->row[static::$idField]);
61
  }
62
63
}
64