Completed
Pull Request — 2.x (#216)
by Akihito
09:22
created

FilePutContents::__invoke()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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