1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Gorlum 18.03.2018 17:53 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Core; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
use Common\Hooker\Pimp; |
10
|
|
|
use template; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class SnPimp |
14
|
|
|
* |
15
|
|
|
* Used to declare commonly known methods to make it easier for IDEs |
16
|
|
|
* |
17
|
|
|
* @package Core |
18
|
|
|
* |
19
|
|
|
* @method void|string allyInfoView(callable $c = null) - renders extra elements on Alliance internal main page |
20
|
|
|
* @method void|string allyInternalMainModel(callable $c = null) - extra model on main Alliance page |
21
|
|
|
* @method void|string tpl_render_topnav(array|string|callable &$user = null, array $planetrow, template $template) - Add some elements to ResourceBar |
22
|
|
|
*/ |
23
|
|
|
class SnPimp extends Pimp { |
24
|
|
|
const MODE_NORMAL = 0; // Normal mode - call hooker |
25
|
|
|
const MODE_ADD = 1; // Add mode - syntax sugar - add named function |
26
|
|
|
const MODE_NAME = 2; // Name mode - syntax sugar - return function name |
27
|
|
|
|
28
|
|
|
protected $mode = self::MODE_NORMAL; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Changes pimp mode to "ADD" |
32
|
|
|
* |
33
|
|
|
* A bit of syntax sugar |
34
|
|
|
* Allows constructions like $this->add()->methodName($callable, ...) |
35
|
|
|
* Helps to maintain uniformity of method names throw the code |
36
|
|
|
* |
37
|
|
|
* @return $this |
38
|
|
|
* @throws \Exception |
39
|
|
|
*/ |
40
|
|
|
public function add() { |
41
|
|
|
if ($this->mode != static::MODE_NORMAL) { |
42
|
|
|
throw new \Exception('Pimp::add() - mode already set'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->mode = static::MODE_ADD; |
46
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Changes pimp mode to "NAME" |
52
|
|
|
* |
53
|
|
|
* Calling pimp method in NAME mode will return name of called method |
54
|
|
|
* |
55
|
|
|
* A bit of syntax sugar |
56
|
|
|
* Allows constructions like $this->name()->methodName(...) |
57
|
|
|
* Helps to maintain uniformity of method names throw the code |
58
|
|
|
* Used mainly as array indexes |
59
|
|
|
* |
60
|
|
|
* @return $this |
61
|
|
|
* @throws \Exception |
62
|
|
|
*/ |
63
|
|
|
public function name() { |
64
|
|
|
if ($this->mode != static::MODE_NORMAL) { |
65
|
|
|
throw new \Exception('Pimp::name() - mode already set'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->mode = static::MODE_NAME; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $name |
75
|
|
|
* @param $arguments |
76
|
|
|
* |
77
|
|
|
* @return static|mixed|null |
78
|
|
|
*/ |
79
|
|
|
public function __call($name, $arguments) { |
80
|
|
|
if ($this->mode == static::MODE_NORMAL) { |
81
|
|
|
// var_dump($name, $arguments); |
|
|
|
|
82
|
|
|
|
83
|
|
|
return parent::__call($name, $arguments); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($this->mode == static::MODE_ADD) { |
87
|
|
|
$this->register($name, reset($arguments)); |
88
|
|
|
$this->mode = static::MODE_NORMAL; |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ($this->mode == static::MODE_NAME) { |
94
|
|
|
$this->mode = static::MODE_NORMAL; |
95
|
|
|
|
96
|
|
|
return $name; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getHooker($name) { |
103
|
|
|
return !empty($this->hookers[$name]) ? $this->hookers[$name] : null; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
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.