• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

keradus / PHP-CS-Fixer / 24023665044

02 Apr 2026 09:33PM UTC coverage: 93.056% (+0.1%) from 92.938%
24023665044

push

github

web-flow
chore: add tests for `BracesPositionFixer` (#9522)

29548 of 31753 relevant lines covered (93.06%)

43.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Console/Internal/Command/ParseCommand.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of PHP CS Fixer.
7
 *
8
 * (c) Fabien Potencier <fabien@symfony.com>
9
 *     Dariusz RumiƄski <dariusz.ruminski@gmail.com>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14

15
namespace PhpCsFixer\Console\Internal\Command;
16

17
use PhpCsFixer\Tokenizer\Tokens;
18
use Symfony\Component\Console\Attribute\AsCommand;
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Input\InputOption;
23
use Symfony\Component\Console\Output\ConsoleOutputInterface;
24
use Symfony\Component\Console\Output\OutputInterface;
25

26
/**
27
 * @internal
28
 *
29
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
30
 */
31
#[AsCommand(name: 'parse', description: 'Parse file into tokens.')]
32
final class ParseCommand extends Command
33
{
34
    public const MODE_NATIVE = 'native';
35
    public const MODE_FIXER = 'fixer';
36

37
    public const FORMAT_DUMP = 'dump';
38
    public const FORMAT_JSON = 'json';
39

40
    public function __construct()
41
    {
42
        parent::__construct('parse');
×
43
        $this->setDescription('Parse file into tokens.');
×
44
    }
45

46
    protected function configure(): void
47
    {
48
        $this
×
49
            ->setDefinition(
×
50
                [
×
51
                    new InputArgument('path', InputArgument::REQUIRED),
×
52
                    new InputOption('mode', null, InputOption::VALUE_REQUIRED, 'Parsing mode: `fixer` or `native`.', self::MODE_FIXER, [self::MODE_NATIVE, self::MODE_FIXER]),
×
53
                    new InputOption('format', null, InputOption::VALUE_REQUIRED, 'Output format: `json` or `dump`.', self::FORMAT_JSON, [self::FORMAT_DUMP, self::FORMAT_JSON]),
×
54
                ],
×
55
            )
×
56
        ;
×
57
    }
58

59
    protected function execute(InputInterface $input, OutputInterface $output): int
60
    {
61
        $stdErr = $output instanceof ConsoleOutputInterface
×
62
            ? $output->getErrorOutput()
×
63
            : $output;
×
64

65
        $path = $input->getArgument('path');
×
66
        $mode = $input->getOption('mode');
×
67
        $format = $input->getOption('format');
×
68

69
        if (!\in_array($mode, [self::MODE_FIXER, self::MODE_NATIVE], true)) {
×
70
            $stdErr->writeln('<error>Invalid "mode" option.</error>');
×
71

72
            return 1;
×
73
        }
74
        if (!\in_array($format, [self::FORMAT_DUMP, self::FORMAT_JSON], true)) {
×
75
            $stdErr->writeln('<error>Invalid "format" option.</error>');
×
76

77
            return 1;
×
78
        }
79

80
        $code = @file_get_contents($path);
×
81

82
        if (false === $code) {
×
83
            $stdErr->writeln('<error>Cannot read file.</error>');
×
84

85
            return 1;
×
86
        }
87

88
        if (self::MODE_FIXER === $mode) {
×
89
            $tokens = Tokens::fromCode($code);
×
90
            $tokensJson = $tokens->toJson();
×
91
        } else {
92
            $tokens = \defined('TOKEN_PARSE')
×
93
                ? token_get_all($code, \TOKEN_PARSE)
×
94
                : token_get_all($code);
×
95

96
            $tokensJson = json_encode(\SplFixedArray::fromArray($tokens), \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT | \JSON_NUMERIC_CHECK);
×
97
        }
98

99
        if (self::FORMAT_DUMP === $format) {
×
100
            $output->writeln(var_export($tokens, true));
×
101
        } else {
102
            $output->writeln($tokensJson);
×
103
        }
104

105
        return 0;
×
106
    }
107
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc