Test Failed
Push — trunk ( db071f...8d464a )
by SuperNova.WS
06:33
created

AccessLogged::inc()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 7
loc 7
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
15
  /**
16
   * Starting values of properties
17
   *
18
   * @var array $_startValues
19
   */
20
  protected $_startValues = [];
21
  /**
22
   * Changed values
23
   *
24
   * @var array $_changes
25
   */
26
  protected $_changes = [];
27
  /**
28
   * Increment/Decrement results AKA deltas
29
   *
30
   * @var array $_deltas
31
   */
32
  protected $_deltas = [];
33
34
  public function __set($name, $value) {
35
    if (array_key_exists($name, $this->values)) {
36
      if(array_key_exists($name, $this->_deltas)) {
37
        throw new \Exception(get_called_class() . '::' . $name . ' already INCREMENTED/DECREMENTED - can not CHANGE', ERR_ERROR);
38
      }
39
40
      $this->_changes[$name] = $value;
41
    } else {
42
      $this->_startValues[$name] = $value;
43
    }
44
45
    parent::__set($name, $value);
46
  }
47
48
  /**
49
   * Increments field by value
50
   *
51
   * @param string    $name
52
   * @param int|float $value Default: 1
53
   */
54 View Code Duplication
  public function inc($name, $value = 1) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    if(array_key_exists($name, $this->_changes)) {
56
      throw new \Exception(get_called_class() . '::' . $name . ' already changed - can not use INCREMENT', ERR_ERROR);
57
    }
58
    $this->_deltas[$name] += $value;
59
    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 inc()). 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 inc()). 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...
60
  }
61
62
  /**
63
   * Decrements field by value
64
   *
65
   * @param string    $name
66
   * @param int|float $value Default: 1
67
   */
68 View Code Duplication
  public function dec($name, $value = 1) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    if(array_key_exists($name, $this->_changes)) {
70
      throw new \Exception(get_called_class() . '::' . $name . ' already changed - can not use DECREMENT', ERR_ERROR);
71
    }
72
    $this->_deltas[$name] -= $value;
73
    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 dec()). 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 dec()). 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...
74
  }
75
76
  public function getChanges() {
77
    return $this->_changes;
78
  }
79
80
  public function getDeltas() {
81
    return $this->_deltas;
82
  }
83
84
  /**
85
   * Flushes changes
86
   *
87
   * Makes current values a start one and resets changes/deltas
88
   */
89
  public function flush() {
90
    $this->_startValues = $this->values;
91
    $this->_changes = [];
92
    $this->_deltas = [];
93
  }
94
95
  public function clear() {
96
    parent::clear();
97
    $this->flush();
98
  }
99
100
}
101