Completed
Push — master ( 7fa794...bc9a8a )
by Alexander
03:31
created

BackwardsCompatibilityCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
ccs 0
cts 29
cp 0
rs 8.8571
cc 1
eloc 23
nc 1
nop 0
crap 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\AbstractChecker;
16
use ConsoleHelpers\CodeInsight\BackwardsCompatibility\CheckerFactory;
17
use ConsoleHelpers\CodeInsight\KnowledgeBase\KnowledgeBaseFactory;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
class BackwardsCompatibilityCommand extends AbstractCommand
24
{
25
26
	/**
27
	 * Knowledge base factory.
28
	 *
29
	 * @var KnowledgeBaseFactory
30
	 */
31
	private $_knowledgeBaseFactory;
32
33
	/**
34
	 * Backwards compatibility checker factory.
35
	 *
36
	 * @var CheckerFactory
37
	 */
38
	private $_checkerFactory;
39
40
	/**
41
	 * {@inheritdoc}
42
	 */
43
	protected function configure()
44
	{
45
		$this
46
			->setName('bc')
47
			->setDescription('Detects backwards compatibility breaks between 2 project versions')
48
			->addArgument(
49
				'source-project-path',
50
				InputArgument::REQUIRED,
51
				'Path to source project root folder (where <comment>.code-insight.json</comment> is located)'
52
			)
53
			->addArgument(
54
				'target-project-path',
55
				InputArgument::OPTIONAL,
56
				'Path to target project root folder (where <comment>.code-insight.json</comment> is located)',
57
				'.'
58
			)
59
			->addOption(
60
				'source-project-fork',
61
				null,
62
				InputOption::VALUE_REQUIRED,
63
				'Source project fork name'
64
			)
65
			->addOption(
66
				'target-project-fork',
67
				null,
68
				InputOption::VALUE_REQUIRED,
69
				'Target project fork name'
70
			);
71
	}
72
73
	/**
74
	 * Prepare dependencies.
75
	 *
76
	 * @return void
77
	 */
78
	protected function prepareDependencies()
79
	{
80
		parent::prepareDependencies();
81
82
		$container = $this->getContainer();
83
84
		$this->_knowledgeBaseFactory = $container['knowledge_base_factory'];
85
		$this->_checkerFactory = $container['bc_checker_factory'];
86
	}
87
88
	/**
89
	 * {@inheritdoc}
90
	 */
91
	protected function execute(InputInterface $input, OutputInterface $output)
92
	{
93
		$source_knowledge_base = $this->_knowledgeBaseFactory->getKnowledgeBase(
94
			$this->getPath('source-project-path'),
95
			$this->io->getOption('source-project-fork'),
96
			$this->io
97
		);
98
		$target_knowledge_base = $this->_knowledgeBaseFactory->getKnowledgeBase(
99
			$this->getPath('target-project-path'),
100
			$this->io->getOption('target-project-fork'),
101
			$this->io
102
		);
103
104
		$bc_breaks = $this->getBackwardsCompatibilityBreaks(
105
			$source_knowledge_base->getDatabase(),
106
			$target_knowledge_base->getDatabase(),
107
			$target_knowledge_base->getBackwardsCompatibilityCheckers($this->_checkerFactory)
108
		);
109
110
		if ( !$bc_breaks ) {
111
			$this->io->writeln('No backwards compatibility breaks detected.');
112
113
			return;
114
		}
115
116
		$this->io->writeln('Backward compatibility breaks:');
117
118
		foreach ( $bc_breaks as $bc_break => $incidents ) {
119
			$this->io->writeln('<fg=red>=== ' . $bc_break . ' (' . count($incidents) . ') ===</>');
120
121
			foreach ( $incidents as $incident ) {
122
				$incident = implode(PHP_EOL . '   ', explode(PHP_EOL, $incident));
123
124
				$this->io->writeln(' * ' . $incident);
125
			}
126
127
			$this->io->writeln('');
128
		}
129
	}
130
131
	/**
132
	 * Finds backward compatibility breaks.
133
	 *
134
	 * @param ExtendedPdoInterface $source_db Source database.
135
	 * @param ExtendedPdoInterface $target_db Target database.
136
	 * @param AbstractChecker[]    $checkers  Checkers.
137
	 *
138
	 * @return array
139
	 */
140
	protected function getBackwardsCompatibilityBreaks(
141
		ExtendedPdoInterface $source_db,
142
		ExtendedPdoInterface $target_db,
143
		array $checkers
144
	) {
145
		$breaks = array();
146
147
		foreach ( $checkers as $checker ) {
148
			$breaks = array_merge($breaks, $checker->check($source_db, $target_db));
149
		}
150
151
		return $breaks;
152
	}
153
154
}
155