Completed
Push — master ( 5f4795...e3abfa )
by T
05:46
created

CompareCommand   B

Complexity

Total Complexity 6

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Test Coverage

Coverage 0%

Importance

Changes 8
Bugs 0 Features 4
Metric Value
wmc 6
c 8
b 0
f 4
lcom 1
cbo 16
dl 0
loc 104
ccs 0
cts 80
cp 0
rs 8.4614

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 16 1
B execute() 0 85 5
1
<?php
2
3
namespace PHPSemVerCheckerGit\Console\Command;
4
5
use Gitter\Client;
6
use PHPSemVerChecker\Analyzer\Analyzer;
7
use PHPSemVerChecker\Configuration\LevelMapping;
8
use PHPSemVerChecker\Finder\Finder;
9
use PHPSemVerChecker\Reporter\Reporter;
10
use PHPSemVerChecker\Scanner\Scanner;
11
use PHPSemVerCheckerGit\Filter\SourceFilter;
12
use PHPSemVerCheckerGit\Reporter\JsonReporter;
13
use Symfony\Component\Console\Helper\ProgressBar;
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
	protected function configure()
21
	{
22
		$this
23
			->setName('compare')
24
			->setDescription('Compare a set of files to determine what semantic versioning change needs to be done')
25
			->setDefinition([
26
				new InputArgument('before', InputArgument::REQUIRED, 'A branch/tag/commit to check'),
27
				new InputArgument('after', InputArgument::REQUIRED, 'A branch/tag/commit to against'),
28
				new InputOption('include-before', null,  InputOption::VALUE_OPTIONAL, 'List of paths to include <info>(comma separated)</info>'),
29
				new InputOption('include-after', null, InputOption::VALUE_OPTIONAL, 'List of paths to include <info>(comma separated)</info>'),
30
				new InputOption('exclude-before', null,  InputOption::VALUE_REQUIRED, 'List of paths to exclude <info>(comma separated)</info>'),
31
				new InputOption('exclude-after', null, InputOption::VALUE_REQUIRED, 'List of paths to exclude <info>(comma separated)</info>'),
32
				new InputOption('config', null, InputOption::VALUE_REQUIRED, 'A configuration file to configure php-semver-checker-git'),
33
				new InputOption('to-json', null, InputOption::VALUE_REQUIRED, 'Output the result to a JSON file')
34
			]);
35
	}
36
37
	protected function execute(InputInterface $input, OutputInterface $output)
38
	{
39
		$startTime = microtime(true);
40
41
		$targetDirectory = getcwd();
42
		$commitBefore = $this->config->get('before');
43
		$commitAfter = $this->config->get('after');
44
45
		$includeBefore = $this->config->get('include-before');
46
		$excludeBefore = $this->config->get('exclude-before');
47
48
		$includeAfter = $this->config->get('include-after');
49
		$excludeAfter = $this->config->get('exclude-after');
50
51
		$finder = new Finder();
52
		$sourceFilter = new SourceFilter();
53
		$beforeScanner = new Scanner();
54
		$afterScanner = new Scanner();
55
56
		$client = new Client();
57
58
		$repository = $client->getRepository($targetDirectory);
59
60
		$modifiedFiles = $repository->getModifiedFiles($commitBefore, $commitAfter);
61
		$modifiedFiles = array_filter($modifiedFiles, function ($modifiedFile) {
62
			return substr($modifiedFile, -4) === '.php';
63
		});
64
65
		$initialBranch = $repository->getCurrentBranch();
66
67
		$repository->checkout($commitBefore . ' --');
68
69
		$sourceBefore = $finder->findFromString($targetDirectory, $includeBefore, $excludeBefore);
70
		$sourceBeforeMatchedCount = count($sourceBefore);
71
		$sourceBefore = $sourceFilter->filter($sourceBefore, $modifiedFiles);
72
		$progress = new ProgressBar($output, count($sourceBefore));
73
		foreach ($sourceBefore as $file) {
74
			$beforeScanner->scan($file);
75
			$progress->advance();
76
		}
77
78
		$progress->clear();
79
80
		$repository->checkout($commitAfter . ' --');
81
82
		$sourceAfter = $finder->findFromString($targetDirectory, $includeAfter, $excludeAfter);
83
		$sourceAfterMatchedCount = count($sourceAfter);
84
		$sourceAfter = $sourceFilter->filter($sourceAfter, $modifiedFiles);
85
		$progress = new ProgressBar($output, count($sourceAfter));
86
		foreach ($sourceAfter as $file) {
87
			$afterScanner->scan($file);
88
			$progress->advance();
89
		}
90
91
		$progress->clear();
92
93
		if ($initialBranch) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $initialBranch of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
94
			$repository->checkout($initialBranch);
95
		}
96
97
		$progress->clear();
98
99
		$registryBefore = $beforeScanner->getRegistry();
100
		$registryAfter = $afterScanner->getRegistry();
101
102
		$analyzer = new Analyzer();
103
		$report = $analyzer->analyze($registryBefore, $registryAfter);
104
105
		$reporter = new Reporter($report);
106
		$reporter->setFullPath(true);
107
		$reporter->output($output);
108
109
		$toJson = $this->config->get('to-json');
110
		if ($toJson) {
111
			$commitBeforeHash = $repository->getCommit($commitBefore)->getHash();
112
			$commitAfterHash = $repository->getCommit($commitAfter)->getHash();
113
			$jsonReporter = new JsonReporter($report, $toJson, $commitBeforeHash, $commitAfterHash);
114
			$jsonReporter->output();
115
		}
116
117
		$duration = microtime(true) - $startTime;
118
		$output->writeln('');
119
		$output->writeln('[Scanned files] Before: ' . count($sourceBefore) . ' ('.$sourceBeforeMatchedCount.' unfiltered), After: ' . count($sourceAfter) . ' ('.$sourceAfterMatchedCount.' unfiltered)');
120
		$output->writeln('Time: ' . round($duration, 3) . ' seconds, Memory: ' . round(memory_get_peak_usage() / 1024 / 1024, 3) . ' MB');
121
	}
122
}
123