1 | <?php |
||
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) |
|
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) |
|
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 | 6 | $hash_part .= ' = '; |
|
123 | |||
124 | 6 | if ( $parameter_data['DefaultConstant'] ) { |
|
125 | $hash_part .= $parameter_data['DefaultConstant']; |
||
126 | } |
||
127 | else { |
||
128 | 6 | $hash_part .= $this->decodeValue($parameter_data['DefaultValue']); |
|
129 | } |
||
130 | } |
||
131 | |||
132 | 6 | return $hash_part; |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * Determines if 2 param signatures are compatible. |
||
137 | * |
||
138 | * @param string $source_signature Source signature. |
||
139 | * @param string $target_signature Target signature. |
||
140 | * |
||
141 | * @return boolean |
||
142 | */ |
||
143 | 6 | protected function isParamSignatureCompatible($source_signature, $target_signature) |
|
144 | { |
||
145 | 6 | if ( $source_signature === $target_signature ) { |
|
146 | 6 | return true; |
|
147 | } |
||
148 | |||
149 | 3 | $source_params = $source_signature ? explode(', ', $source_signature) : array(); |
|
150 | 3 | $target_params = $target_signature ? explode(', ', $target_signature) : array(); |
|
151 | 3 | $source_param_count = count($source_params); |
|
152 | |||
153 | // Beginning of target signature doesn't match source signature. |
||
154 | 3 | if ( $source_params !== array_slice($target_params, 0, $source_param_count) ) { |
|
155 | 3 | $target_params_wo_defaults = preg_replace('/ = [^,]*/', '', $target_params); |
|
156 | |||
157 | // Making existing parameter optional doesn't break compatibility. |
||
158 | 3 | if ( $source_params !== array_slice($target_params_wo_defaults, 0, $source_param_count) ) { |
|
159 | 3 | return false; |
|
160 | } |
||
161 | } |
||
162 | |||
163 | 3 | $added_params = array_slice($target_params, $source_param_count); |
|
164 | |||
165 | // No new parameters added. |
||
166 | 3 | if ( !$added_params ) { |
|
167 | 3 | return true; |
|
168 | } |
||
169 | |||
170 | 3 | $is_compatible = true; |
|
171 | |||
172 | // When all added parameters are optional, then signature is compatible. |
||
173 | 3 | foreach ( $added_params as $added_param ) { |
|
174 | 3 | if ( strpos($added_param, '=') === false ) { |
|
175 | 3 | $is_compatible = false; |
|
176 | 3 | break; |
|
177 | } |
||
178 | } |
||
179 | |||
180 | 3 | return $is_compatible; |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * Decodes json-encoded PHP value. |
||
185 | * |
||
186 | * @param string $json_string JSON string. |
||
187 | * |
||
188 | * @return string |
||
189 | */ |
||
190 | 6 | protected function decodeValue($json_string) |
|
198 | |||
199 | /** |
||
200 | * Adds incident. |
||
201 | * |
||
202 | * @param string $type Incident type. |
||
203 | * @param string $element Element affected. |
||
204 | * @param string|null $old_value Old value. |
||
205 | * @param string|null $new_value New value. |
||
206 | * |
||
207 | * @return void |
||
208 | */ |
||
209 | 4 | protected function addIncident($type, $element, $old_value = null, $new_value = null) |
|
210 | { |
||
211 | $incident_record = array( |
||
212 | 4 | 'type' => $type, |
|
213 | 4 | 'element' => $element, |
|
214 | ); |
||
215 | |||
216 | 4 | if ( isset($old_value) || isset($new_value) ) { |
|
217 | 3 | $incident_record['old'] = $old_value; |
|
218 | 3 | $incident_record['new'] = $new_value; |
|
219 | } |
||
220 | |||
221 | 4 | $this->_incidents[] = $incident_record; |
|
222 | 4 | } |
|
223 | |||
224 | /** |
||
225 | * Returns cache key valid for specific database only. |
||
226 | * |
||
227 | * @param ExtendedPdoInterface $db Database. |
||
228 | * @param string $cache_key Cache key. |
||
229 | * |
||
230 | * @return string |
||
231 | */ |
||
232 | 4 | protected function getCacheKey(ExtendedPdoInterface $db, $cache_key) |
|
236 | |||
237 | } |
||
238 |