ContentWriter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 13
Duplicated Lines 0 %

Test Coverage

Coverage 62.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 8
c 1
b 0
f 0
dl 0
loc 13
ccs 5
cts 8
cp 0.625
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B write() 0 11 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Composer\Config;
6
7
use Yiisoft\Composer\Config\Exception\FailedWriteException;
8
9
class ContentWriter
10
{
11 2
    public function write(string $path, string $content): void
12
    {
13 2
        if (file_exists($path) && $content === file_get_contents($path)) {
14
            return;
15
        }
16 2
        $dirname = dirname($path);
17 2
        if (!file_exists($dirname) && !mkdir($dirname, 0777, true) && !is_dir($dirname)) {
18
            throw new FailedWriteException(sprintf('Directory "%s" was not created', $dirname));
19
        }
20 2
        if (false === file_put_contents($path, $content)) {
21
            throw new FailedWriteException("Failed write file $path");
22
        }
23 2
    }
24
}
25