Completed
Push — trunk ( 8c8cf8...dc0cf2 )
by SuperNova.WS
04:27
created

SnPimp::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 2
rs 9.6666
c 0
b 0
f 0
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 Meta\Economic\BuildDataStatic;
11
use Que\QueUnitStatic;
12
use template;
13
14
/**
15
 * Class SnPimp
16
 *
17
 * Used to declare commonly known methods to make it easier for IDEs
18
 *
19
 * @package Core
20
 *
21
 * @method void tpl_render_topnav(array &$user = null, array $planetrow, template $template) - Add some elements to ResourceBar
22
 * @method array que_unit_make_sql(int $unit_id, array $user, array $planet = [], array $build_data = [], float $unit_level = 0, float $unit_amount = 1, int $build_mode = BUILD_CREATE)
23
 * @method float getStructuresTimeDivisor(array $user, array $planet, int $unit_id, array $unit_data)
24
 *
25
 * @method void|string allyInfoView(callable $c = null) - renders extra elements on Alliance internal main page
26
 * @method void|string allyInternalMainModel(callable $c = null) - extra model on main Alliance page
27
 */
28
class SnPimp extends Pimp {
29
  const MODE_NORMAL = 0; // Normal mode - call hooker
30
  const MODE_ADD = 1; // Add mode - syntax sugar - add named function
31
  const MODE_NAME = 2; // Name mode - syntax sugar - return function name
32
33
  protected $mode = self::MODE_NORMAL;
34
  protected $registerOrder = Pimp::ORDER_AS_IS;
35
36
  public function __construct(GlobalContainer $gc) {
37
    parent::__construct($gc);
38
39
    /** @noinspection PhpParamsInspection */
40
    $this->add()->tpl_render_topnav($t = 'sn_tpl_render_topnav', [], null);
0 ignored issues
show
Bug introduced by
$t = 'sn_tpl_render_topnav' of type string is incompatible with the type array expected by parameter $&$user of Core\SnPimp::tpl_render_topnav(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
    $this->add()->tpl_render_topnav(/** @scrutinizer ignore-type */ $t = 'sn_tpl_render_topnav', [], null);
Loading history...
41
    /** @noinspection PhpParamsInspection */
42
    $this->add()->que_unit_make_sql([QueUnitStatic::class, 'que_unit_make_sql'], []);
0 ignored issues
show
Bug introduced by
array(Que\QueUnitStatic:...s, 'que_unit_make_sql') of type array<integer,string> is incompatible with the type integer expected by parameter $unit_id of Core\SnPimp::que_unit_make_sql(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
    $this->add()->que_unit_make_sql(/** @scrutinizer ignore-type */ [QueUnitStatic::class, 'que_unit_make_sql'], []);
Loading history...
43
    /** @noinspection PhpParamsInspection */
44
    $this->add()->getStructuresTimeDivisor([BuildDataStatic::class, 'getStructuresTimeDivisor'], [], 0, []);
45
  }
46
47
  /**
48
   * Changes pimp mode to "ADD"
49
   *
50
   * A bit of syntax sugar
51
   * Allows constructions like $this->add()->methodName($callable, ...)
52
   * Helps to maintain uniformity of method names throw the code
53
   *
54
   * @return $this
55
   * @throws \Exception
56
   */
57
  public function add($order = Pimp::ORDER_AS_IS) {
58
    if ($this->mode != static::MODE_NORMAL) {
59
      throw new \Exception('Pimp::add() - mode already set');
60
    }
61
62
    $this->mode = static::MODE_ADD;
63
    $this->registerOrder = $order;
64
65
    return $this;
66
  }
67
68
  /**
69
   * Changes pimp mode to "NAME"
70
   *
71
   * Calling pimp method in NAME mode will return name of called method
72
   *
73
   * A bit of syntax sugar
74
   * Allows constructions like $this->name()->methodName(...)
75
   * Helps to maintain uniformity of method names throw the code
76
   * Used mainly as array indexes
77
   *
78
   * @return $this
79
   * @throws \Exception
80
   */
81
  public function name() {
82
    if ($this->mode != static::MODE_NORMAL) {
83
      throw new \Exception('Pimp::name() - mode already set');
84
    }
85
86
    $this->mode = static::MODE_NAME;
87
88
    return $this;
89
  }
90
91
  /**
92
   * @param $name
93
   * @param $arguments
94
   *
95
   * @return static|mixed|null
96
   */
97
  public function __call($name, $arguments) {
98
    if ($this->mode == static::MODE_NORMAL) {
99
      return parent::__call($name, $arguments);
100
    }
101
102
    if ($this->mode == static::MODE_ADD) {
103
      $this->register($name, reset($arguments), $this->registerOrder);
104
      $this->mode = static::MODE_NORMAL;
105
      $this->registerOrder = Pimp::ORDER_AS_IS;
106
107
      return $this;
108
    }
109
110
    if ($this->mode == static::MODE_NAME) {
111
      $this->mode = static::MODE_NORMAL;
112
113
      return $name;
114
    }
115
116
    return null;
117
  }
118
119
  public function getHooker($name) {
120
    return !empty($this->hookers[$name]) ? $this->hookers[$name] : null;
121
  }
122
123
}
124