Completed
Pull Request — master (#74)
by Marcel
02:43
created

CompareCommand::execute()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 40
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 19
Bugs 0 Features 5
Metric Value
c 19
b 0
f 5
dl 0
loc 40
ccs 0
cts 30
cp 0
rs 8.8571
cc 2
eloc 28
nc 2
nop 2
crap 6
1
<?php
2
3
namespace PHPSemVerChecker\Console\Command;
4
5
use PHPSemVerChecker\Analyzer\Analyzer;
6
use PHPSemVerChecker\Configuration\Configuration;
7
use PHPSemVerChecker\Configuration\LevelMapping;
8
use PHPSemVerChecker\Filter\SourceFilter;
9
use PHPSemVerChecker\Finder\Finder;
10
use PHPSemVerChecker\Reporter\JsonReporter;
11
use PHPSemVerChecker\Reporter\Reporter;
12
use PHPSemVerChecker\Scanner\ProgressScanner;
13
use PHPSemVerChecker\Scanner\Scanner;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
class CompareCommand extends Command {
21
	/**
22
	 * @var Configuration
23
	 */
24
	protected $config;
25
26 1
	protected function configure()
27
	{
28 1
		$this
29 1
			->setName('compare')
30 1
			->setDescription('Compare a set of files to determine what semantic versioning change needs to be done')
31 1
			->setDefinition([
32 1
				new InputArgument('source-before', InputArgument::REQUIRED, 'A base directory to check (ex my-test)'),
33 1
				new InputArgument('source-after', InputArgument::REQUIRED, 'A base directory to check against (ex my-test)'),
34 1
				new InputOption('include-before', null,  InputOption::VALUE_OPTIONAL, 'List of paths to include <info>(comma separated)</info>'),
35 1
				new InputOption('include-after', null, InputOption::VALUE_OPTIONAL, 'List of paths to include <info>(comma separated)</info>'),
36 1
				new InputOption('exclude-before', null,  InputOption::VALUE_REQUIRED, 'List of paths to exclude <info>(comma separated)</info>'),
37 1
				new InputOption('exclude-after', null, InputOption::VALUE_REQUIRED, 'List of paths to exclude <info>(comma separated)</info>'),
38 1
				new InputOption('full-path', null, InputOption::VALUE_NONE, 'Display the full path to the file instead of the relative path'),
39 1
				new InputOption('config', null, InputOption::VALUE_REQUIRED, 'A configuration file to configure php-semver-checker'),
40 1
				new InputOption('to-json', null, InputOption::VALUE_REQUIRED, 'Output the result to a JSON file')
41 1
			]);
42 1
	}
43
44
	protected function initialize(InputInterface $input, OutputInterface $output)
45
	{
46
		parent::initialize($input, $output);
47
		$configPath = $input->getOption('config');
48
		$this->config = $configPath ? Configuration::fromFile($configPath) : Configuration::defaults();
49
		$this->config->mergeCommandInput($input);
50
		// Set overrides
51
		LevelMapping::setOverrides($this->config->getLevelMapping());
52
	}
53
54
55
	protected function execute(InputInterface $input, OutputInterface $output)
56
	{
57
		$startTime = microtime(true);
58
59
		$finder = new Finder();
60
		$scannerBefore = new Scanner();
61
		$scannerAfter = new Scanner();
62
63
		$sourceBefore = $finder->findFromConfig($this->config, 'before');
64
		$sourceAfter = $finder->findFromConfig($this->config, 'after');
65
66
		$sourceFilter = new SourceFilter();
67
		$identicalCount = $sourceFilter->filter($sourceBefore, $sourceAfter);
68
69
		$progress = new ProgressScanner($output);
70
		$progress->addJob($this->config['source-before'], $sourceBefore, $scannerBefore);
71
		$progress->addJob($this->config['source-after'], $sourceAfter, $scannerAfter);
72
		$progress->runJobs();
73
74
		$registryBefore = $scannerBefore->getRegistry();
75
		$registryAfter = $scannerAfter->getRegistry();
76
77
		$analyzer = new Analyzer();
78
		$report = $analyzer->analyze($registryBefore, $registryAfter);
79
80
		$reporter = new Reporter($report);
81
		$reporter->setFullPath($this->config['full-path']);
82
		$reporter->output($output);
83
84
		$toJson = $this->config['to-json'];
85
		if ($toJson) {
86
			$jsonReporter = new JsonReporter($report, $toJson);
87
			$jsonReporter->output();
88
		}
89
90
		$duration = microtime(true) - $startTime;
91
		$output->writeln('');
92
		$output->writeln('[Scanned files] Before: ' . count($sourceBefore) . ', After: ' . count($sourceAfter) . ', Identical: ' . $identicalCount);
93
		$output->writeln('Time: ' . round($duration, 3) . ' seconds, Memory: ' . round(memory_get_peak_usage() / 1024 / 1024, 3) . ' MB');
94
	}
95
}
96