1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Common; |
4
|
|
|
|
5
|
|
|
use \Common\ContainerMagic; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Common\ContainerAccessors |
9
|
|
|
* |
10
|
|
|
* Support accessors for properties: getter, setter, unsetter |
11
|
|
|
* |
12
|
|
|
* Below $that - is a shortcut for container object which will be passed to accessor |
13
|
|
|
* |
14
|
|
|
* Getter is a callable like |
15
|
|
|
* function ($this) {} |
16
|
|
|
* |
17
|
|
|
* Setter is a callable like |
18
|
|
|
* function ($that, $value) {} |
19
|
|
|
* |
20
|
|
|
* Unsetter is a callable like |
21
|
|
|
* function ($that) {} |
22
|
|
|
* |
23
|
|
|
* Use setDirect() and getDirect() methods to access same variable for setter/getter function! |
24
|
|
|
* If setter works with other object properties it needs an unsetter to handle clearProperties() method |
25
|
|
|
* |
26
|
|
|
*/ |
27
|
|
|
class ContainerAccessors extends ContainerMagic { |
28
|
|
|
/** |
29
|
|
|
* @var \Common\Accessors $accessors |
30
|
|
|
*/ |
31
|
|
|
protected $accessors; |
32
|
|
|
|
33
|
|
|
public function __construct() { |
34
|
|
|
$this->accessors = new \Common\Accessors(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param \Common\Accessors $accessors |
39
|
|
|
*/ |
40
|
|
|
public function setAccessors($accessors) { |
41
|
|
|
$this->accessors = $accessors; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Performs accessor operation on property with specified name |
46
|
|
|
* |
47
|
|
|
* @param string $varName |
48
|
|
|
* @param string $accessor |
49
|
|
|
* @param null|mixed $value |
50
|
|
|
* |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
1 |
|
protected function performMagic($varName, $accessor, $value = null) { |
54
|
1 |
|
if ($this->accessors->haveAccessor($varName, $accessor)) { |
55
|
1 |
|
return $this->accessors->invokeAccessor($varName, $accessor, array($this, $value)); |
56
|
|
|
} else { |
57
|
1 |
|
return parent::$accessor($varName, $value); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
1 |
|
public function __set($name, $value) { |
63
|
1 |
|
if (is_callable($value)) { |
64
|
1 |
|
$this->accessors->setAccessor($name, P_CONTAINER_GET, $value); |
65
|
1 |
|
} else { |
66
|
1 |
|
$this->performMagic($name, P_CONTAINER_SET, $value); |
67
|
|
|
} |
68
|
1 |
|
} |
69
|
|
|
|
70
|
1 |
|
public function __get($name) { |
71
|
1 |
|
return $this->performMagic($name, P_CONTAINER_GET, null); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
public function __unset($name) { |
75
|
1 |
|
$this->performMagic($name, P_CONTAINER_UNSET, null); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
1 |
|
public function __isset($name) { |
79
|
|
|
// TODO - or here already can isset($this->name) ???? |
|
|
|
|
80
|
1 |
|
$value = $this->$name; |
81
|
|
|
|
82
|
1 |
|
return isset($value); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Direct access to parent class setter |
88
|
|
|
* |
89
|
|
|
* @param string $name |
90
|
|
|
* @param mixed $value |
91
|
|
|
*/ |
92
|
1 |
|
public function setDirect($name, $value) { |
93
|
1 |
|
ContainerMagic::__set($name, $value); |
94
|
1 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Direct access to parent class getter |
98
|
|
|
* |
99
|
|
|
* @param string $name |
100
|
|
|
* |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
1 |
|
public function getDirect($name) { |
104
|
1 |
|
return ContainerMagic::__get($name); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Direct access to parent class unsetter |
109
|
|
|
* |
110
|
|
|
* @param string $name |
111
|
|
|
*/ |
112
|
1 |
|
public function unsetDirect($name) { |
113
|
1 |
|
ContainerMagic::__unset($name); |
114
|
1 |
|
} |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
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.