ParserDiffTest::formatArray()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 2
nop 1
dl 0
loc 10
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * Fake parser that output the difference of two different parsers
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License along
16
 * with this program; if not, write to the Free Software Foundation, Inc.,
17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
 * http://www.gnu.org/copyleft/gpl.html
19
 *
20
 * @file
21
 * @ingroup Parser
22
 */
23
24
/**
25
 * @ingroup Parser
26
 */
27
class ParserDiffTest
28
{
29
	public $parsers;
30
	public $conf;
31
	public $shortOutput = false;
32
33
	public function __construct( $conf ) {
34
		if ( !isset( $conf['parsers'] ) ) {
35
			throw new MWException( __METHOD__ . ': no parsers specified' );
36
		}
37
		$this->conf = $conf;
38
	}
39
40
	public function init() {
41
		if ( !is_null( $this->parsers ) ) {
42
			return;
43
		}
44
45
		if ( isset( $this->conf['shortOutput'] ) ) {
46
			$this->shortOutput = $this->conf['shortOutput'];
47
		}
48
49
		foreach ( $this->conf['parsers'] as $i => $parserConf ) {
50
			if ( !is_array( $parserConf ) ) {
51
				$class = $parserConf;
52
				$parserConf = [ 'class' => $parserConf ];
53
			} else {
54
				$class = $parserConf['class'];
55
			}
56
			$this->parsers[$i] = new $class( $parserConf );
57
		}
58
	}
59
60
	public function __call( $name, $args ) {
61
		$this->init();
62
		$results = [];
63
		$mismatch = false;
64
		$lastResult = null;
65
		$first = true;
66
		foreach ( $this->parsers as $i => $parser ) {
67
			$currentResult = call_user_func_array( [ &$this->parsers[$i], $name ], $args );
68
			if ( $first ) {
69
				$first = false;
70
			} else {
71
				if ( is_object( $lastResult ) ) {
72
					if ( $lastResult != $currentResult ) {
73
						$mismatch = true;
74
					}
75
				} else {
76
					if ( $lastResult !== $currentResult ) {
77
						$mismatch = true;
78
					}
79
				}
80
			}
81
			$results[$i] = $currentResult;
82
			$lastResult = $currentResult;
83
		}
84
		if ( $mismatch ) {
85
			if ( count( $results ) == 2 ) {
86
				$resultsList = [];
87
				foreach ( $this->parsers as $i => $parser ) {
88
					$resultsList[] = var_export( $results[$i], true );
89
				}
90
				$diff = wfDiff( $resultsList[0], $resultsList[1] );
0 ignored issues
show
Deprecated Code introduced by
The function wfDiff() has been deprecated with message: since 1.25, use DiffEngine/UnifiedDiffFormatter directly

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
91
			} else {
92
				$diff = '[too many parsers]';
93
			}
94
			$msg = "ParserDiffTest: results mismatch on call to $name\n";
95
			if ( !$this->shortOutput ) {
96
				$msg .= 'Arguments: ' . $this->formatArray( $args ) . "\n";
97
			}
98
			$msg .= 'Results: ' . $this->formatArray( $results ) . "\n" .
99
				"Diff: $diff\n";
100
			throw new MWException( $msg );
101
		}
102
		return $lastResult;
103
	}
104
105
	public function formatArray( $array ) {
106
		if ( $this->shortOutput ) {
107
			foreach ( $array as $key => $value ) {
108
				if ( $value instanceof ParserOutput ) {
109
					$array[$key] = "ParserOutput: {$value->getText()}";
110
				}
111
			}
112
		}
113
		return var_export( $array, true );
114
	}
115
116
	public function setFunctionHook( $id, $callback, $flags = 0 ) {
117
		$this->init();
118
		foreach ( $this->parsers as $parser ) {
119
			$parser->setFunctionHook( $id, $callback, $flags );
120
		}
121
	}
122
}
123