Completed
Push — work-fleets ( bd15ac...5c3a01 )
by SuperNova.WS
06:16
created

ContainerAccessors::getDirect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
ccs 0
cts 3
cp 0
crap 2
1
<?php
2
3
use \Common\ContainerMagic;
4
use \Common\IContainerAccessors;
5
6
/**
7
 * Class ContainerAccessors
8
 *
9
 * Support accessors for properties: getter, setter, unsetter
10
 *
11
 * Below $that - is a shortcut for container object which will be passed to accessor
12
 *
13
 * Getter is a callable like
14
 *    function ($this) {}
15
 *
16
 * Setter is a callable like
17
 *    function ($that, $value)  {}
18
 *
19
 * Unsetter is a callable like
20
 *    function ($that) {}
21
 *
22
 * Use setDirect() and getDirect() methods to access same variable for setter/getter function!
23
 * If setter works with other object properties it needs an unsetter to handle clearProperties() method
24
 *
25
 */
26
class ContainerAccessors extends ContainerMagic implements IContainerAccessors {
27
  /**
28
   * Array of accessors - getters/setters/etc
29
   *
30
   * @var callable[][]
31
   */
32
  protected $accessors = array();
33
34 1
  public function setDirect($name, $value) {
35 1
    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 setDirect()). 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...
36 1
  }
37
38
  public function getDirect($name) {
39
    parent::__get($name);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (__get() instead of getDirect()). 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...
40
  }
41
42 1
  public function assignAccessor($varName, $type, $callable) {
43 1
    if (empty($callable)) {
44 1
      return;
45
    }
46
47 1
    if (is_callable($callable)) {
48 1
      $this->accessors[$varName][$type] = $callable;
49 1
    } else {
50 1
      throw new Exception('Error assigning callable in ' . get_called_class() . '! Callable typed [' . $type . '] is not a callable or not accessible in the scope');
51
    }
52 1
  }
53
54
  /**
55
   * Performs $processor operation on property with specified name
56
   *
57
   * @param string     $processor
58
   * @param string     $name
59
   * @param null|mixed $value
60
   *
61
   * @return mixed
62
   */
63
  protected function performMagic($processor, $name, $value = null) {
64
    if (
65
      !empty($this->accessors[$name][$processor])
66
      &&
67
      is_callable($this->accessors[$name][$processor])
68
    ) {
69
      return call_user_func($this->accessors[$name][$processor], $this, $value);
70
    } else {
71
      return parent::$processor($name, $value);
72
    }
73
  }
74
75 1
  public function __set($name, $value) {
76 1
    if (is_callable($value)) {
77 1
      $this->accessors[$name][P_CONTAINER_GET] = $value;
78 1
    } else {
79 1
      $this->performMagic(P_CONTAINER_SET, $name, $value);
80
    }
81 1
  }
82
83 1
  public function __get($name) {
84 1
    return $this->performMagic(P_CONTAINER_GET, $name, null);
85
  }
86
87
  public function __unset($name) {
88
    $this->performMagic(P_CONTAINER_UNSET, $name, null);
89
  }
90
91 1
  public function __isset($name) {
92
    // TODO - or here already can isset($this->name) ????
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% 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...
93 1
    $value = $this->$name;
94
95 1
    return isset($value);
96
  }
97
98
}
99