Passed
Push — master ( 6c17d9...96f05a )
by Jeroen De
39s
created

Setup::newCachingFileFetcher()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

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