Completed
Pull Request — master (#99)
by
unknown
02:17
created

CompareCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 12

Test Coverage

Coverage 30.76%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 3
lcom 2
cbo 12
dl 0
loc 75
ccs 16
cts 52
cp 0.3076
rs 10
c 6
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 17 1
A execute() 0 48 2
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\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
class CompareCommand extends BaseCommand {
20
	/**
21
	 * @return void
22
	 */
23 1
	protected function configure()
24
	{
25 1
		$this
26 1
			->setName('compare')
27 1
			->setDescription('Compare a set of files to determine what semantic versioning change needs to be done')
28 1
			->setDefinition([
29 1
				new InputArgument('source-before', InputArgument::REQUIRED, 'A base directory to check (ex my-test)'),
30 1
				new InputArgument('source-after', InputArgument::REQUIRED, 'A base directory to check against (ex my-test)'),
31 1
				new InputOption('include-before', null, InputOption::VALUE_REQUIRED, 'List of paths to include <info>(comma separated)</info>'),
32 1
				new InputOption('include-after', null, InputOption::VALUE_REQUIRED, 'List of paths to include <info>(comma separated)</info>'),
33 1
				new InputOption('exclude-before', null, InputOption::VALUE_REQUIRED, 'List of paths to exclude <info>(comma separated)</info>'),
34 1
				new InputOption('exclude-after', null, InputOption::VALUE_REQUIRED, 'List of paths to exclude <info>(comma separated)</info>'),
35 1
				new InputOption('full-path', null, InputOption::VALUE_NONE, 'Display the full path to the file instead of the relative path'),
36 1
				new InputOption('config', null, InputOption::VALUE_REQUIRED, 'A configuration file to configure php-semver-checker'),
37 1
				new InputOption('to-json', null, InputOption::VALUE_REQUIRED, 'Output the result to a JSON file')
38 1
			]);
39 1
	}
40
41
	/**
42
	 * @param \Symfony\Component\Console\Input\InputInterface   $input
43
	 * @param \Symfony\Component\Console\Output\OutputInterface $output
44
	 */
45
	protected function execute(InputInterface $input, OutputInterface $output)
46
	{
47
		$startTime = microtime(true);
48
49
		$finder = new Finder();
50
		$scannerBefore = new Scanner();
51
		$scannerAfter = new Scanner();
52
53
		$sourceBefore = $this->config->get('source-before');
54
		$includeBefore = $this->config->get('include-before');
55
		$excludeBefore = $this->config->get('exclude-before');
56
57
		$sourceAfter = $this->config->get('source-after');
58
		$includeAfter = $this->config->get('include-after');
59
		$excludeAfter = $this->config->get('exclude-after');
60
61
		$sourceBefore = $finder->findFromString($sourceBefore, $includeBefore, $excludeBefore);
62
		$sourceAfter = $finder->findFromString($sourceAfter, $includeAfter, $excludeAfter);
63
64
		$sourceFilter = new SourceFilter();
65
		$identicalCount = $sourceFilter->filter($sourceBefore, $sourceAfter);
66
67
		$progress = new ProgressScanner($output);
68
		$progress->addJob($this->config->get('source-before'), $sourceBefore, $scannerBefore);
69
		$progress->addJob($this->config->get('source-after'), $sourceAfter, $scannerAfter);
70
		$progress->runJobs();
71
72
		$registryBefore = $scannerBefore->getRegistry();
73
		$registryAfter = $scannerAfter->getRegistry();
74
75
		$analyzer = new Analyzer();
76
		$report = $analyzer->analyze($registryBefore, $registryAfter);
77
78
		$reporter = new Reporter($report);
79
		$reporter->setFullPath($this->config->get('full-path'));
80
		$reporter->output($output);
81
82
		$toJson = $this->config->get('to-json');
83
		if ($toJson) {
84
			$jsonReporter = new JsonReporter($report, $toJson);
85
			$jsonReporter->output();
86
		}
87
88
		$duration = microtime(true) - $startTime;
89
		$output->writeln('');
90
		$output->writeln('[Scanned files] Before: ' . count($sourceBefore) . ', After: ' . count($sourceAfter) . ', Identical: ' . $identicalCount);
91
		$output->writeln('Time: ' . round($duration, 3) . ' seconds, Memory: ' . round(memory_get_peak_usage() / 1024 / 1024, 3) . ' MB');
92
	}
93
}
94