Completed
Push — master ( ff4f9b...44b6f8 )
by Alexander
02:12
created

BackwardsCompatibilityCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
lcom 1
cbo 8
dl 0
loc 151
ccs 0
cts 83
cp 0
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B configure() 0 36 1
A prepareDependencies() 0 10 1
A completeOptionValues() 0 10 2
B execute() 0 24 1
A getBackwardsCompatibilityBreaks() 0 13 2
1
<?php
2
/**
3
 * This file is part of the Code-Insight library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/console-helpers/code-insight
9
 */
10
11
namespace ConsoleHelpers\CodeInsight\Command;
12
13
14
use Aura\Sql\ExtendedPdoInterface;
15
use ConsoleHelpers\CodeInsight\BackwardsCompatibility\Checker\AbstractChecker;
16
use ConsoleHelpers\CodeInsight\BackwardsCompatibility\Checker\CheckerFactory;
17
use ConsoleHelpers\CodeInsight\BackwardsCompatibility\Reporter\ReporterFactory;
18
use ConsoleHelpers\CodeInsight\KnowledgeBase\KnowledgeBaseFactory;
19
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Input\InputOption;
23
use Symfony\Component\Console\Output\OutputInterface;
24
25
class BackwardsCompatibilityCommand extends AbstractCommand
26
{
27
28
	/**
29
	 * Knowledge base factory.
30
	 *
31
	 * @var KnowledgeBaseFactory
32
	 */
33
	private $_knowledgeBaseFactory;
34
35
	/**
36
	 * Backwards compatibility checker factory.
37
	 *
38
	 * @var CheckerFactory
39
	 */
40
	private $_checkerFactory;
41
42
	/**
43
	 * Backwards compatibility reporter factory.
44
	 *
45
	 * @var ReporterFactory
46
	 */
47
	private $_reporterFactory;
48
49
	/**
50
	 * {@inheritdoc}
51
	 */
52
	protected function configure()
53
	{
54
		$this
55
			->setName('bc')
56
			->setDescription('Detects backwards compatibility breaks between 2 project versions')
57
			->addArgument(
58
				'source-project-path',
59
				InputArgument::REQUIRED,
60
				'Path to source project root folder (where <comment>.code-insight.json</comment> is located)'
61
			)
62
			->addArgument(
63
				'target-project-path',
64
				InputArgument::OPTIONAL,
65
				'Path to target project root folder (where <comment>.code-insight.json</comment> is located)',
66
				'.'
67
			)
68
			->addOption(
69
				'source-project-fork',
70
				null,
71
				InputOption::VALUE_REQUIRED,
72
				'Source project fork name'
73
			)
74
			->addOption(
75
				'target-project-fork',
76
				null,
77
				InputOption::VALUE_REQUIRED,
78
				'Target project fork name'
79
			)
80
			->addOption(
81
				'format',
82
				null,
83
				InputOption::VALUE_REQUIRED,
84
				'Output format, e.g. <comment>text</comment>, <comment>html</comment>, <comment>json</comment>',
85
				'text'
86
			);
87
	}
88
89
	/**
90
	 * Prepare dependencies.
91
	 *
92
	 * @return void
93
	 */
94
	protected function prepareDependencies()
95
	{
96
		parent::prepareDependencies();
97
98
		$container = $this->getContainer();
99
100
		$this->_knowledgeBaseFactory = $container['knowledge_base_factory'];
101
		$this->_checkerFactory = $container['bc_checker_factory'];
102
		$this->_reporterFactory = $container['bc_reporter_factory'];
103
	}
104
105
	/**
106
	 * Return possible values for the named option
107
	 *
108
	 * @param string            $optionName Option name.
109
	 * @param CompletionContext $context    Completion context.
110
	 *
111
	 * @return array
112
	 */
113
	public function completeOptionValues($optionName, CompletionContext $context)
114
	{
115
		$ret = parent::completeOptionValues($optionName, $context);
116
117
		if ( $optionName === 'format' ) {
118
			return $this->_reporterFactory->getNames();
119
		}
120
121
		return $ret;
122
	}
123
124
	/**
125
	 * {@inheritdoc}
126
	 */
127
	protected function execute(InputInterface $input, OutputInterface $output)
128
	{
129
		// Get reporter upfront so that we can error out early for invalid reporters.
130
		$reporter = $this->_reporterFactory->get($this->io->getOption('format'));
131
132
		$source_knowledge_base = $this->_knowledgeBaseFactory->getKnowledgeBase(
133
			$this->getPath('source-project-path'),
134
			$this->io->getOption('source-project-fork'),
135
			$this->io
136
		);
137
		$target_knowledge_base = $this->_knowledgeBaseFactory->getKnowledgeBase(
138
			$this->getPath('target-project-path'),
139
			$this->io->getOption('target-project-fork'),
140
			$this->io
141
		);
142
143
		$bc_breaks = $this->getBackwardsCompatibilityBreaks(
144
			$source_knowledge_base->getDatabase(),
145
			$target_knowledge_base->getDatabase(),
146
			$target_knowledge_base->getBackwardsCompatibilityCheckers($this->_checkerFactory)
147
		);
148
149
		$this->io->writeln($reporter->generate($bc_breaks));
150
	}
151
152
	/**
153
	 * Finds backward compatibility breaks.
154
	 *
155
	 * @param ExtendedPdoInterface $source_db Source database.
156
	 * @param ExtendedPdoInterface $target_db Target database.
157
	 * @param AbstractChecker[]    $checkers  Checkers.
158
	 *
159
	 * @return array
160
	 */
161
	protected function getBackwardsCompatibilityBreaks(
162
		ExtendedPdoInterface $source_db,
163
		ExtendedPdoInterface $target_db,
164
		array $checkers
165
	) {
166
		$breaks = array();
167
168
		foreach ( $checkers as $checker ) {
169
			$breaks = array_merge($breaks, $checker->check($source_db, $target_db));
170
		}
171
172
		return $breaks;
173
	}
174
175
}
176