Test Setup Failed
Pull Request — master (#31)
by Jeroen De
10:28
created

Setup::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace GitHub;
4
5
use FileFetcher\CachingFileFetcher;
6
use FileFetcher\SimpleFileFetcher;
7
use ParserHooks\FunctionRunner;
8
use ParserHooks\HookDefinition;
9
use ParserHooks\HookRegistrant;
10
use SimpleCache\Cache\CombinatoryCache;
11
use SimpleCache\Cache\MediaWikiCache;
12
use SimpleCache\Cache\SimpleInMemoryCache;
13
14
/**
15
 * @licence GNU GPL v2+
16
 * @author Jeroen De Dauw < [email protected] >
17
 */
18
class Setup {
19
20
	private $globals;
21
	private $rootDirectory;
22
	private $defaultGitHubRepo = 'JeroenDeDauw/GitHub';
23
	private $cacheTime = 600;
24
	private $gitHubUrl = 'https://cdn.rawgit.com';
25
	private $gitHubFetcher = 'simple';
26
27
	public function __construct( &$globals, $rootDirectory ) {
28
		$this->globals =& $globals;
29
		$this->rootDirectory = $rootDirectory;
30
	}
31
32
	public function run() {
33
		$this->loadSettings();
34
35
		$this->registerExtensionCredits();
36
		$this->registerParserHookHandler();
37
	}
38
39
	private function registerExtensionCredits() {
40
		$this->globals['wgExtensionCredits']['other'][] = array(
41
			'path' => $this->rootDirectory . '/GitHub.php',
42
			'name' => 'GitHub',
43
			'version' => GitHub_VERSION,
44
			'author' => array(
45
				'[https://www.mediawiki.org/wiki/User:Jeroen_De_Dauw Jeroen De Dauw]',
46
			),
47
			'url' => 'https://github.com/JeroenDeDauw/GitHub',
48
			'descriptionmsg' => 'github-desc',
49
			'license-name' => 'GPL-2.0+'
50
		);
51
	}
52
53
	private function loadSettings() {
54
		if ( array_key_exists( 'egGitHubDefaultRepo', $this->globals ) ) {
55
			$this->defaultGitHubRepo = $this->globals['egGitHubDefaultRepo'];
56
		}
57
58
		if ( array_key_exists( 'egGitHubCacheTime', $this->globals ) ) {
59
			$this->cacheTime = $this->globals['egGitHubCacheTime'];
60
		}
61
62
		if ( array_key_exists( 'egGitHubUrl', $this->globals ) ) {
63
			$this->gitHubUrl = $this->globals['egGitHubUrl'];
64
		}
65
66
		if ( array_key_exists( 'egGitHubFetcher', $this->globals ) ) {
67
			$this->gitHubFetcher = $this->globals['egGitHubFetcher'];
68
		}
69
	}
70
71
	private function registerParserHookHandler() {
72
		$self = $this;
73
74
		$this->globals['wgHooks']['ParserFirstCallInit'][] = function( \Parser &$parser ) use ( $self ) {
75
			$hookRegistrant = new HookRegistrant( $parser );
76
77
			$hookRegistrant->registerFunction(
78
				new FunctionRunner(
79
					$self->getGitHubHookDefinition(),
80
					$self->getGitHubHookHandler(),
81
					array(
82
						FunctionRunner::OPT_DO_PARSE => false
83
					)
84
				)
85
			);
86
87
			return true;
88
		};
89
	}
90
91
	public function newFileFetcher() {
92
		return new CachingFileFetcher(
93
			$this->gitHubFetcher === 'mediawiki' ? new MediaWikiFileFetcher() : new SimpleFileFetcher(),
94
			new CombinatoryCache( array(
95
				new SimpleInMemoryCache(),
96
				new MediaWikiCache( wfGetMainCache(), $this->cacheTime )
97
			) )
98
		);
99
	}
100
101
	/**
102
	 * @since 1.0
103
	 *
104
	 * @return HookDefinition
105
	 */
106
	public function getGitHubHookDefinition() {
107
		return new HookDefinition(
108
			'github',
109
			array(
0 ignored issues
show
Documentation introduced by
array('file' => array('d..., 'type' => 'boolean')) is of type array<string,array,{"fil..."type\":\"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...
110
				'file' => array(
111
					'default' => 'README.md',
112
					'aliases' => 'filename',
113
					'message' => 'github-par-filename',
114
				),
115
				'repo' => array(
116
					'default' => $this->defaultGitHubRepo,
117
					'aliases' => 'reponame',
118
					'message' => 'github-par-reponame',
119
				),
120
				'branch' => array(
121
					'default' => 'master',
122
					'aliases' => 'branchname',
123
					'message' => 'github-par-branchname',
124
				),
125
				'lang' => array(
126
					'default' => '',
127
					'message' => 'github-par-lang',
128
				),
129
				'line' => array(
130
					'default' => false,
131
					'message' => 'github-par-line',
132
					'type'    => 'boolean',
133
				),
134
				'start' => array(
135
					'default' => 1,
136
					'message' => 'github-par-start',
137
					'type'    => 'integer',
138
				),
139
				'highlight' => array(
140
					'default' => '',
141
					'message' => 'github-par-highlight',
142
				),
143
				'inline' => array(
144
					'default' => false,
145
					'message' => 'github-par-inline',
146
					'type'    => 'boolean',
147
				),
148
			),
149
			array( 'file', 'repo', 'branch', 'lang' )
150
		);
151
	}
152
153
154
	public function getGitHubHookHandler() {
155
		return new GitHubParserHook(
156
			$this->newFileFetcher(),
157
			$this->gitHubUrl
158
		);
159
	}
160
161
}
162