Filesystem   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 21
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A patch() 0 19 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the dotfiles project.
7
 *
8
 *     (c) Anthonius Munthi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Dotfiles\Core\Util;
15
16
use Symfony\Component\Filesystem\Filesystem as BaseFileSystem;
17
18
class Filesystem extends BaseFileSystem
19
{
20
    public function patch($file, $patch): void
21
    {
22
        if (!is_dir($dir = dirname($file))) {
23
            mkdir($dir, 0755, true);
24
        }
25
        if (!is_file($file)) {
26
            touch($file);
27
        }
28
        $prefix = '### > dotfiles-patch ###';
29
        $suffix = '### < dotfiles-patch ###';
30
        $patch = "\n${prefix}\n${patch}\n${suffix}\n";
31
        $regex = '/\\n'.$prefix.'.*'.$suffix.'\\n/is';
32
33
        $contents = file_get_contents($file);
34
        if (preg_match($regex, $contents, $matches)) {
35
            $contents = str_replace($matches[0], $patch, $contents);
36
            $this->dumpFile($file, $contents);
37
        } else {
38
            $this->appendToFile($file, $patch);
39
        }
40
    }
41
}
42