| Conditions | 17 | 
| Paths | 1152 | 
| Total Lines | 57 | 
| Code Lines | 25 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 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 namespace JackJoe\ActivityLog\Models; | ||
| 49 | public static function log($data = []) | ||
|  | |||
| 50 |   { | ||
| 51 | // set the defaults from config | ||
| 52 |     $defaults = config('activity-log.defaults'); | ||
| 53 |     if (!is_array($defaults)) { | ||
| 54 | $defaults = []; | ||
| 55 | } | ||
| 56 | |||
| 57 |     if (is_object($data)) { | ||
| 58 | $data = (array) $data; | ||
| 59 | } | ||
| 60 | |||
| 61 | // conversion path to content | ||
| 62 |     if (isset($data['contentType']) && !isset($data['content'])) { | ||
| 63 | $data['content'] = $data['contentType']; | ||
| 64 | } | ||
| 65 | |||
| 66 | // conversion path to state | ||
| 67 |     if (isset($data['description']) && !isset($data['state'])) { | ||
| 68 | $data['state'] = $data['description']; | ||
| 69 | } | ||
| 70 | |||
| 71 | // set the user ID | ||
| 72 |     if (config('activity-log.auto_set_user_id') && !isset($data['userId'])) { | ||
| 73 |       $user = call_user_func(config('activity-log.auth_method')); | ||
| 74 | $data['userId'] = isset($user->id) ? $user->id : null; | ||
| 75 | } | ||
| 76 | |||
| 77 | // set IP address | ||
| 78 |     if (!isset($data['ipAddress'])) { | ||
| 79 | $data['ipAddress'] = Request::getClientIp(); | ||
| 80 | } | ||
| 81 | |||
| 82 | // set user agent | ||
| 83 |     if (!isset($data['userAgent'])) { | ||
| 84 | $data['userAgent'] = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'No User Agent'; | ||
| 85 | } | ||
| 86 | |||
| 87 | // set additional data and encode it as JSON if it is an array or an object | ||
| 88 |     if (isset($data['data']) && (is_array($data['data']) || is_object($data['data']))) { | ||
| 89 | $data['data'] = json_encode($data['data']); | ||
| 90 | } | ||
| 91 | |||
| 92 | // format array keys to snake case for insertion into database | ||
| 93 | $dataFormatted = []; | ||
| 94 |     foreach ($data as $key => $value) { | ||
| 95 | $dataFormatted[snake_case($key)] = $value; | ||
| 96 | } | ||
| 97 | |||
| 98 | // merge defaults array with formatted data array | ||
| 99 | $data = array_merge($defaults, $dataFormatted); | ||
| 100 | |||
| 101 | // create the record | ||
| 102 | $self = static::create($data); | ||
| 103 | |||
| 104 | return $self; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | 
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: