FunctionRunner::run()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 1
1
<?php
2
3
namespace ParserHooks;
4
5
use ParamProcessor\Processor;
6
use Parser;
7
use ParserHooks\Internal\Runner;
8
use PPFrame;
9
10
/**
11
 * Class that handles a parser function hook call coming from MediaWiki
12
 * by processing the parameters as declared in the hook definition and
13
 * then passes the result to the hook handler.
14
 *
15
 * @since 1.0
16
 *
17
 * @licence GNU GPL v2+
18
 * @author Jeroen De Dauw < [email protected] >
19
 */
20
class FunctionRunner extends Runner {
21
22
	const OPT_DO_PARSE = 'parse'; // Boolean, since 1.1
23
24
	/**
25
	 * @since 1.1
26
	 *
27
	 * @param HookDefinition $definition
28
	 * @param HookHandler $handler
29
	 * @param array $options
30
	 * @param Processor|null $paramProcessor
31
	 */
32 2
	public function __construct( HookDefinition $definition, HookHandler $handler, array $options = [], Processor $paramProcessor = null ) {
33 2
		parent::__construct( $definition, $handler, $options, $paramProcessor );
34 2
	}
35
36
	/**
37
	 * @since 1.0
38
	 *
39
	 * @param Parser $parser
40
	 * @param array $arguments
41
	 * @param PPFrame $frame
42
	 *
43
	 * @return array
44
	 */
45 2
	public function run( Parser &$parser, array $arguments, PPFrame $frame ) {
46 2
		$resultText = $this->handler->handle(
47 2
			$parser,
48 2
			$this->getProcessedParams( $this->getExpandedParams( $arguments, $frame ) )
49
		);
50
51 2
		return $this->getResultStructure( $resultText );
52
	}
53
54 2
	protected function getExpandedParams( array $rawArguments, PPFrame $frame ) {
55 2
		$rawArgList = [];
56
57 2
		foreach( $rawArguments as $arg ) {
58 2
			$rawArgList[] = $frame->expand( $arg );
59
		}
60
61 2
		return $rawArgList;
62
	}
63
64 2
	protected function getProcessedParams( array $expendedArgs ) {
65 2
		$this->paramProcessor->setFunctionParams(
66 2
			$expendedArgs,
67 2
			$this->definition->getParameters(),
68 2
			$this->definition->getDefaultParameters()
69
		);
70
71 2
		return $this->paramProcessor->processParameters();
72
	}
73
74 2
	protected function getResultStructure( $resultText ) {
75 2
		$result = [ $resultText ];
76
77 2
		if ( !$this->getOption( self::OPT_DO_PARSE ) ) {
78 1
			$result['noparse'] = true;
79 1
			$result['isHTML'] = true;
80
		}
81
82 2
		return $result;
83
	}
84
85
	/**
86
	 * @see Runner::getDefaultOptions
87
	 *
88
	 * @since 1.1
89
	 *
90
	 * @return array
91
	 */
92 2
	protected function getDefaultOptions() {
93
		return [
94 2
			self::OPT_DO_PARSE => true,
95
		];
96
	}
97
98
}
99