Total Complexity | 47 |
Total Lines | 146 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like App often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use App, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | class App extends Prefab{ |
||
4 | public $db; |
||
|
|||
5 | public $app; |
||
6 | public $session; |
||
7 | public $authenticatedUser; |
||
8 | |||
9 | public function __construct() { |
||
10 | $this->app = $this->app ?: Base::instance(); |
||
11 | } |
||
12 | |||
13 | static public function singleton() { |
||
14 | if (Registry::exists('APP')) |
||
15 | $app = Registry::get('APP'); |
||
16 | else { |
||
17 | $app = new self; |
||
18 | Registry::set('APP',$app); |
||
19 | } |
||
20 | return $app; |
||
21 | } |
||
22 | |||
23 | public function db() { |
||
24 | return $this->db ?: $this->app->DB; |
||
25 | } |
||
26 | |||
27 | public function session() { |
||
28 | return $this->session ?: $this->app->SESSION; |
||
29 | } |
||
30 | |||
31 | public function user() { |
||
32 | return $this->authenticatedUser = $this->app->get('SESSION.USER')?:false; |
||
33 | } |
||
34 | |||
35 | public function initialized($def = null) { |
||
36 | return null !== $def ? $this->app->set('INITIALIZED', $def) : $this->app->get('INITIALIZED'); |
||
37 | } |
||
38 | |||
39 | public function config($key = null) { |
||
40 | return $this->app->get($key?:'CONFIG') ; |
||
41 | } |
||
42 | |||
43 | public function status() { |
||
44 | return $this->app->IS_LIVE; |
||
45 | } |
||
46 | |||
47 | public function run() { |
||
48 | $this->loadConfig(); |
||
49 | $this->loadRoutes(); |
||
50 | $this->checkForMaintenance(); |
||
51 | $this->configureDebug(); |
||
52 | $this->configureDB(); |
||
53 | $this->configureSession(); |
||
54 | $this->configureAssets(); |
||
55 | $this->registerErrorHandler(); |
||
56 | $this->initialized(true); |
||
57 | $this->app->run(); |
||
58 | } |
||
59 | |||
60 | public function loadRoutes($file = null){ |
||
62 | } |
||
63 | |||
64 | public function loadConfig($file = null){ |
||
65 | $this->_load('config', $file); |
||
66 | } |
||
67 | |||
68 | private function _load($path, $file = null) { |
||
69 | if($file) { |
||
70 | $this->app->config(base_path("{path}/{$file}")); |
||
71 | }else{ |
||
72 | foreach(glob(base_path("{$path}/*.ini")) as $file) { |
||
73 | $this->app->config($file); |
||
74 | } |
||
75 | } |
||
76 | } |
||
77 | |||
78 | public function checkForMaintenance() { |
||
79 | if(!$this->status()) { |
||
80 | template('maintenance'); |
||
81 | exit(); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | public function configureDebug() { |
||
86 | if(!$this->app->DEV) { |
||
87 | $this->app->set('DEBUG', 0); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | public function configureDB() { |
||
102 | } |
||
103 | |||
104 | public function configureSession() { |
||
105 | $type = strtolower($this->app->SESSION); |
||
106 | |||
107 | if($type) { |
||
108 | if($this->app->CSRF && ('jig' == $type || 'sql' == $type || 'mongo' == $type)) { |
||
109 | $this->configureCSRF($type); |
||
110 | }else if($this->app->CSRF){ |
||
111 | $this->session = new Session(null, 'CSRF'); |
||
112 | }else{ |
||
113 | if($type == 'jig' || $type == 'mongo' || $type == 'sql'){ |
||
114 | $session = str_ireplace('/', '', 'DB\/'.$this->_getDBType($type).'\Session'); |
||
115 | $this->session = new $session($this->app->DB); |
||
116 | }else{ |
||
117 | $this->session = new Session(); |
||
118 | } |
||
119 | } |
||
120 | $this->app->set('SESSION', $this->session); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | public function configureCSRF($type) { |
||
127 | } |
||
128 | |||
129 | private function _getDBType($type) { |
||
130 | if($type == 'jig' || $type == 'mongo'){ |
||
131 | return ucfirst($type); |
||
132 | }else if($type == 'sql') { |
||
133 | return strtoupper($type); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | public function configureAssets() { |
||
141 | }); |
||
142 | } |
||
143 | |||
144 | public function registerErrorHandler() { |
||
145 | if($this->app->DEV) { |
||
146 | Falsum\Run::handler($this->app->DEBUG != 3); |
||
147 | }else{ |
||
148 | $this->app->set('ONERROR', 'App\Core\Controllers\ErrorController->init'); |
||
149 | } |
||
150 | } |
||
151 | } |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.