Completed
Push — master ( 54b9b6...b49317 )
by BruceScrutinizer
02:08
created

ConfigTemplateCreator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
rs 10
c 1
b 0
f 1
1
<?php
2
declare(strict_types=1);
3
4
namespace NamespaceProtector\Config;
5
6
use NamespaceProtector\Common\PathInterface;
7
8
final class ConfigTemplateCreator
9
{
10
    private const FILENAME = 'namespace-protector-config.json';
11
    private const FILENAME_VISIBILITY =  'namespace-protector-visibility.json';
12
    private const TEMPLATE_CONFIG_JSON = 'template-config-json';
13
14
    public static function createJsonTemplateConfig(PathInterface $baseComposerJsonDirectory): void
15
    {
16
        self::createFileWithBackup($baseComposerJsonDirectory().self::FILENAME, self::TEMPLATE_CONFIG_JSON);
17
    }
18
19
    public static function createJsonTemplateVisibility(): void
20
    {
21
        self::createFileWithBackup(self::FILENAME_VISIBILITY, 'template-visibility');
22
    }
23
24
    private static function createFileWithBackup(string $fileName, string $templateFile): void
25
    {
26
        @rename($fileName, $fileName . '_backup.json');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for rename(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

26
        /** @scrutinizer ignore-unhandled */ @rename($fileName, $fileName . '_backup.json');

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...
27
28
        $content = \safe\file_get_contents(__DIR__ . '/' . $templateFile);
29
        \safe\file_put_contents($fileName, $content);
30
    }
31
}
32