Passed
Push — master ( 55c0e1...a65543 )
by Jonathan
01:52
created

outputClassMethodUsages()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 2
dl 0
loc 15
ccs 9
cts 10
cp 0.9
crap 3.009
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace UsageFinder\Command;
6
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Stopwatch\Stopwatch;
13
use UsageFinder\ClassMethodReference;
14
use UsageFinder\ClassMethodUsage;
15
use UsageFinder\FindClassMethodUsages;
16
use function assert;
17
use function count;
18
use function is_string;
19
use function sprintf;
20
21
final class FindClassMethodUsagesCommand extends Command
22
{
23 1
    protected function configure() : void
24
    {
25
        $this
26 1
            ->setName('find')
27 1
            ->setDescription('Find class method usages in the given path.')
28 1
            ->addArgument('path', InputArgument::REQUIRED, 'The path to search in.')
29 1
            ->addArgument('find', InputArgument::REQUIRED, 'What to search for.')
30 1
            ->addOption('threads', 't', InputOption::VALUE_REQUIRED, 'How many threads to run psalm with.', 1);
31 1
    }
32
33 1
    public function execute(InputInterface $input, OutputInterface $output) : void
34
    {
35 1
        $path = $input->getArgument('path');
36 1
        assert(is_string($path));
37
38 1
        $find = $input->getArgument('find');
39 1
        assert(is_string($find));
40
41 1
        $threads = (int) $input->getOption('threads');
42
43 1
        $classMethodReference = new ClassMethodReference($find);
44
45 1
        $output->writeln(sprintf(
46 1
            'Searching for <info>%s</info> in <info>%s</info>.',
47 1
            $classMethodReference->getName(),
48 1
            $path
49
        ));
50 1
        $output->writeln('');
51
52 1
        $stopwatch = new Stopwatch(true);
53 1
        $stopwatch->start('usage-finder');
54
55 1
        $classMethodUsages = (new FindClassMethodUsages())->__invoke(
56 1
            $path,
57 1
            $classMethodReference,
58 1
            $threads
59
        );
60
61 1
        $this->outputClassMethodUsages($classMethodUsages, $output);
62
63 1
        $event = $stopwatch->stop('usage-finder');
64
65 1
        $output->writeln([
66 1
            sprintf('Finished in <info>%sms</info>', $event->getDuration()),
67 1
            '',
68
        ]);
69 1
    }
70
71
    /**
72
     * @param array<int, ClassMethodUsage> $classMethodUsages
73
     */
74 1
    private function outputClassMethodUsages(array $classMethodUsages, OutputInterface $output) : void
75
    {
76 1
        if (count($classMethodUsages) > 0) {
77 1
            foreach ($classMethodUsages as $classMethodUsage) {
78 1
                $output->writeln(sprintf(
79 1
                    '  Found usage in <info>%s</info> on line <info>%d</info>.',
80 1
                    $classMethodUsage->getFile(),
81 1
                    $classMethodUsage->getLine()
82
                ));
83
84 1
                $output->writeln($classMethodUsage->getConsoleSnippet());
85 1
                $output->writeln('');
86
            }
87
        } else {
88
            $output->writeln('Could not find any usages.');
89
        }
90 1
    }
91
}
92