Options   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 16

5 Methods

Rating   Name   Duplication   Size   Complexity  
B shouldWriteFile() 0 33 11
A __construct() 0 4 1
A expandTargetDir() 0 10 2
A get() 0 3 1
A toArray() 0 3 1
1
<?php
2
3
/**
4
 * This file exists for testing our patching stuff to fix https://github.com/symfony/flex/pull/963
5
 */
6
7
/*
8
 * This file is part of the Symfony package.
9
 *
10
 * (c) Fabien Potencier <[email protected]>
11
 *
12
 * For the full copyright and license information, please view the LICENSE
13
 * file that was distributed with this source code.
14
 */
15
16
namespace Symfony\Flex;
17
18
use Composer\IO\IOInterface;
19
use Composer\Util\ProcessExecutor;
20
21
/**
22
 * @author Fabien Potencier <[email protected]>
23
 */
24
class Options
25
{
26
    private $options;
27
    private $writtenFiles = [];
28
    private $io;
29
30
    public function __construct(array $options = [], IOInterface $io = null)
31
    {
32
        $this->options = $options;
33
        $this->io = $io;
34
    }
35
36
    public function get(string $name)
37
    {
38
        return $this->options[$name] ?? null;
39
    }
40
41
    public function expandTargetDir(string $target): string
42
    {
43
        return preg_replace_callback('{%(.+?)%}', function ($matches) {
44
            $option = str_replace('_', '-', strtolower($matches[1]));
45
            if (!isset($this->options[$option])) {
46
                return $matches[0];
47
            }
48
49
            return rtrim($this->options[$option], '/');
50
        }, $target);
51
    }
52
53
    public function shouldWriteFile(string $file, bool $overwrite): bool
54
    {
55
        if (isset($this->writtenFiles[$file])) {
56
            return false;
57
        }
58
        $this->writtenFiles[$file] = true;
59
60
        if (!file_exists($file)) {
61
            return true;
62
        }
63
64
        if (!$overwrite) {
65
            return false;
66
        }
67
68
        if (!filesize($file)) {
69
            return true;
70
        }
71
72
        exec('git status --short --ignored --untracked-files=all -- '.ProcessExecutor::escape($file).' 2>&1', $output, $status);
73
74
        if (0 !== $status) {
75
            return $this->io && $this->io->askConfirmation(sprintf('Cannot determine the state of the "%s" file, overwrite anyway? [y/N] ', $file), false);
76
        }
77
78
        if (empty($output[0]) || preg_match('/^[ AMDRCU][ D][ \t]/', $output[0])) {
79
            return true;
80
        }
81
82
        $name = basename($file);
83
        $name = \strlen($output[0]) - \strlen($name) === strrpos($output[0], $name) ? substr($output[0], 3) : $name;
84
85
        return $this->io && $this->io->askConfirmation(sprintf('File "%s" has uncommitted changes, overwrite? [y/N] ', $name), false);
86
    }
87
88
    public function toArray(): array
89
    {
90
        return $this->options;
91
    }
92
}
93