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 | public function log_out( |
||
51 | $type, |
||
52 | $source, |
||
53 | $message, |
||
54 | $workflowId = null) |
||
55 | { |
||
56 | $log = [ |
||
57 | "time" => date("Y-m-d H:i:s", time()), |
||
58 | "source" => $source, |
||
59 | "type" => $type, |
||
60 | "message" => $message |
||
61 | ]; |
||
62 | |||
63 | if ($workflowId) |
||
64 | $log["workflowId"] = $workflowId; |
||
65 | |||
66 | // Open Syslog. Use programe name as key |
||
67 | if (!openlog (__FILE__, LOG_CONS|LOG_PID, LOG_LOCAL1)) |
||
68 | throw new CpeException("Unable to connect to Syslog!", |
||
69 | OPENLOG_ERROR); |
||
70 | |||
71 | // Change Syslog priority level |
||
72 | switch ($type) |
||
73 | { |
||
74 | case "INFO": |
||
75 | $priority = LOG_INFO; |
||
76 | break; |
||
77 | case "ERROR": |
||
78 | $priority = LOG_ERR; |
||
79 | break; |
||
80 | case "FATAL": |
||
81 | $priority = LOG_ALERT; |
||
82 | break; |
||
83 | case "WARNING": |
||
84 | $priority = LOG_WARNING; |
||
85 | break; |
||
86 | case "DEBUG": |
||
87 | $priority = LOG_DEBUG; |
||
88 | break; |
||
89 | default: |
||
90 | throw new CpeException("Unknown log Type!", |
||
91 | LOG_TYPE_ERROR); |
||
92 | } |
||
93 | |||
94 | // Print log in file |
||
95 | $this->print_to_file($log, $workflowId); |
||
96 | |||
97 | // Encode log message in JSON for better parsing |
||
98 | $out = json_encode($log); |
||
99 | // Send to syslog |
||
100 | syslog($priority, $out); |
||
101 | } |
||
102 | |||
124 | } |
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