| Total Complexity | 41 |
| Total Lines | 230 |
| Duplicated Lines | 0 % |
| Changes | 17 | ||
| Bugs | 1 | Features | 1 |
Complex classes like Log 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 Log, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Log implements LoggerInterface { |
||
| 12 | |||
| 13 | private $channel; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @param string $channel |
||
| 17 | */ |
||
| 18 | public function __construct($channel = '') { |
||
| 24 | } |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param mixed $message |
||
| 29 | * @param array $context |
||
| 30 | */ |
||
| 31 | public function alert($message, $context = []): void |
||
| 32 | { |
||
| 33 | try { |
||
| 34 | $logger = LoggerFactory::getInstance($this->channel, $context); |
||
| 35 | $message = FormatHelper::stringfyMessage($message); |
||
| 36 | $logger->alert($message, (array) $context); |
||
| 37 | } catch (\Exception $e) { |
||
| 38 | //there may be too many open files |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param mixed $message |
||
| 44 | * @param array $context |
||
| 45 | */ |
||
| 46 | public function critical($message, $context = []): void |
||
| 47 | { |
||
| 48 | try { |
||
| 49 | $logger = LoggerFactory::getInstance($this->channel, $context); |
||
| 50 | $message = FormatHelper::stringfyMessage($message); |
||
| 51 | $logger->critical($message, (array) $context); |
||
| 52 | } catch (\Exception $e) { |
||
| 53 | //there may be too many open files |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param mixed $message |
||
| 59 | * @param array $context |
||
| 60 | */ |
||
| 61 | public function error($message, $context = []): void |
||
| 62 | { |
||
| 63 | if ($this->isDebugEnabled(Logger::ERROR)) { |
||
| 64 | try { |
||
| 65 | $logger = LoggerFactory::getInstance($this->channel, $context); |
||
| 66 | $message = FormatHelper::stringfyMessage($message); |
||
| 67 | $logger->error($message, (array) $context); |
||
| 68 | } catch (\Exception $e) { |
||
| 69 | //there may be too many open files |
||
| 70 | } |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param mixed $message |
||
| 76 | * @param array $context |
||
| 77 | */ |
||
| 78 | public function warning($message, $context = []): void |
||
| 79 | { |
||
| 80 | if ($this->isDebugEnabled(Logger::WARNING)) { |
||
| 81 | try { |
||
| 82 | $logger = LoggerFactory::getInstance($this->channel, $context); |
||
| 83 | $message = FormatHelper::stringfyMessage($message); |
||
| 84 | $logger->warning($message, (array) $context); |
||
| 85 | } catch (\Exception $e) { |
||
| 86 | //there may be too many open files |
||
| 87 | } |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param mixed $message |
||
| 93 | * @param array $context |
||
| 94 | */ |
||
| 95 | public function notice($message, $context = []): void |
||
| 96 | { |
||
| 97 | if ($this->isDebugEnabled(Logger::NOTICE)) { |
||
| 98 | try { |
||
| 99 | $logger = LoggerFactory::getInstance($this->channel, $context); |
||
| 100 | $message = FormatHelper::stringfyMessage($message); |
||
| 101 | $logger->notice($message, (array) $context); |
||
| 102 | } catch (\Exception $e) { |
||
| 103 | //there may be too many open files |
||
| 104 | } |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param mixed $message |
||
| 110 | * @param array $context |
||
| 111 | */ |
||
| 112 | public function info($message, $context = []): void |
||
| 113 | { |
||
| 114 | if ($this->isDebugEnabled(Logger::INFO)) { |
||
| 115 | try { |
||
| 116 | $logger = LoggerFactory::getInstance($this->channel, $context); |
||
| 117 | $message = FormatHelper::stringfyMessage($message); |
||
| 118 | $logger->info($message, (array) $context); |
||
| 119 | } catch (\Exception $e) { |
||
| 120 | //there may be too many open files |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param mixed $message |
||
| 127 | * @param array $context |
||
| 128 | */ |
||
| 129 | public function debug($message, $context = []): void |
||
| 130 | { |
||
| 131 | if ($this->isDebugEnabled(Logger::DEBUG)) { |
||
| 132 | try { |
||
| 133 | $logger = LoggerFactory::getInstance($this->channel, $context); |
||
| 134 | $message = FormatHelper::stringfyMessage($message); |
||
| 135 | $logger->debug($message, (array) $context); |
||
| 136 | } catch (\Exception $e) { |
||
| 137 | //there may be too many open files |
||
| 138 | } |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param mixed $message |
||
| 144 | * @param array $context |
||
| 145 | */ |
||
| 146 | public function emergency($message, $context = []): void |
||
| 147 | { |
||
| 148 | try { |
||
| 149 | $logger = LoggerFactory::getInstance($this->channel, $context); |
||
| 150 | $message = FormatHelper::stringfyMessage($message); |
||
| 151 | $logger->emergency($message, (array) $context); |
||
| 152 | } catch (\Exception $e) { |
||
| 153 | //there may be too many open files |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param mixed $level |
||
| 159 | * @param mixed $message |
||
| 160 | * @param array $context |
||
| 161 | */ |
||
| 162 | public function log($level, $message, $context = []): void |
||
| 163 | { |
||
| 164 | if ($this->isDebugEnabled($level)) { |
||
| 165 | try { |
||
| 166 | $logger = LoggerFactory::getInstance($this->channel, $context); |
||
| 167 | $message = FormatHelper::stringfyMessage($message); |
||
| 168 | $logger->log($level, $message, (array) $context); |
||
| 169 | } catch (\Exception $e) { |
||
| 170 | //there may be too many open files |
||
| 171 | } |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param mixed $level |
||
| 177 | * @param mixed $message |
||
| 178 | * @param array $context |
||
| 179 | */ |
||
| 180 | public function telegram($level, $message, $context = []): void |
||
| 181 | { |
||
| 182 | if (!$this->isDebugEnabled($level)) { |
||
| 183 | return; |
||
| 184 | } |
||
| 185 | $this->log($level, $message, $context); |
||
| 186 | |||
| 187 | $logger = LoggerFactory::getInstance(LoggerFactory::TELEGRAM_CHANNEL, $context); |
||
| 188 | |||
| 189 | if($logger) { |
||
| 190 | try { |
||
| 191 | $message = FormatHelper::stringfyTelegramMessage($message, (array) $context); |
||
| 192 | $logger->log($level, $message, $context); |
||
| 193 | } catch (\Exception $e) { |
||
| 194 | $this->logInnerException($e); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param mixed $level |
||
| 201 | * @return bool |
||
| 202 | */ |
||
| 203 | private function isDebugEnabled($level) |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param \Exception $exception |
||
| 222 | */ |
||
| 223 | protected function logInnerException(\Exception $exception) |
||
| 224 | { |
||
| 225 | //there may be too many open files |
||
| 226 | try { |
||
| 227 | $logPath = $_SERVER['DOCUMENT_ROOT'] . ($_ENV['APP_LOG_FOLDER'] ?: '/log/'); |
||
| 228 | $logPath = rtrim($logPath, "/"); |
||
| 229 | file_put_contents($logPath . '/monolog_error.log', (string) $exception); |
||
| 230 | } catch(\Exception $e) { |
||
| 231 | |||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | public static function cleanLogs($daysAgo = 15) { |
||
| 241 | } |
||
| 242 | } |
||
| 243 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths