Passed
Push — master ( 210521...1582b6 )
by Maurício
14:48 queued 06:32
created

libraries/classes/Command/SetVersionCommand.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\Command;
6
7
use RangeException;
8
use Symfony\Component\Console\Attribute\AsCommand;
9
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
use function file_put_contents;
15
use function preg_match;
16
use function sprintf;
17
18
#[AsCommand(name: 'set-version', description: 'Sets the version number.')]
19
final class SetVersionCommand extends Command
20
{
21
    private static string $generatedClassTemplate = <<<'PHP'
22
<?php
23
24
declare(strict_types=1);
25
26
namespace PhpMyAdmin;
27
28
use const VERSION_SUFFIX;
29
30
/**
31
 * This class is generated by scripts/console.
32
 *
33
 * @see \PhpMyAdmin\Command\SetVersionCommand
34
 */
35
final class Version
36
{
37
    // The VERSION_SUFFIX constant is defined at libraries/constants.php
38
    public const VERSION = '%1$u.%2$u.%3$u%4$s' . VERSION_SUFFIX;
39
    public const SERIES = '%1$u.%2$u';
40
    public const MAJOR = %1$u;
41
    public const MINOR = %2$u;
42
    public const PATCH = %3$u;
43
    public const ID = %1$u%2$02u%3$02u;
44
    public const PRE_RELEASE_NAME = '%5$s';
45
    public const IS_DEV = %6$s;
46
}
47
48
PHP;
49
50 96
    protected function configure(): void
51
    {
52 96
        $this->setHelp('This command generates the PhpMyAdmin\Version class based on the version number provided.');
53 96
        $this->addArgument('version', InputArgument::REQUIRED, 'The version number');
54
    }
55
56
    protected function execute(InputInterface $input, OutputInterface $output): int
57
    {
58
        /** @var string $version */
59
        $version = $input->getArgument('version');
60
61
        $generatedClass = $this->getGeneratedClass($version);
62
63
        if (! $this->writeGeneratedClassFile($generatedClass)) {
64
            return Command::FAILURE;
65
        }
66
67
        $output->writeln('PhpMyAdmin\Version class successfully generated!');
68
69
        return Command::SUCCESS;
70
    }
71
72 96
    private function getGeneratedClass(string $version): string
73
    {
74
        // Do not allow any major below 5
75 96
        $return = preg_match('/^([5-9]+)\.(\d{1,2})\.(\d{1,2})(-([a-z0-9]+))?$/', $version, $matches);
76 96
        if ($return === false || $return === 0) {
77 64
            throw new RangeException('The version number is in the wrong format: ' . $version);
78
        }
79
80 32
        return sprintf(
81 32
            self::$generatedClassTemplate,
82 32
            $matches[1],
83 32
            $matches[2],
84 32
            $matches[3],
85 32
            $matches[4] ?? '',
86 32
            $matches[5] ?? '',
87 32
            ($matches[5] ?? '') === 'dev' ? 'true' : 'false',
88 32
        );
89
    }
90
91
    private function writeGeneratedClassFile(string $generatedClass): bool
92
    {
93
        $result = file_put_contents(ROOT_PATH . 'libraries/classes/Version.php', $generatedClass);
94
95
        return $result !== false;
96
    }
97
}
98