Completed
Pull Request — master (#15)
by Marco
02:37
created

ApiCompare::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
nc 1
nop 0
dl 0
loc 12
c 0
b 0
f 0
cc 1
rs 9.4285
1
<?php
2
declare(strict_types=1);
3
4
namespace Roave\ApiCompare\Command;
5
6
use Assert\Assert;
7
use Roave\ApiCompare\Comparator;
8
use Roave\ApiCompare\Factory\DirectoryReflectorFactory;
9
use Roave\ApiCompare\Formatter\SymfonyConsoleTextFormatter;
10
use Roave\ApiCompare\Git\CheckedOutRepository;
11
use Roave\ApiCompare\Git\PerformCheckoutOfRevision;
12
use Roave\ApiCompare\Git\Revision;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
final class ApiCompare extends Command
19
{
20
    /** @var PerformCheckoutOfRevision */
21
    private $git;
22
23
    /**
24
     * @var DirectoryReflectorFactory
25
     */
26
    private $reflectorFactory;
27
28
    /**
29
     * @param PerformCheckoutOfRevision $git
30
     * @param DirectoryReflectorFactory $reflectorFactory
31
     * @throws \Symfony\Component\Console\Exception\LogicException
32
     */
33
    public function __construct(
34
        PerformCheckoutOfRevision $git,
35
        DirectoryReflectorFactory $reflectorFactory
36
    ) {
37
        parent::__construct();
38
        $this->git = $git;
39
        $this->reflectorFactory = $reflectorFactory;
40
    }
41
42
    /**
43
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
44
     */
45
    protected function configure() : void
46
    {
47
        $this
48
            ->setName('api-compare:compare')
49
            ->setDescription('List comparisons between class APIs')
50
            ->addArgument('from', InputArgument::REQUIRED)
51
            ->addArgument('to', InputArgument::REQUIRED)
52
            ->addArgument(
53
                'sources-path',
54
                InputArgument::OPTIONAL,
55
                'Path to the sources, relative to the repository root',
56
                'src'
57
            )
58
        ;
59
    }
60
61
    /**
62
     * @param InputInterface $input
63
     * @param OutputInterface $output
64
     * @throws \Symfony\Component\Process\Exception\RuntimeException
65
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
66
     * @throws \Roave\BetterReflection\SourceLocator\Exception\InvalidFileInfo
67
     * @throws \Roave\BetterReflection\SourceLocator\Exception\InvalidDirectory
68
     */
69
    public function execute(InputInterface $input, OutputInterface $output) : void
70
    {
71
        // @todo fix flaky assumption about the path of the source repo...
72
        $sourceRepo = CheckedOutRepository::fromPath(getcwd());
73
74
        $fromPath    = $this->git->checkout($sourceRepo, Revision::fromSha1($input->getArgument('from')));
75
        $toPath      = $this->git->checkout($sourceRepo, Revision::fromSha1($input->getArgument('to')));
76
        $sourcesPath = $input->getArgument('sources-path');
77
78
        // @todo fix hard-coded /src/ addition...
79
        try {
80
            $fromSources = $fromPath . '/' . $sourcesPath;
81
            $toSources   = $toPath . '/' . $sourcesPath;
82
83
            Assert::that($fromSources)->directory();
84
            Assert::that($toSources)->directory();
85
86
            (new SymfonyConsoleTextFormatter($output))->write(
87
                (new Comparator())->compare(
88
                    $this->reflectorFactory->__invoke((string)$fromPath . '/' . $sourcesPath),
89
                    $this->reflectorFactory->__invoke((string)$toPath . '/' . $sourcesPath)
90
                )
91
            );
92
        } finally {
93
            $this->git->remove($fromPath);
94
            $this->git->remove($toPath);
95
        }
96
    }
97
}
98