Failed Conditions
Push — master ( e02dc1...26605a )
by Alexander
01:39
created

AbstractChecker::defineIncidentGroups()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 6
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\BackwardsCompatibility;
12
13
14
use Aura\Sql\ExtendedPdoInterface;
15
16
abstract class AbstractChecker
17
{
18
19
	/**
20
	 * Source database.
21
	 *
22
	 * @var ExtendedPdoInterface
23
	 */
24
	protected $sourceDatabase;
25
26
	/**
27
	 * Target database.
28
	 *
29
	 * @var ExtendedPdoInterface
30
	 */
31
	protected $targetDatabase;
32
33
	/**
34
	 * Incidents.
35
	 *
36
	 * @var array
37
	 */
38
	private $_incidents = array();
39
40
	/**
41
	 * Returns backwards compatibility checker name.
42
	 *
43
	 * @return string
44
	 */
45
	abstract public function getName();
46
47
	/**
48
	 * Checks backwards compatibility and returns violations by category.
49
	 *
50
	 * @param ExtendedPdoInterface $source_db Source DB.
51
	 * @param ExtendedPdoInterface $target_db Target DB.
52
	 *
53
	 * @return array
54
	 */
55
	public function check(ExtendedPdoInterface $source_db, ExtendedPdoInterface $target_db)
56
	{
57
		$this->sourceDatabase = $source_db;
58
		$this->targetDatabase = $target_db;
59
60
		$this->doCheck();
61
62
		return array_filter($this->_incidents);
63
	}
64
65
	/**
66
	 * Collects backwards compatibility violations.
67
	 *
68
	 * @return void
69
	 */
70
	abstract protected function doCheck();
71
72
	/**
73
	 * Builds string representation of a parameter.
74
	 *
75
	 * @param array $parameter_data Parameter data.
76
	 *
77
	 * @return string
78
	 */
79
	protected function paramToString(array $parameter_data)
80
	{
81
		if ( $parameter_data['HasType'] ) {
82
			$type = $parameter_data['TypeName'];
83
		}
84
		elseif ( $parameter_data['IsArray'] ) {
85
			$type = 'array';
86
		}
87
		elseif ( $parameter_data['IsCallable'] ) {
88
			$type = 'callable';
89
		}
90
		else {
91
			$type = $parameter_data['TypeClass'];
92
		}
93
94
		$hash_part = strlen($type) ? $type . ' ' : '';
95
96
		if ( $parameter_data['IsPassedByReference'] ) {
97
			$hash_part .= '&$' . $parameter_data['Name'];
98
		}
99
		else {
100
			$hash_part .= '$' . $parameter_data['Name'];
101
		}
102
103
		if ( $parameter_data['HasDefaultValue'] ) {
104
			$hash_part .= ' = ';
105
106
			if ( $parameter_data['DefaultConstant'] ) {
107
				$hash_part .= $parameter_data['DefaultConstant'];
108
			}
109
			else {
110
				$hash_part .= $this->decodeValue($parameter_data['DefaultValue']);
111
			}
112
		}
113
114
		return $hash_part;
115
	}
116
117
	/**
118
	 * Decodes json-encoded PHP value.
119
	 *
120
	 * @param string $json_string JSON string.
121
	 *
122
	 * @return string
123
	 */
124
	protected function decodeValue($json_string)
125
	{
126
		$value = var_export(json_decode($json_string), true);
127
		$value = str_replace(array("\t", "\n"), '', $value);
128
		$value = str_replace('array (', 'array(', $value);
129
130
		return $value;
131
	}
132
133
	/**
134
	 * Defines incident groups.
135
	 *
136
	 * @param array $groups Groups.
137
	 *
138
	 * @return void
139
	 */
140
	protected function defineIncidentGroups(array $groups)
141
	{
142
		$this->_incidents = array();
143
144
		foreach ( $groups as $group ) {
145
			$this->_incidents[$group] = array();
146
		}
147
	}
148
149
	/**
150
	 * Adds incident.
151
	 *
152
	 * @param string      $group     Incident group.
153
	 * @param string      $incident  Incident description.
154
	 * @param string|null $old_value Old value.
155
	 * @param string|null $new_value New value.
156
	 *
157
	 * @return void
158
	 */
159
	protected function addIncident($group, $incident, $old_value = null, $new_value = null)
160
	{
161
		if ( isset($old_value) || isset($new_value) ) {
162
			$incident = '<fg=white;options=bold>' . $incident . '</>' . PHP_EOL;
163
			$incident .= 'OLD: ' . $old_value . PHP_EOL;
164
			$incident .= 'NEW: ' . $new_value . PHP_EOL;
165
		}
166
167
		$this->_incidents[$group][] = $incident;
168
	}
169
170
}
171