GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SafeFileWriter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 15
cp 0
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A write() 0 15 4
1
<?php
2
namespace PhpBoot\Utils;
3
/**
4
 * 并发安全的写文件
5
 * 其原子性取决于文件系统
6
 * 通过先写临时文件, 然后重命名的方式实现
7
 * @author caoym
8
 *
9
 */
10
class SafeFileWriter
11
{
12
    /**
13
     * 写入文件
14
     * @param string $path 路径
15
     * @param mixed $data 写入的值
16
     * @param boolean $overwrite 是否覆盖已有文件
17
     * @return boolean
18
     */
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); //删除原始文件
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
26
        }
27
        if(!rename($tmpFile, $path)){
28
            @unlink($tmpFile); //删除原始文件
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
29
            \PhpBoot\abort("write to file: $tmpFile failed");
30
            return false;
31
        }
32
        return true;
33
    }
34
}
35