Conditions | 8 |
Paths | 14 |
Total Lines | 52 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
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 |
||
50 | |||
51 | // Log message to syslog and log file |
||
52 | public function log_out( |
||
53 | $type, |
||
54 | $source, |
||
55 | $message, |
||
56 | $workflowId = null) |
||
57 | { |
||
58 | $log = [ |
||
59 | "time" => date("Y-m-d H:i:s", time()), |
||
60 | "source" => $source, |
||
61 | "type" => $type, |
||
62 | "message" => $message |
||
63 | ]; |
||
64 | |||
65 | if ($workflowId) |
||
66 | $log["workflowId"] = $workflowId; |
||
67 | |||
68 | // Open Syslog. Use programe name as key |
||
69 | if (!openlog (__FILE__, LOG_CONS|LOG_PID, LOG_LOCAL1)) |
||
70 | throw new CpeException("Unable to connect to Syslog!", |
||
71 | OPENLOG_ERROR); |
||
72 | |||
73 | // Change Syslog priority level |
||
74 | switch ($type) |
||
75 | { |
||
76 | case "INFO": |
||
77 | $priority = LOG_INFO; |
||
78 | break; |
||
79 | case "ERROR": |
||
80 | $priority = LOG_ERR; |
||
81 | break; |
||
82 | case "FATAL": |
||
83 | $priority = LOG_ALERT; |
||
84 | break; |
||
85 | case "WARNING": |
||
86 | $priority = LOG_WARNING; |
||
87 | break; |
||
88 | case "DEBUG": |
||
89 | $priority = LOG_DEBUG; |
||
90 | break; |
||
91 | default: |
||
92 | throw new CpeException("Unknown log Type!", |
||
93 | LOG_TYPE_ERROR); |
||
94 | } |
||
95 | |||
96 | // Print log in file |
||
97 | $this->print_to_file($log, $workflowId); |
||
98 | |||
99 | // Encode log message in JSON for better parsing |
||
100 | $out = json_encode($log); |
||
101 | // Send to syslog |
||
102 | syslog($priority, $out); |
||
126 | } |
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state