Completed
Pull Request — 2.x (#216)
by Akihito
08:15
created

FilePutContents   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

1 Method

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