Conditions | 24 |
Paths | > 20000 |
Total Lines | 104 |
Code Lines | 61 |
Lines | 20 |
Ratio | 19.23 % |
Changes | 1 | ||
Bugs | 0 | 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 |
||
8 | public static function write($level, $msg, $method = null) |
||
9 | { |
||
10 | $log_threshold = \Config::get('log_threshold'); |
||
11 | $config = \Config::get('log',array()); |
||
12 | |||
13 | if( isset($config['drivers']['file']['log_threshold'])) |
||
14 | { |
||
15 | $log_threshold = $config['drivers']['file']['log_threshold']; |
||
16 | } |
||
17 | if ($level > $log_threshold) |
||
18 | { |
||
19 | return false; |
||
20 | } |
||
21 | $levels = array( |
||
22 | 1 => 'Error', |
||
23 | 2 => 'Warning', |
||
24 | 3 => 'Debug', |
||
25 | 4 => 'Info', |
||
26 | ); |
||
27 | $level = isset($levels[$level]) ? $levels[$level] : $level; |
||
28 | |||
29 | if (\Config::get('profiling')) |
||
30 | { |
||
31 | \Console::log($method.' - '.$msg); |
||
32 | } |
||
33 | |||
34 | $filepath = \Config::get('log_path').date('Y/m').'/'; |
||
35 | |||
36 | if ( ! is_dir($filepath)) |
||
37 | { |
||
38 | $old = umask(0); |
||
39 | |||
40 | mkdir($filepath, \Config::get('file.chmod.folders', 0777), true); |
||
41 | umask($old); |
||
42 | } |
||
43 | |||
44 | $filename = $filepath.date('Y-m-d').'_log'; |
||
45 | |||
46 | $message = ''; |
||
47 | |||
48 | if ( ! $exists = file_exists($filename)) |
||
49 | { |
||
50 | $message .= "<"."?php defined('COREPATH') or exit('No direct script access allowed'); ?".">".PHP_EOL.PHP_EOL; |
||
51 | } |
||
52 | |||
53 | if ( ! $fp = @fopen($filename, 'a')) |
||
54 | { |
||
55 | return false; |
||
56 | } |
||
57 | |||
58 | $call = ''; |
||
59 | if ( ! empty($method)) |
||
60 | { |
||
61 | $call .= $method; |
||
62 | }else{ |
||
63 | $backtrace = debug_backtrace(); |
||
64 | $i=0; |
||
65 | View Code Duplication | for(;$i<count($backtrace);$i++){ |
|
66 | $backtrace[$i]['object'] = null; |
||
67 | $break = false; |
||
68 | |||
69 | if(isset($backtrace[$i]['class'])){ |
||
70 | if(!strstr($backtrace[$i]['class'],__NAMESPACE__) |
||
71 | and !strstr($backtrace[$i]['class'],'Fuel\Core\Log')) { |
||
72 | |||
73 | // |
||
74 | if($level === 'Error'){ |
||
75 | if ($level == 'Error') var_dump($backtrace); |
||
76 | } |
||
77 | $break = true; |
||
78 | } |
||
79 | } |
||
80 | |||
81 | if($break){ |
||
82 | break; |
||
83 | } |
||
84 | } |
||
85 | if(isset($backtrace[$i])){ |
||
86 | $call .= isset($backtrace[$i]['class']) ? $backtrace[$i]['class'] : ' - '; |
||
87 | $call .= isset($backtrace[$i]['type']) ? $backtrace[$i]['type'] : ' - '; |
||
88 | $call .= isset($backtrace[$i]['function']) ? $backtrace[$i]['function'] : ' - '; |
||
89 | $call .= isset($backtrace[$i-1]['line']) ? ':'.$backtrace[$i-1]['line'] : ' - '; |
||
90 | } |
||
91 | } |
||
92 | |||
93 | $message .= $level.' '.(($level == 'info') ? ' -' : '-').' '; |
||
94 | $message .= date(\Config::get('log_date_format')); |
||
95 | $message .= ' - ' . 'ouid=' . parent::$opensocial_user_id; |
||
96 | $message .= ' --> '.(empty($call) ? '' : $call.' - ').$msg.PHP_EOL; |
||
97 | |||
98 | flock($fp, LOCK_EX); |
||
99 | fwrite($fp, $message); |
||
100 | flock($fp, LOCK_UN); |
||
101 | fclose($fp); |
||
102 | |||
103 | if ( ! $exists) |
||
104 | { |
||
105 | $old = umask(0); |
||
106 | @chmod($filename, \Config::get('file.chmod.files', 0666)); |
||
107 | umask($old); |
||
108 | } |
||
109 | |||
110 | return true; |
||
111 | } |
||
112 | } |
||
113 |
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: