| Conditions | 4 |
| Paths | 8 |
| Total Lines | 15 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 20 |
| Changes | 0 | ||
| 1 | <?php |
||
| 19 | static public function write($path, $data, $overwrite = true){ |
||
| 20 | $path = str_replace('\\', '/', $path); |
||
| 21 | $fileDir = dirname($path); |
||
| 22 | $tmpFile = tempnam($fileDir, 'safe_writer_'); |
||
| 23 | false !== @file_put_contents($tmpFile, $data) or \PhpBoot\abort("write to file: $tmpFile failed"); |
||
| 24 | if($overwrite){ |
||
| 25 | @unlink($path); //删除原始文件 |
||
|
|
|||
| 26 | } |
||
| 27 | if(!rename($tmpFile, $path)){ |
||
| 28 | @unlink($tmpFile); //删除原始文件 |
||
| 29 | \PhpBoot\abort("write to file: $tmpFile failed"); |
||
| 30 | return false; |
||
| 31 | } |
||
| 32 | return true; |
||
| 33 | } |
||
| 34 | } |
||
| 35 |
If you suppress an error, we recommend checking for the error condition explicitly: