Completed
Push — master ( b85bec...862a1a )
by Alexander
05:15
created

AbstractChecker   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 73.17%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 173
ccs 30
cts 41
cp 0.7317
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
getName() 0 1 ?
A check() 0 9 1
doCheck() 0 1 ?
A decodeValue() 0 8 1
A getCacheKey() 0 4 1
C paramToString() 0 37 8
A addIncident() 0 14 3
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\Checker;
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 12
	public function __construct(CacheProvider $cache)
54
	{
55 12
		$this->cache = $cache;
56 12
	}
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 8
	public function check(ExtendedPdoInterface $source_db, ExtendedPdoInterface $target_db)
74
	{
75 8
		$this->sourceDatabase = $source_db;
76 8
		$this->targetDatabase = $target_db;
77
78 8
		$this->doCheck();
79
80 8
		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 6
	protected function paramToString(array $parameter_data)
98
	{
99 6
		if ( $parameter_data['HasType'] ) {
100 4
			$type = $parameter_data['TypeName'];
101
		}
102 6
		elseif ( $parameter_data['IsArray'] ) {
103
			$type = 'array';
104
		}
105 6
		elseif ( $parameter_data['IsCallable'] ) {
106
			$type = 'callable';
107
		}
108
		else {
109 6
			$type = $parameter_data['TypeClass'];
110
		}
111
112 6
		$hash_part = strlen($type) ? $type . ' ' : '';
113
114 6
		if ( $parameter_data['IsPassedByReference'] ) {
115 4
			$hash_part .= '&$' . $parameter_data['Name'];
116
		}
117
		else {
118 6
			$hash_part .= '$' . $parameter_data['Name'];
119
		}
120
121 6
		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 6
		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
	 * Adds incident.
153
	 *
154
	 * @param string      $type      Incident type.
155
	 * @param string      $element   Element affected.
156
	 * @param string|null $old_value Old value.
157
	 * @param string|null $new_value New value.
158
	 *
159
	 * @return void
160
	 */
161 4
	protected function addIncident($type, $element, $old_value = null, $new_value = null)
162
	{
163
		$incident_record = array(
164 4
			'type' => $type,
165 4
			'element' => $element,
166
		);
167
168 4
		if ( isset($old_value) || isset($new_value) ) {
169 3
			$incident_record['old'] = $old_value;
170 3
			$incident_record['new'] = $new_value;
171
		}
172
173 4
		$this->_incidents[] = $incident_record;
174 4
	}
175
176
	/**
177
	 * Returns cache key valid for specific database only.
178
	 *
179
	 * @param ExtendedPdoInterface $db        Database.
180
	 * @param string               $cache_key Cache key.
181
	 *
182
	 * @return string
183
	 */
184 4
	protected function getCacheKey(ExtendedPdoInterface $db, $cache_key)
185
	{
186 4
		return sha1($db->getDsn()) . ':' . $cache_key;
187
	}
188
189
}
190