Passed
Push — master ( 9645f3...34dd33 )
by Konstantinos
01:59
created

Command::verbForMessage()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 1
nc 8
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpNsFixer\Console;
4
5
use PhpNsFixer\Event\FileProcessedEvent;
6
use PhpNsFixer\Fixer\Result;
7
use Symfony\Component\Console\Command\Command as SymfonyCommand;
8
use Symfony\Component\Console\Helper\ProgressBar;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\EventDispatcher\EventDispatcher;
11
use Tightenco\Collect\Support\Collection;
12
13
abstract class Command extends SymfonyCommand
14
{
15
    /**
16
     * @var EventDispatcher
17
     */
18
    protected $dispatcher;
19
20
    /**
21
     * @var ProgressBar
22
     */
23
    protected $progressBar;
24
25 5
    public function __construct($name = null)
26
    {
27 5
        parent::__construct($name);
28
29 5
        $this->dispatcher = new EventDispatcher();
30 5
        $this->dispatcher->addListener(FileProcessedEvent::class, function (FileProcessedEvent $event) {
31 5
            $this->progressBar->setMessage($event->getFile()->getRelativePathname(), 'filename');
32 5
            $this->progressBar->advance();
33 5
        });
34 5
    }
35
36
    /**
37
     * @param OutputInterface $output
38
     * @param Collection $files
39
     * @return void
40
     */
41 5
    protected function progressStart(OutputInterface $output, Collection $files): void
42
    {
43 5
        ProgressBar::setFormatDefinition('custom', 'Checking files... %current%/%max% (%filename%)');
44
45 5
        $this->progressBar = new ProgressBar($output, $files->count());
46 5
        $this->progressBar->setFormat('custom');
47 5
        $this->progressBar->start();
48 5
    }
49
50
    /**
51
     * @param OutputInterface $output
52
     */
53 5
    protected function progressFinish(OutputInterface $output): void
54
    {
55 5
        $this->progressBar->setMessage('Done', 'filename');
56 5
        $this->progressBar->finish();
57
58 5
        $output->writeln("\n");
59 5
    }
60
61
    /**
62
     * @param OutputInterface $output
63
     * @param Collection $problematicFiles
64
     * @param bool $dryRun
65
     */
66 5
    protected function printResults(OutputInterface $output, Collection $problematicFiles, bool $dryRun = true): void
67
    {
68 5
        if ($problematicFiles->count() === 0) {
69 2
            $output->writeln("<info>No problems found! :)</info>");
70 2
            return;
71
        }
72
73 3
        $output->writeln(
74 3
            sprintf(
75 3
                "<options=bold,underscore>There %s %d wrong %s:</>\n",
76 3
                $this->verbForMessage($problematicFiles, $dryRun),
77 3
                $problematicFiles->count(),
78 3
                $problematicFiles->count() !== 1 ? 'namespaces' : 'namespace'
79
            )
80
        );
81
82 3
        $problematicFiles->each(function (Result $result, $key) use ($output) {
83 3
            $output->writeln(sprintf("%d) %s:", $key + 1, $result->getFile()->getRelativePathname()));
84 3
            $output->writeln(sprintf("\t<fg=red>- %s</>", $result->getExpected()));
85 3
            $output->writeln(sprintf("\t<fg=green>+ %s</>", $result->getActual()));
86 3
        });
87 3
    }
88
89
    /**
90
     * @param Collection $problematicFiles
91
     * @param bool $isDryRun
92
     * @return string
93
     */
94 3
    protected function verbForMessage(Collection $problematicFiles, bool $isDryRun = false): string
95
    {
96 3
        return $problematicFiles->count() !== 1 ? ($isDryRun ? 'are' : 'were') : ($isDryRun ? 'is' : 'was');
97
    }
98
}
99