Passed
Push — master ( 6267cb...128545 )
by Konstantinos
01:38
created

FixCommand::execute()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 23
nc 2
nop 2
dl 0
loc 37
ccs 25
cts 25
cp 1
crap 3
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
namespace PhpNsFixer\Console;
4
5
use PhpNsFixer\Event\FileProcessedEvent;
6
use PhpNsFixer\Finder\FileFinder;
7
use PhpNsFixer\Fixer\Result;
8
use PhpNsFixer\Runner\Runner;
9
use PhpNsFixer\Runner\RunnerOptions;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Helper\ProgressBar;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Symfony\Component\EventDispatcher\EventDispatcher;
17
use Tightenco\Collect\Support\Collection;
18
19
class FixCommand extends Command
20
{
21
    /**
22
     * @var ProgressBar
23
     */
24
    private $progressBar;
25
26
    /**
27
     * @var EventDispatcher
28
     */
29
    private $dispatcher;
30
31 3
    public function __construct($name = null)
32
    {
33 3
        parent::__construct($name);
34
35 3
        $this->dispatcher = new EventDispatcher();
36 3
        $this->dispatcher->addListener(FileProcessedEvent::class, function (FileProcessedEvent $event) {
37 3
            $this->progressBar->setMessage($event->getFile()->getRelativePathname(), 'filename');
38 3
            $this->progressBar->advance();
39 3
        });
40 3
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 3
    protected function configure()
46
    {
47 3
        $this->setName('fix');
48 3
        $this->setDefinition([
49 3
            new InputArgument('path', InputArgument::REQUIRED, 'The path.'),
50 3
            new InputOption('prefix', 'P', InputOption::VALUE_REQUIRED, 'Namespace prefix.'),
51 3
            new InputOption('skip-empty', 'E', InputOption::VALUE_NONE, 'Skip files without namespace.'),
52 3
            new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Only show which files would have been modified.')
53
        ]);
54 3
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 3
    protected function execute(InputInterface $input, OutputInterface $output)
60
    {
61 3
        $files = FileFinder::list($input->getArgument('path'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('path') can also be of type null and string[]; however, parameter $path of PhpNsFixer\Finder\FileFinder::list() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

61
        $files = FileFinder::list(/** @scrutinizer ignore-type */ $input->getArgument('path'));
Loading history...
62
63 3
        $this->progressStart($output, $files);
64
65 3
        $runnerOptions = new RunnerOptions(
66 3
            $files,
67 3
            $input->getOption('prefix') ?? '',
68 3
            $input->getOption('skip-empty') ?? false,
69 3
            $input->getOption('dry-run') ?? false
70
        );
71 3
        $problematicFiles = (new Runner($runnerOptions, $this->dispatcher))->run();
72
73 3
        $this->progressFinish($output);
74
75 3
        if ($problematicFiles->count() === 0) {
76 1
            $output->writeln("<info>No problems found! :)</info>");
77 1
            return 0;
78
        }
79
80 2
        $output->writeln(
81 2
            sprintf(
82 2
                "<options=bold,underscore>There %s %d wrong %s:</>\n",
83 2
                $this->verbForMessage($problematicFiles, $input->getOption('dry-run')),
84 2
                $problematicFiles->count(),
85 2
                $problematicFiles->count() !== 1 ? 'namespaces' : 'namespace'
86
            )
87
        );
88
89 2
        $problematicFiles->each(function (Result $result, $key) use ($output) {
90 2
            $output->writeln(sprintf("%d) %s:", $key + 1, $result->getFile()->getRelativePathname()));
91 2
            $output->writeln(sprintf("\t<fg=red>- %s</>", $result->getExpected()));
92 2
            $output->writeln(sprintf("\t<fg=green>+ %s</>", $result->getActual()));
93 2
        });
94
95 2
        return $problematicFiles->count();
96
    }
97
98
    /**
99
     * @param OutputInterface $output
100
     * @param Collection $files
101
     * @return void
102
     */
103 3
    private function progressStart(OutputInterface $output, Collection $files): void
104
    {
105 3
        $this->progressBar = new ProgressBar($output, $files->count());
106
107 3
        $this->progressBar->setFormatDefinition('custom', 'Checking files... %current%/%max% (%filename%)');
108 3
        $this->progressBar->setFormat('custom');
109
110 3
        $this->progressBar->start();
111 3
    }
112
113
    /**
114
     * @param OutputInterface $output
115
     */
116 3
    private function progressFinish(OutputInterface $output): void
117
    {
118 3
        $this->progressBar->setMessage('Done', 'filename');
119 3
        $this->progressBar->finish();
120
121 3
        $output->writeln("\n");
122 3
    }
123
124
    /**
125
     * @param Collection $problematicFiles
126
     * @param bool $isDryRun
127
     * @return string
128
     */
129 2
    private function verbForMessage(Collection $problematicFiles, bool $isDryRun = false): string
130
    {
131 2
        return $problematicFiles->count() !== 1 ? ($isDryRun ? 'are' : 'were') : ($isDryRun ? 'is' : 'was');
132
    }
133
}
134