Test Failed
Branch trunk (412648)
by SuperNova.WS
03:40
created

AccessLogged::blockChange()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by Gorlum 16.06.2017 14:32
4
 */
5
6
namespace Common;
7
8
9
/**
10
 * Class AccessLogged
11
 * @package Common
12
 */
13
class AccessLogged extends AccessMagic {
14
  const ACCESS_SET = null;
15
  const ACCESS_DELTA_INC = +1;
16
  const ACCESS_DELTA_DEC = -1;
17
18
  /**
19
   * Starting values of properties
20
   *
21
   * @var array $_startValues
22
   */
23
  protected $_startValues = [];
24
  /**
25
   * Changed values
26
   *
27
   * @var array $_changes
28
   */
29
  protected $_changes = [];
30
  /**
31
   * Increment/Decrement results AKA deltas
32
   *
33
   * @var array $_deltas
34
   */
35
  protected $_deltas = [];
36
37
  /**
38
   * Stored delta value which would be applied on next __set() call
39
   *
40
   * @var int|float $_currentDelta
41
   */
42
  protected $_currentDelta = self::ACCESS_SET;
43
44
  protected function blockChange($name) {
45
    if (array_key_exists($name, $this->_deltas)) {
46
      throw new \Exception(get_called_class() . '::' . $name . ' already INCREMENTED/DECREMENTED - can not CHANGE', ERR_ERROR);
47
    }
48
  }
49
50
  protected function blockDelta($name) {
51
    if (array_key_exists($name, $this->_changes)) {
52
      throw new \Exception(get_called_class() . '::' . $name . ' already changed - can not use DELTA', ERR_ERROR);
53
    }
54
  }
55
56
  protected function valueSet($name, $value) {
57
    if ($this->__isset($name)) {
58
      $this->blockChange($name);
59
60
      $this->_changes[$name] = $value;
61
    } else {
62
      $this->_startValues[$name] = $value;
63
    }
64
65
    parent::__set($name, $value);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (__set() instead of valueSet()). Are you sure this is correct? If so, you might want to change this to $this->__set().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
66
  }
67
68
  protected function valueDelta($name, $value) {
69
    $this->blockDelta($name);
70
71
    !isset($this->_deltas[$name]) ? $this->_deltas[$name] = 0 : false;
72
    !isset($this->_startValues[$name]) ? $this->_startValues[$name] = 0 : false;
73
74
    $value *= $this->_currentDelta === self::ACCESS_DELTA_DEC ? -1 : +1;
75
76
    $this->_deltas[$name] += $value;
77
78
    parent::__set($name, parent::__get($name) + $value);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (__get() instead of valueDelta()). Are you sure this is correct? If so, you might want to change this to $this->__get().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (__set() instead of valueDelta()). Are you sure this is correct? If so, you might want to change this to $this->__set().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
79
80
    $this->_currentDelta = self::ACCESS_SET;
81
  }
82
83
  public function __set($name, $value) {
84
    if ($this->_currentDelta === self::ACCESS_SET) {
85
      $this->valueSet($name, $value);
86
    } else {
87
      $this->valueDelta($name, $value);
88
    }
89
  }
90
91
//  /**
92
//   * Changes field by value
93
//   *
94
//   * Changes only $values and $_deltas
95
//   *
96
//   * @param string    $name
97
//   * @param int|float $value Default: 1
98
//   */
99
//  public function delta($name, $value = 1) {
100
//    $this->valueDelta($name, $value);
101
//  }
102
//
103
//  /**
104
//   * Increments property by value
105
//   *
106
//   * Changes only $values and $_deltas
107
//   *
108
//   * @param string    $name
109
//   * @param int|float $value Default: 1
110
//   */
111
//  public function inc($name, $value = 1) {
112
//    $this->delta($name, +$value);
113
//  }
114
//
115
//  /**
116
//   * Decrements property by value
117
//   *
118
//   * Changes only $values and $_deltas
119
//   *
120
//   * @param string    $name
121
//   * @param int|float $value Default: 1
122
//   */
123
//  public function dec($name, $value = 1) {
124
//    $this->delta($name, -$value);
125
//  }
126
127
  /**
128
   * Mark next set operation as delta increment
129
   *
130
   * @return $this
131
   */
132
  public function inc() {
133
    $this->_currentDelta = self::ACCESS_DELTA_INC;
134
135
    return $this;
136
  }
137
138
  /**
139
   * Mark next set operation as delta decrement
140
   *
141
   * @return $this
142
   */
143
  public function dec() {
144
    $this->_currentDelta = self::ACCESS_DELTA_DEC;
145
146
    return $this;
147
  }
148
149
  public function getChanges() {
150
    return $this->_changes;
151
  }
152
153
  public function getDeltas() {
154
    return $this->_deltas;
155
  }
156
157
  /**
158
   * Accepts changes
159
   *
160
   * Makes current values a start one and resets changes/deltas
161
   */
162
  public function acceptChanges() {
163
    $this->_startValues = $this->values;
164
    $this->_changes = [];
165
    $this->_deltas = [];
166
  }
167
168
  public function clear() {
169
    parent::clear();
170
    $this->acceptChanges();
171
  }
172
173
}
174