Completed
Push — refactor ( 1ac315 )
by Akihito
05:56
created

FilePutContents::__invoke()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.5222
c 0
b 0
f 0
cc 5
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Compiler;
6
7
use Ray\Compiler\Exception\FileNotWritable;
8
9
use function dirname;
10
use function file_put_contents;
11
use function is_dir;
12
use function is_string;
13
use function mkdir;
14
use function rename;
15
use function tempnam;
16
use function unlink;
17
18
final class FilePutContents
19
{
20
    public function __invoke(string $filename, string $content): void
21
    {
22
        $dir = dirname($filename);
23
        ! is_dir($dir) && mkdir($dir, 0777, true);
24
        $tmpFile = tempnam(dirname($filename), 'swap');
25
        if (is_string($tmpFile) && file_put_contents($tmpFile, $content) && @rename($tmpFile, $filename)) {
26
            return;
27
        }
28
29
        @unlink((string) $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...
30
31
        throw new FileNotWritable($filename);
32
    }
33
}
34