Conditions | 23 |
Paths | > 20000 |
Total Lines | 83 |
Code Lines | 52 |
Lines | 20 |
Ratio | 24.1 % |
Changes | 3 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
16 | public static function write($level, $msg, $method = null) |
||
17 | { |
||
18 | $log_threshold = \Config::get('log_threshold'); |
||
19 | $config = \Config::get('log', array()); |
||
20 | |||
21 | if (isset($config['drivers']['td']['log_threshold'])) |
||
22 | { |
||
23 | $log_threshold = $config['drivers']['td']['log_threshold']; |
||
24 | } |
||
25 | if ($level > $log_threshold) |
||
26 | { |
||
27 | return false; |
||
28 | } |
||
29 | $levels = array( |
||
30 | 1 => 'Error', |
||
31 | 2 => 'Warning', |
||
32 | 3 => 'Debug', |
||
33 | 4 => 'Info', |
||
34 | ); |
||
35 | $level = isset($levels[$level]) ? $levels[$level] : $level; |
||
36 | |||
37 | if (\Config::get('profiling')) |
||
38 | { |
||
39 | \Console::log($method.' - '.$msg); |
||
40 | } |
||
41 | |||
42 | $host = empty($config['drivers']['td']['host']) ? null : $config['drivers']['td']['host']; |
||
43 | $port = empty($config['drivers']['td']['port']) ? null : $config['drivers']['td']['port']; |
||
44 | $options = empty($config['drivers']['td']['options']) ? array() : $config['drivers']['td']['options']; |
||
45 | $packer = empty($config['drivers']['td']['packer']) ? null : $config['drivers']['td']['packer']; |
||
46 | $database = empty($config['drivers']['td']['database']) ? 'default' : $config['drivers']['td']['database']; |
||
47 | |||
48 | $logger = new \Fluent\Logger\FluentLogger($host,$port,$options,$packer); |
||
49 | |||
50 | $message = array(); |
||
51 | |||
52 | $call = array(); |
||
53 | if ( ! empty($method)) |
||
54 | { |
||
55 | $call['method'] = $method; |
||
56 | }else{ |
||
57 | $backtrace = debug_backtrace(); |
||
58 | $i=0; |
||
59 | View Code Duplication | for(;$i<count($backtrace);$i++){ |
|
60 | $backtrace[$i]['object'] = null; |
||
61 | $break = false; |
||
62 | |||
63 | if(isset($backtrace[$i]['class'])){ |
||
64 | if(!strstr($backtrace[$i]['class'],__NAMESPACE__) |
||
65 | and !strstr($backtrace[$i]['class'],'Fuel\Core\Log')) { |
||
66 | |||
67 | // |
||
68 | if($level === 'Error'){ |
||
69 | $msg = print_r($backtrace,true); |
||
70 | } |
||
71 | $break = true; |
||
72 | } |
||
73 | } |
||
74 | |||
75 | if($break){ |
||
76 | break; |
||
77 | } |
||
78 | } |
||
79 | if(isset($backtrace[$i])){ |
||
80 | $call['class'] = isset($backtrace[$i]['class']) ? $backtrace[$i]['class'] : 'null'; |
||
81 | $call['type'] = isset($backtrace[$i]['type']) ? $backtrace[$i]['type'] : 'null'; |
||
82 | $call['function'] = isset($backtrace[$i]['function']) ? $backtrace[$i]['function'] : 'null'; |
||
83 | $call['line'] = isset($backtrace[$i-1]['line']) ? $backtrace[$i-1]['line'] : 'null'; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | $message['level'] = $level; |
||
88 | $message['date'] = date(\Config::get('log_date_format')); |
||
89 | $message['msg'] = $msg; |
||
90 | $message['call'] = $call; |
||
91 | |||
92 | $res = $logger->post('td.'.$database.'.fuel_log',$message); |
||
93 | if(!$res){ |
||
94 | return false; |
||
95 | } |
||
96 | |||
97 | return true; |
||
98 | } |
||
99 | } |
||
100 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: