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

AbstractChecker::addIncident()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
nc 2
cc 3
eloc 6
nop 4
crap 12
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
	protected $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
	abstract public function check(ExtendedPdoInterface $source_db, ExtendedPdoInterface $target_db);
56
57
	/**
58
	 * Builds string representation of a parameter.
59
	 *
60
	 * @param array $parameter_data Parameter data.
61
	 *
62
	 * @return string
63
	 */
64
	protected function paramToString(array $parameter_data)
65
	{
66
		if ( $parameter_data['HasType'] ) {
67
			$type = $parameter_data['TypeName'];
68
		}
69
		elseif ( $parameter_data['IsArray'] ) {
70
			$type = 'array';
71
		}
72
		elseif ( $parameter_data['IsCallable'] ) {
73
			$type = 'callable';
74
		}
75
		else {
76
			$type = $parameter_data['TypeClass'];
77
		}
78
79
		$hash_part = strlen($type) ? $type . ' ' : '';
80
81
		if ( $parameter_data['IsPassedByReference'] ) {
82
			$hash_part .= '&$' . $parameter_data['Name'];
83
		}
84
		else {
85
			$hash_part .= '$' . $parameter_data['Name'];
86
		}
87
88
		if ( $parameter_data['HasDefaultValue'] ) {
89
			$hash_part .= ' = ';
90
91
			if ( $parameter_data['DefaultConstant'] ) {
92
				$hash_part .= $parameter_data['DefaultConstant'];
93
			}
94
			else {
95
				$hash_part .= $this->decodeValue($parameter_data['DefaultValue']);
96
			}
97
		}
98
99
		return $hash_part;
100
	}
101
102
	/**
103
	 * Decodes json-encoded PHP value.
104
	 *
105
	 * @param string $json_string JSON string.
106
	 *
107
	 * @return string
108
	 */
109
	protected function decodeValue($json_string)
110
	{
111
		$value = var_export(json_decode($json_string), true);
112
		$value = str_replace(array("\t", "\n"), '', $value);
113
		$value = str_replace('array (', 'array(', $value);
114
115
		return $value;
116
	}
117
118
	/**
119
	 * Adds incident.
120
	 *
121
	 * @param string      $group     Incident group.
122
	 * @param string      $incident  Incident description.
123
	 * @param string|null $old_value Old value.
124
	 * @param string|null $new_value New value.
125
	 *
126
	 * @return void
127
	 */
128
	protected function addIncident($group, $incident, $old_value = null, $new_value = null)
129
	{
130
		if ( isset($old_value) || isset($new_value) ) {
131
			$incident = '<fg=white;options=bold>' . $incident . '</>' . PHP_EOL;
132
			$incident .= 'OLD: ' . $old_value . PHP_EOL;
133
			$incident .= 'NEW: ' . $new_value . PHP_EOL;
134
		}
135
136
		$this->incidents[$group][] = $incident;
137
	}
138
139
}
140