Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
RefactorCommand
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 configure
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5/**
6 * This file is part of fast-forward/dev-tools.
7 *
8 * This source file is subject to the license bundled
9 * with this source code in the file LICENSE.
10 *
11 * @copyright Copyright (c) 2026 Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
12 * @license   https://opensource.org/licenses/MIT MIT License
13 *
14 * @see       https://github.com/php-fast-forward/dev-tools
15 * @see       https://github.com/php-fast-forward
16 * @see       https://datatracker.ietf.org/doc/html/rfc2119
17 */
18
19namespace FastForward\DevTools\Command;
20
21use Symfony\Component\Console\Input\InputInterface;
22use Symfony\Component\Console\Input\InputOption;
23use Symfony\Component\Console\Output\OutputInterface;
24use Symfony\Component\Process\Process;
25
26/**
27 * Provides functionality to execute automated code refactoring using Rector.
28 * This class MUST NOT be extended and SHALL encapsulate the logic for Rector invocation.
29 */
30final class RefactorCommand extends AbstractCommand
31{
32    /**
33     * @var string the default Rector configuration file
34     */
35    public const string CONFIG = 'rector.php';
36
37    /**
38     * Configures the refactor command options and description.
39     *
40     * This method MUST define the expected `--fix` option. It SHALL configure the command name
41     * and descriptions accurately.
42     *
43     * @return void
44     */
45    protected function configure(): void
46    {
47        $this
48            ->setName('refactor')
49            ->setDescription('Runs Rector for code refactoring.')
50            ->setHelp('This command runs Rector to refactor your code.')
51            ->addOption(
52                name: 'fix',
53                shortcut: 'f',
54                mode: InputOption::VALUE_NONE,
55                description: 'Automatically fix code refactoring issues.'
56            );
57    }
58
59    /**
60     * Executes the refactoring process securely.
61     *
62     * The method MUST execute Rector securely via `Process`. It SHALL use dry-run mode
63     * unless the `--fix` option is specified. It MUST return `self::SUCCESS` or `self::FAILURE`.
64     *
65     * @param InputInterface $input the input interface to retrieve arguments properly
66     * @param OutputInterface $output the output interface to log outputs
67     *
68     * @return int the status code denoting success or failure
69     */
70    protected function execute(InputInterface $input, OutputInterface $output): int
71    {
72        $output->writeln('<info>Running Rector for code refactoring...</info>');
73
74        $command = new Process([
75            $this->getAbsolutePath('vendor/bin/rector'),
76            'process',
77            '--config',
78            parent::getConfigFile(self::CONFIG),
79            $input->getOption('fix') ? null : '--dry-run',
80        ]);
81
82        return parent::runProcess($command, $output);
83    }
84}