Completed
Push — master ( f37ab5...9ec323 )
by T
20:09
created

CompareCommand::initialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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