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

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

1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\Command;
6
7
use Symfony\Component\Console\Attribute\AsCommand;
8
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...
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
use function file_get_contents;
13
use function file_put_contents;
14
use function intval;
15
use function is_array;
16
use function json_decode;
17
use function preg_replace_callback;
18
19
use const ROOT_PATH;
20
21
#[AsCommand(name: 'fix-po-twig', description: 'Fixes POT file for Twig templates.')]
22
final class FixPoTwigCommand extends Command
23
{
24
    private const POT_FILE = ROOT_PATH . 'po/phpmyadmin.pot';
25
    private const REPLACE_FILE = ROOT_PATH . 'twig-templates/replace.json';
26
27
    protected function configure(): void
28
    {
29
        $this->setHelp(
30
            'The <info>%command.name%</info> command fixes the Twig file name and line number in the'
31
            . ' POT file to match the Twig template and not the compiled Twig file.',
32
        );
33
    }
34
35
    protected function execute(InputInterface $input, OutputInterface $output): int
36
    {
37
        $replaceFile = file_get_contents(self::REPLACE_FILE);
38
        if ($replaceFile === false) {
39
            return Command::FAILURE;
40
        }
41
42
        $replacements = json_decode($replaceFile, true);
43
        if (! is_array($replacements)) {
44
            return Command::FAILURE;
45
        }
46
47
        /* Read pot file */
48
        $pot = file_get_contents(self::POT_FILE);
49
        if ($pot === false) {
50
            return Command::FAILURE;
51
        }
52
53
        /* Do the replacements */
54
        $pot = preg_replace_callback(
55
            '@(twig-templates[0-9a-f/]*.php):([0-9]*)@',
56
            static function (array $matches) use ($replacements): string {
57
                $filename = $matches[1];
58
                $line = intval($matches[2]);
59
                $replace = $replacements[$filename];
60
                foreach ($replace[1] as $cacheLine => $result) {
61
                    if ($line >= $cacheLine) {
62
                        return $replace[0] . ':' . $result;
63
                    }
64
                }
65
66
                return $replace[0] . ':0';
67
            },
68
            $pot,
69
        );
70
        if ($pot === null) {
71
            return Command::FAILURE;
72
        }
73
74
        if (file_put_contents(self::POT_FILE, $pot) === false) {
75
            return Command::FAILURE;
76
        }
77
78
        return Command::SUCCESS;
79
    }
80
}
81