Completed
Push — 2.x ( 8ab5a7...2e07e4 )
by Akihito
01:09
created

FilePutContents   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 16
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 13 5
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