Completed
Push — work-fleets ( beb57c...cb2521 )
by SuperNova.WS
07:55
created

EntityContainer::processNumeric()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 7
nc 4
nop 2
dl 0
loc 13
rs 8.2222
c 1
b 0
f 1
ccs 9
cts 9
cp 1
crap 7
1
<?php
2
3
namespace Entity;
4
5
use Common\ContainerAccessors;
6
7
/**
8
 * Class Entity\EntityContainer
9
 *
10
 * @property array $row - Entity row read from DB
11
 */
12
class EntityContainer extends ContainerAccessors {
13
  /**
14
   * @var EntityModel $model
15
   */
16
  protected $model;
17
18
  /**
19
   * @var \Common\Accessors $accessors
20
   */
21
  protected $accessors;
22
23
  /**
24
   * @var array $original
25
   */
26
  protected $original = array();
27
28
  /**
29
   * @var array $delta
30
   */
31
  protected $delta = array();
32
33
  /** @noinspection PhpMissingParentConstructorInspection */
34
  /**
35
   * Entity\EntityContainer constructor.
36
   *
37
   * @param EntityModel $model
38
   */
39 1
  public function __construct($model) {
40 1
    $this->setModel($model);
41 1
  }
42
43
  /**
44
   * @param EntityModel $model
45
   */
46 1
  public function setModel(EntityModel $model) {
47 1
    $this->model = $model;
48 1
    $this->accessors = $model->getAccessors();
49 1
  }
50
51
  /**
52
   * @return EntityModel
53
   */
54 1
  public function getModel() {
55 1
    return $this->model;
56
  }
57
58 1
  protected function processNumeric($name, $value) {
59 1
    if(!is_int($value) && !is_float($value)) {
60 1
      return;
61
    }
62
63
    // If no original value and new value is set - then we take new value as old value
64 1
    if(empty($this->original[$name]) && !empty($value)) {
65 1
      $this->original[$name] = $value;
66 1
    } elseif(!empty($this->original[$name]) && $value != $this->original[$name]) {
67
      // New value not equal original value. We should update delta
68 1
      $this->delta[$name] = $value - $this->original[$name];
69 1
    }
70 1
  }
71
72 1
  public function __set($name, $value) {
73 1
    $properties = $this->model->getProperties();
74 1
    if(isset($properties[$name][P_DB_FIELD_TYPE])) {
75 1
      $value = \classSupernova::$gc->types->castAs($properties[$name][P_DB_FIELD_TYPE], $value);
0 ignored issues
show
Bug introduced by
The method castAs does only exist in Common\Types, 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...
76 1
    }
77
78 1
    parent::__set($name, $value);
79 1
    $this->processNumeric($name, $value);
80 1
  }
81
82
}
83