Issues (163)

src/Console/Command/InitCommand.php (3 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the humbug/php-scoper package.
7
 *
8
 * Copyright (c) 2017 Théo FIDRY <[email protected]>,
9
 *                    Pádraic Brady <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Humbug\PhpScoper\Console\Command;
16
17
use Fidry\Console\Command\Command;
18
use Fidry\Console\Command\Configuration;
19
use Fidry\Console\Command\Configuration as CommandConfiguration;
20
use Fidry\Console\Input\IO;
21
use Symfony\Component\Console\Helper\FormatterHelper;
22
use Symfony\Component\Console\Input\InputOption;
23
use Symfony\Component\Filesystem\Filesystem;
24
use function file_exists;
25
use function Safe\getcwd;
26
use function Safe\sprintf;
27
use const DIRECTORY_SEPARATOR;
28
29
/**
30
 * @private
31
 * @codeCoverageIgnore
32
 */
33
final class InitCommand implements Command
34
{
35
    private const CONFIG_FILE_OPT = 'config';
36
    private const CONFIG_FILE_TEMPLATE = __DIR__.'/../../scoper.inc.php.tpl';
37
    private const CONFIG_FILE_DEFAULT = 'scoper.inc.php';
38
39
    private Filesystem $fileSystem;
40
    private FormatterHelper $formatterHelper;
41
42
    public function __construct(
43
        Filesystem $fileSystem,
44
        FormatterHelper $formatterHelper
45
    ) {
46
        $this->fileSystem = $fileSystem;
47
        $this->formatterHelper = $formatterHelper;
48
    }
49
50
    public function getConfiguration(): CommandConfiguration
51
    {
52
        return new Configuration(
53
            'init',
54
            'Generates a configuration file.',
55
            '',
56
            [],
57
            [
58
                ChangeableDirectory::createOption(),
59
                new InputOption(
60
                    self::CONFIG_FILE_OPT,
61
                    'c',
62
                    InputOption::VALUE_REQUIRED,
63
                    sprintf(
0 ignored issues
show
Deprecated Code introduced by
The function Safe\sprintf() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

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

63
                    /** @scrutinizer ignore-deprecated */ sprintf(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
64
                        'Configuration file. Will use "%s" if found by default.',
65
                        self::CONFIG_FILE_DEFAULT,
66
                    ),
67
                    null,
68
                ),
69
            ],
70
        );
71
    }
72
73
    public function execute(IO $io): int
74
    {
75
        ChangeableDirectory::changeWorkingDirectory($io);
76
77
        $io->newLine();
78
        $io->writeln(
79
            $this->formatterHelper->formatSection(
80
                'PHP-Scoper configuration generate',
81
                'Welcome!',
82
            ),
83
        );
84
85
        $configFile = $this->retrieveConfig($io);
86
87
        if (null === $configFile) {
88
            $io->writeln('Skipping configuration file generator.');
89
90
            return 0;
91
        }
92
93
        $this->fileSystem->copy(self::CONFIG_FILE_TEMPLATE, $configFile);
94
95
        $io->writeln([
96
            '',
97
            sprintf(
0 ignored issues
show
Deprecated Code introduced by
The function Safe\sprintf() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

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

97
            /** @scrutinizer ignore-deprecated */ sprintf(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
98
                'Generated the configuration file "<comment>%s</comment>".',
99
                $configFile,
100
            ),
101
            '',
102
        ]);
103
104
        return 0;
105
    }
106
107
    private function retrieveConfig(IO $io): ?string
108
    {
109
        $configFile = $io->getOption(self::CONFIG_FILE_OPT)->asNullableNonEmptyString();
110
111
        $configFile = (null === $configFile)
112
            ? $this->makeAbsolutePath(self::CONFIG_FILE_DEFAULT)
113
            : $this->makeAbsolutePath($configFile);
114
115
        if (file_exists($configFile)) {
116
            $canDeleteFile = $io->confirm(
117
                sprintf(
0 ignored issues
show
Deprecated Code introduced by
The function Safe\sprintf() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

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

117
                /** @scrutinizer ignore-deprecated */ sprintf(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
118
                    'The configuration file "<comment>%s</comment>" already exists. Are you sure you want to '
119
                    .'replace it?',
120
                    $configFile,
121
                ),
122
                false,
123
            );
124
125
            if (!$canDeleteFile) {
126
                $io->writeln('Skipped file generation.');
127
128
                return $configFile;
129
            }
130
131
            $this->fileSystem->remove($configFile);
132
        } else {
133
            $createConfig = $io->confirm('No configuration file found. Do you want to create one?');
134
135
            if (!$createConfig) {
136
                return null;
137
            }
138
        }
139
140
        return $configFile;
141
    }
142
143
    private function makeAbsolutePath(string $path): string
144
    {
145
        if (!$this->fileSystem->isAbsolutePath($path)) {
146
            $path = getcwd().DIRECTORY_SEPARATOR.$path;
147
        }
148
149
        return $path;
150
    }
151
}
152