| Conditions | 12 |
| Paths | 41 |
| Total Lines | 68 |
| Code Lines | 32 |
| 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 |
||
| 50 | public function store(\Exception $exception) |
||
| 51 | { |
||
| 52 | # simple way to generate a unique id |
||
| 53 | $id = time() . uniqid(); |
||
| 54 | |||
| 55 | $data = []; |
||
| 56 | |||
| 57 | if (file_exists($this->outputFile)) |
||
| 58 | { |
||
| 59 | $string = file_get_contents($this->outputFile); |
||
| 60 | |||
| 61 | if (!empty($string)) |
||
| 62 | { |
||
| 63 | $data = json_decode($string, true); |
||
| 64 | |||
| 65 | # json_encode can return TRUE, FALSE or NULL (http://php.net/manual/en/function.json-decode.php) |
||
| 66 | if (is_null($data) || $data === false) |
||
| 67 | { |
||
| 68 | $this->error(Errno::JSON_DECODE_ERROR, $this->outputFile); |
||
|
|
|||
| 69 | return false; |
||
| 70 | } |
||
| 71 | } |
||
| 72 | } |
||
| 73 | else |
||
| 74 | { |
||
| 75 | $directory = strstr($this->outputFile, basename($this->outputFile), true); |
||
| 76 | |||
| 77 | if (!file_exists($directory)) |
||
| 78 | { |
||
| 79 | $this->error(Errno::FILE_NOT_FOUND, $directory); |
||
| 80 | return false; |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | $data[$id] = [ |
||
| 85 | "message" => $exception->getMessage(), |
||
| 86 | "object" => serialize($exception) |
||
| 87 | ]; |
||
| 88 | |||
| 89 | if (!function_exists('mb_detect_encoding')) |
||
| 90 | throw new \RuntimeException("mbstring library is not installed!"); |
||
| 91 | |||
| 92 | /* |
||
| 93 | * Encodes to UTF8 all messages. It ensures JSON encoding. |
||
| 94 | */ |
||
| 95 | if (!mb_detect_encoding($data[$id]["message"], 'UTF-8', true)) |
||
| 96 | $data[$id]["message"] = utf8_encode($data[$id]["message"]); |
||
| 97 | |||
| 98 | if (!mb_detect_encoding($data[$id]["object"], 'UTF-8', true)) |
||
| 99 | $data[$id]["object"] = utf8_decode($data[$id]["object"]); |
||
| 100 | |||
| 101 | if (($encoded_data = json_encode($data)) === false) |
||
| 102 | { |
||
| 103 | $this->error(Errno::JSON_ENCODE_ERROR, $this->outputFile); |
||
| 104 | return false; |
||
| 105 | } |
||
| 106 | |||
| 107 | $hd = @fopen($this->outputFile, "w+"); |
||
| 108 | |||
| 109 | if (!$hd || !@fwrite($hd, $encoded_data)) |
||
| 110 | { |
||
| 111 | $this->error(Errno::FILE_PERMISSION_DENIED, $this->outputFile); |
||
| 112 | return false; |
||
| 113 | } |
||
| 114 | |||
| 115 | @fclose($hd); |
||
| 116 | |||
| 117 | return $id; |
||
| 118 | } |
||
| 119 | } |