Completed
Push — master ( 5705a3...944e8c )
by mw
158:28 queued 126:23
created

JsonTestCaseFileHandler   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 243
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 38
lcom 1
cbo 1
dl 0
loc 243
rs 8.3999
c 1
b 0
f 1

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A isIncomplete() 0 11 3
A getDebugMode() 0 6 2
A requiredToSkipFor() 0 11 3
A requiredToSkipForConnector() 0 13 3
A requiredToSkipForJsonVersion() 0 10 2
B requiredToSkipForMwVersion() 0 21 5
A getReasonForSkip() 0 3 1
C getSettingsFor() 0 36 11
A getListOfProperties() 0 3 1
A getListOfSubjects() 0 3 1
A getContentsFor() 0 10 2
A findTestCasesFor() 0 3 1
A getFileContentsFor() 0 10 2
1
<?php
2
3
namespace SMW\Tests;
4
5
use SMW\FileReader;
6
7
use RuntimeException;
8
9
/**
10
 * @license GNU GPL v2+
11
 * @since 2.2
12
 *
13
 * @author mwjames
14
 */
15
class JsonTestCaseFileHandler {
16
17
	/**
18
	 * @var FileReader
19
	 */
20
	private $fileReader;
21
22
	/**
23
	 * @var string
24
	 */
25
	private $reasonToSkip = '';
26
27
	/**
28
	 * @since 2.2
29
	 *
30
	 * @param FileReader $fileReader
31
	 */
32
	public function __construct( FileReader $fileReader ) {
33
		$this->fileReader = $fileReader;
34
	}
35
36
	/**
37
	 * @since 2.2
38
	 *
39
	 * @return boolean
40
	 */
41
	public function isIncomplete() {
42
43
		$meta = $this->getFileContentsFor( 'meta' );
44
		$isIncomplete = isset( $meta['is-incomplete'] ) ? (bool)$meta['is-incomplete'] : false;
45
46
		if ( $isIncomplete ) {
47
			$this->reasonToSkip = '"'. $this->getFileContentsFor( 'description' ) . '" has been marked as incomplete.';
48
		}
49
50
		return $isIncomplete;
51
	}
52
53
	/**
54
	 * @since 2.2
55
	 *
56
	 * @return boolean
57
	 */
58
	public function getDebugMode() {
59
60
		$meta = $this->getFileContentsFor( 'meta' );
61
62
		return isset( $meta['debug'] ) ? (bool)$meta['debug'] : false;
63
	}
64
65
	/**
66
	 * @since 2.4
67
	 *
68
	 * @param array $case
69
	 * @param string $identifier
70
	 *
71
	 * @return boolean
72
	 */
73
	public function requiredToSkipFor( array $case, $identifier ) {
74
75
		$skipOn = isset( $case['skip-on'] ) ? $case['skip-on'] : array();
76
		$identifier = strtolower( $identifier );
77
78
		if ( in_array( $identifier, array_keys( $skipOn ) ) ) {
79
			return true;
80
		}
81
82
		return false;
83
	}
84
85
	/**
86
	 * @since 2.2
87
	 *
88
	 * @return boolean
89
	 */
90
	public function requiredToSkipForConnector( $connectorId ) {
91
92
		$connectorId = strtolower( $connectorId );
93
		$meta = $this->getFileContentsFor( 'meta' );
94
95
		$skipOn = isset( $meta['skip-on'] ) ? $meta['skip-on'] : array();
96
97
		if ( in_array( $connectorId, array_keys( $skipOn ) ) ) {
98
			$this->reasonToSkip = $skipOn[$connectorId];
99
		}
100
101
		return $this->reasonToSkip !== '';
102
	}
103
104
	/**
105
	 * @since 2.2
106
	 *
107
	 * @return boolean
108
	 */
109
	public function requiredToSkipForJsonVersion( $version ) {
110
111
		$meta = $this->getFileContentsFor( 'meta' );
112
113
		if ( version_compare( $version, $meta['version'], 'lt' ) ) {
114
			$this->reasonToSkip = $meta['version'] . ' as file version is not supported';
115
		}
116
117
		return $this->reasonToSkip !== '';
118
	}
119
120
	/**
121
	 * @since 2.2
122
	 *
123
	 * @return boolean
124
	 */
125
	public function requiredToSkipForMwVersion( $mwVersion ) {
126
127
		$meta = $this->getFileContentsFor( 'meta' );
128
		$skipOn = isset( $meta['skip-on'] ) ? $meta['skip-on'] : array();
129
130
		foreach ( $skipOn as $id => $reason ) {
131
132
			if ( strpos( $id, 'mw-' ) === false ) {
133
				continue;
134
			}
135
136
			list( $mw, $versionToSkip ) = explode( "mw-", $id, 2 );
0 ignored issues
show
Unused Code introduced by
The assignment to $mw is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
137
138
			if ( version_compare( $mwVersion, $versionToSkip, '=' ) ) {
139
				$this->reasonToSkip = "MediaWiki " . $mwVersion . " version is not supported ({$reason})";
140
				break;
141
			}
142
		}
143
144
		return $this->reasonToSkip !== '';
145
	}
146
147
	/**
148
	 * @since 2.2
149
	 *
150
	 * @return string
151
	 */
152
	public function getReasonForSkip() {
153
		return $this->reasonToSkip;
154
	}
155
156
	/**
157
	 * @since 2.2
158
	 *
159
	 * @return array|integer|string|boolean
160
	 * @throws RuntimeException
161
	 */
162
	public function getSettingsFor( $key ) {
0 ignored issues
show
Coding Style introduced by
getSettingsFor uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
163
164
		$settings = $this->getFileContentsFor( 'settings' );
165
166
		if ( ( $key === 'wgContLang' || $key === 'wgLang' ) && isset( $settings[$key] ) ) {
167
			\RequestContext::getMain()->setLanguage( $settings[$key] );
168
			return \Language::factory( $settings[$key] );
169
		}
170
171
		// Needs special attention due to NS constant usage
172
		if ( $key === 'smwgNamespacesWithSemanticLinks' && isset( $settings[$key] ) ) {
173
			$smwgNamespacesWithSemanticLinks = array();
174
175
			foreach ( $settings[$key] as $ns => $value ) {
176
				$smwgNamespacesWithSemanticLinks[constant( $ns )] = (bool)$value;
177
			}
178
179
			return $smwgNamespacesWithSemanticLinks;
180
		}
181
182
		// Needs special attention due to constant usage
183
		if ( $key === 'smwgQConceptCaching' && isset( $settings[$key] ) ) {
184
			return constant( $settings[$key] );
185
		}
186
187
		if ( isset( $settings[$key] ) ) {
188
			return $settings[$key];
189
		}
190
191
		// Set default values
192
		if ( isset( $GLOBALS[$key] ) ) {
193
			return $GLOBALS[$key];
194
		}
195
196
		throw new RuntimeException( "{$key} is unknown" );
197
	}
198
199
	/**
200
	 * @since 2.2
201
	 *
202
	 * @return array
203
	 */
204
	public function getListOfProperties() {
205
		return $this->getFileContentsFor( 'properties' );
206
	}
207
208
	/**
209
	 * @since 2.2
210
	 *
211
	 * @return array
212
	 */
213
	public function getListOfSubjects() {
214
		return $this->getFileContentsFor( 'subjects' );
215
	}
216
217
	/**
218
	 * @since 2.4
219
	 *
220
	 * @param string $key
221
	 *
222
	 * @return array
223
	 */
224
	public function getContentsFor( $key ) {
225
226
		try{
227
			$contents = $this->getFileContentsFor( $key );
228
		} catch( \Exception $e ) {
229
			$contents = array();
230
		}
231
232
		return $contents;
233
	}
234
235
	/**
236
	 * @since 2.2
237
	 *
238
	 * @param string $key
239
	 *
240
	 * @return array
241
	 */
242
	public function findTestCasesFor( $key ) {
243
		return $this->getContentsFor( $key );
244
	}
245
246
	private function getFileContentsFor( $index ) {
247
248
		$contents = $this->fileReader->read();
249
250
		if ( isset( $contents[$index] ) ) {
251
			return $contents[$index];
252
		}
253
254
		throw new RuntimeException( "{$index} is unknown" );
255
	}
256
257
}
258