| Total Complexity | 53 |
| Total Lines | 288 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Load 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 Load, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | abstract class Load |
||
| 40 | { |
||
| 41 | /** |
||
| 42 | * Contains the information of the old $conf global var. |
||
| 43 | * |
||
| 44 | * Config::getConf() can be used at any point to retrieve the contents of the |
||
| 45 | * $conf variable used globally by Dolibarr. |
||
| 46 | * |
||
| 47 | * The content of the variable is saved with the first call and this copy is |
||
| 48 | * returned. If it is necessary to regenerate it, the parameter true can be |
||
| 49 | * passed to it. |
||
| 50 | * |
||
| 51 | * @var null|Conf |
||
| 52 | */ |
||
| 53 | private static $config = null; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Contains a DoliDB connection. |
||
| 57 | * |
||
| 58 | * @var DoliDB, null |
||
| 59 | */ |
||
| 60 | private static $db; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Contains a HookManager class. |
||
| 64 | * |
||
| 65 | * @var $hookManager |
||
|
|
|||
| 66 | */ |
||
| 67 | private static $hook_manager; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Contains a Translate class |
||
| 71 | * |
||
| 72 | * @var Translate |
||
| 73 | */ |
||
| 74 | private static $langs; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Contains a User class instance. |
||
| 78 | * |
||
| 79 | * @var User |
||
| 80 | */ |
||
| 81 | private static $user; |
||
| 82 | |||
| 83 | private static $menu_manager; |
||
| 84 | |||
| 85 | private static $mysoc; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Returns a stdClass with the information contained in the conf.php file. |
||
| 89 | * |
||
| 90 | * @param $reload |
||
| 91 | * |
||
| 92 | * @return Conf|null |
||
| 93 | */ |
||
| 94 | public static function getConfig($reload = false): ?Conf |
||
| 95 | { |
||
| 96 | if ($reload || !isset(self::$config)) { |
||
| 97 | self::$config = self::loadConfig(); |
||
| 98 | } |
||
| 99 | |||
| 100 | return self::$config; |
||
| 101 | } |
||
| 102 | |||
| 103 | private static function loadConfig() |
||
| 104 | { |
||
| 105 | |||
| 106 | self::$config = Config::loadDolibarrConfig(); |
||
| 107 | return self::$config; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Returns a HookManager class instance. |
||
| 112 | * |
||
| 113 | * @return HookManager|null |
||
| 114 | */ |
||
| 115 | public static function getHookManager(): ?HookManager |
||
| 116 | { |
||
| 117 | if (empty(self::$hook_manager)) { |
||
| 118 | self::$hook_manager = self::loadHookManager(); |
||
| 119 | } |
||
| 120 | return self::$hook_manager; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns a HookManager class instance. |
||
| 125 | * |
||
| 126 | * @return mixed |
||
| 127 | */ |
||
| 128 | private static function loadHookManager() |
||
| 129 | { |
||
| 130 | self::$hook_manager = new HookManager(self::$db); |
||
| 131 | return self::$hook_manager; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Returns a DoliDB connection instance. |
||
| 136 | * |
||
| 137 | * @return DoliDB|null |
||
| 138 | */ |
||
| 139 | public static function getDb(): ?DoliDB |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Returns a Dolibarr DB connection (DoliDB) instance. |
||
| 154 | * |
||
| 155 | * @return DoliDb |
||
| 156 | * @throws \Exception |
||
| 157 | */ |
||
| 158 | private static function loadDb() |
||
| 159 | { |
||
| 160 | $conf = self::$config; |
||
| 161 | self::$db = getDoliDBInstance($conf->db->type, $conf->db->host, $conf->db->user, $conf->db->pass, $conf->db->name, (int)$conf->db->port); |
||
| 162 | self::$config->setValues(self::$db); |
||
| 163 | |||
| 164 | return self::$db; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Returns a Translate class instance. |
||
| 169 | * |
||
| 170 | * @return Translate|null |
||
| 171 | */ |
||
| 172 | public static function getLangs(): ?Translate |
||
| 173 | { |
||
| 174 | if (!isset(self::$langs)) { |
||
| 175 | self::$langs = self::loadLangs(); |
||
| 176 | } |
||
| 177 | return self::$langs; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Returns a Translate class instance. |
||
| 182 | * |
||
| 183 | * @return Translate |
||
| 184 | */ |
||
| 185 | private static function loadLangs() |
||
| 186 | { |
||
| 187 | self::$langs = new Translate('', self::$config); |
||
| 188 | return self::$langs; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Returns a User class instance. |
||
| 193 | * |
||
| 194 | * @return User|null |
||
| 195 | */ |
||
| 196 | public static function getUser(): ?User |
||
| 197 | { |
||
| 198 | if (!isset(self::$user)) { |
||
| 199 | self::$user = self::loadUser(); |
||
| 200 | } |
||
| 201 | return self::$user; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Returns a user class instance |
||
| 206 | * |
||
| 207 | * @return User |
||
| 208 | */ |
||
| 209 | private static function loadUser() |
||
| 210 | { |
||
| 211 | self::$user = new User(self::$db); |
||
| 212 | return self::$user; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Returns a MenuManager class instance. |
||
| 217 | * |
||
| 218 | * @return mixed |
||
| 219 | */ |
||
| 220 | public static function getMenuManager() |
||
| 221 | { |
||
| 222 | if (!isset(self::$menu_manager)) { |
||
| 223 | self::$menu_manager = self::loadMenuManager(); |
||
| 224 | } |
||
| 225 | return self::$menu_manager; |
||
| 226 | } |
||
| 227 | |||
| 228 | private static function loadMenuManager() |
||
| 268 | } |
||
| 269 | |||
| 270 | public static function getMySoc() |
||
| 271 | { |
||
| 272 | if (!isset(self::$db)) { |
||
| 273 | return null; |
||
| 274 | } |
||
| 275 | |||
| 280 | } |
||
| 281 | |||
| 282 | private static function loadMySoc() |
||
| 283 | { |
||
| 330 |