Failed Conditions
Push — master ( 26605a...607f8d )
by Alexander
01:54
created

AbstractChecker::getCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
197
	{
198
		return sha1($db->getDsn()) . ':' . $cache_key;
199
	}
200
201
}
202