Failed Conditions
Push — master ( a2f8a2...e02dc1 )
by Alexander
01:53
created

BackwardsCompatibilityCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 18
ccs 0
cts 18
cp 0
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 14
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\Output\OutputInterface;
21
22
class BackwardsCompatibilityCommand extends AbstractCommand
23
{
24
25
	/**
26
	 * Knowledge base factory.
27
	 *
28
	 * @var KnowledgeBaseFactory
29
	 */
30
	private $_knowledgeBaseFactory;
31
32
	/**
33
	 * Backwards compatibility checker factory.
34
	 *
35
	 * @var CheckerFactory
36
	 */
37
	private $_checkerFactory;
38
39
	/**
40
	 * {@inheritdoc}
41
	 */
42
	protected function configure()
43
	{
44
		$this
45
			->setName('backwards-compatibility')
46
			->setAliases(array('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
	}
60
61
	/**
62
	 * Prepare dependencies.
63
	 *
64
	 * @return void
65
	 */
66
	protected function prepareDependencies()
67
	{
68
		parent::prepareDependencies();
69
70
		$container = $this->getContainer();
71
72
		$this->_knowledgeBaseFactory = $container['knowledge_base_factory'];
73
		$this->_checkerFactory = $container['bc_checker_factory'];
74
	}
75
76
	/**
77
	 * {@inheritdoc}
78
	 */
79
	protected function execute(InputInterface $input, OutputInterface $output)
80
	{
81
		$source_knowledge_base = $this->_knowledgeBaseFactory->getKnowledgeBase(
82
			$this->getPath('source-project-path'),
83
			$this->io
84
		);
85
		$target_knowledge_base = $this->_knowledgeBaseFactory->getKnowledgeBase(
86
			$this->getPath('target-project-path'),
87
			$this->io
88
		);
89
90
		$bc_breaks = $this->getBackwardsCompatibilityBreaks(
91
			$source_knowledge_base->getDatabase(),
92
			$target_knowledge_base->getDatabase(),
93
			$target_knowledge_base->getBackwardsCompatibilityCheckers($this->_checkerFactory)
94
		);
95
96
		if ( !$bc_breaks ) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $bc_breaks of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
97
			$this->io->writeln('No backwards compatibility breaks detected.');
98
99
			return;
100
		}
101
102
		$this->io->writeln('Backward compatibility breaks:');
103
104
		foreach ( $bc_breaks as $bc_break => $incidents ) {
105
			$this->io->writeln('<fg=red>=== ' . $bc_break . ' (' . count($incidents) . ') ===</>');
106
107
			foreach ( $incidents as $incident ) {
108
				$incident = implode(PHP_EOL . '   ', explode(PHP_EOL, $incident));
109
110
				$this->io->writeln(' * ' . $incident);
111
			}
112
113
			$this->io->writeln('');
114
		}
115
	}
116
117
	/**
118
	 * Finds backward compatibility breaks.
119
	 *
120
	 * @param ExtendedPdoInterface $source_db Source database.
121
	 * @param ExtendedPdoInterface $target_db Target database.
122
	 * @param AbstractChecker[]    $checkers  Checkers.
123
	 *
124
	 * @return array
125
	 */
126
	protected function getBackwardsCompatibilityBreaks(
127
		ExtendedPdoInterface $source_db,
128
		ExtendedPdoInterface $target_db,
129
		array $checkers
130
	) {
131
		$breaks = array();
132
133
		foreach ( $checkers as $checker ) {
134
			$breaks = array_merge($breaks, $checker->check($source_db, $target_db));
135
		}
136
137
		return $breaks;
138
	}
139
140
}
141