Completed
Push — master ( 35f867...b05c28 )
by mw
33:30
created

DocumentationParserFunction::getOutputForErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace SMW\ParserFunctions;
4
5
use ParamProcessor\ParamDefinition;
6
use ParamProcessor\ProcessingError;
7
use ParamProcessor\ProcessingResult;
8
use Parser;
9
use ParserHooks\HookDefinition;
10
use ParserHooks\HookHandler;
11
use SMW\ParameterListDocBuilder;
12
use SMWQueryProcessor;
13
14
/**
15
 * Class that provides the {{#smwdoc}} parser function, which displays parameter
16
 * documentation for a specified result format.
17
 *
18
 * @ingroup ParserFunction
19
 *
20
 * @license GNU GPL v2+
21
 * @author Jeroen De Dauw < [email protected] >
22
 */
23
class DocumentationParserFunction implements HookHandler {
24
25
	/**
26
	 * @var string
27
	 */
28
	private $language;
29
30
	/**
31
	 * @param Parser $parser
32
	 * @param ProcessingResult $result
33
	 *
34
	 * @return mixed
35
	 */
36
	public function handle( Parser $parser, ProcessingResult $result ) {
37 2
		if ( $result->hasFatal() ) {
38 2
			return $this->getOutputForErrors( $result->getErrors() );
39
		}
40 2
41
		$parameters = $result->getParameters();
42 2
43
		$this->language = $parameters['language']->getValue();
44 2
45 1
		$params = $this->getFormatParameters( $parameters['format']->getValue() );
46 1
47
		if ( $parameters['parameters']->getValue() === 'specific' ) {
48
			foreach ( array_keys( SMWQueryProcessor::getParameters() ) as $name ) {
49 1
				unset( $params[$name] );
50
			}
51
		}
52
		elseif ( $parameters['parameters']->getValue() === 'base' ) {
53
			foreach ( array_diff_key( $params, SMWQueryProcessor::getParameters() ) as $param ) {
54
				unset( $params[$param->getName()] );
55 2
			}
56
		}
57 2
58
		$docBuilder = new ParameterListDocBuilder( $this->newMessageFunction() );
59
60 2
		return $docBuilder->getParameterTable( $params );
61 2
	}
62
63 2
	private function newMessageFunction() {
64 1
		$language = $this->language;
65 1
66 1
		return function() use ( $language ) {
67 2
			$args = func_get_args();
68
			$key = array_shift( $args );
69
			return wfMessage( $key )->params( $args )->useDatabase( true )->inLanguage( $language )->text();
70
		};
71
	}
72
73
	/**
74
	 * @param string $format
75 2
	 *
76 2
	 * @return array of IParamDefinition
77 1
	 */
78
	private function getFormatParameters( $format ) {
0 ignored issues
show
Coding Style introduced by
getFormatParameters 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...
79
		if ( !array_key_exists( $format, $GLOBALS['smwgResultFormats'] ) ) {
80 1
			return array();
81 1
		}
82
83
		return ParamDefinition::getCleanDefinitions(
84
			SMWQueryProcessor::getResultPrinter( $format )->getParamDefinitions( SMWQueryProcessor::getParameters() )
85 14
		);
86 14
	}
87 14
88
	/**
89
	 * @param ProcessingError[] $errors
90 14
	 * @return string
91 14
	 */
92 14
	private function getOutputForErrors( $errors ) {
0 ignored issues
show
Unused Code introduced by
The parameter $errors is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93 14
		// TODO: see https://github.com/SemanticMediaWiki/SemanticMediaWiki/issues/1485
94
		return 'A fatal error occurred in the #smwdoc parser function';
95 14
	}
96 14
97 14
	public static function getHookDefinition() {
0 ignored issues
show
Coding Style introduced by
getHookDefinition 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...
98
		return new HookDefinition(
99
			'smwdoc',
100
			array(
0 ignored issues
show
Documentation introduced by
array(array('name' => 'f...efault' => 'specific')) is of type array<integer,array<stri...fault\":\"string\"}>"}>, but the function expects a array<integer,object<Par...essor\ParamDefinition>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
101
				array(
102
					'name' => 'format',
103
					'message' => 'smw-smwdoc-par-format',
104
					'values' => array_keys( $GLOBALS['smwgResultFormats'] ),
105
				),
106 14
				array(
107
					'name' => 'language',
108
					'message' => 'smw-smwdoc-par-language',
109
					'default' => $GLOBALS['wgLanguageCode'],
110
				),
111
				array(
112
					'name' => 'parameters',
113
					'message' => 'smw-smwdoc-par-parameters',
114
					'values' => array( 'all', 'specific', 'base' ),
115
					'default' => 'specific',
116
				),
117
			),
118
			array( 'format', 'language', 'parameters' )
119
		);
120
	}
121
122
}
123