Completed
Push — master ( 14218a...10a4b7 )
by Jeroen De
03:39
created

Setup::newFileFetcher()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 9
rs 9.6666
cc 2
eloc 6
nc 1
nop 0
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->registerMessageFiles();
37
		$this->registerParserHookHandler();
38
	}
39
40
	private function registerExtensionCredits() {
41
		$this->globals['wgExtensionCredits']['other'][] = array(
42
			'path' => $this->rootDirectory . '/GitHub.php',
43
			'name' => 'GitHub',
44
			'version' => GitHub_VERSION,
45
			'author' => array(
46
				'[https://www.mediawiki.org/wiki/User:Jeroen_De_Dauw Jeroen De Dauw]',
47
			),
48
			'url' => 'https://github.com/JeroenDeDauw/GitHub',
49
			'descriptionmsg' => 'github-desc',
50
			'license-name' => 'GPL-2.0+'
51
		);
52
	}
53
54
	private function registerMessageFiles() {
55
		$this->globals['wgExtensionMessagesFiles']['GitHub'] = $this->rootDirectory . '/GitHub.i18n.php';
56
		$this->globals['wgExtensionMessagesFiles']['GitHubMagic'] = $this->rootDirectory . '/GitHub.i18n.magic.php';
57
		$this->globals['wgMessagesDirs']['GitHub'] = $this->rootDirectory . '/i18n';
58
	}
59
60
	private function loadSettings() {
61
		if ( array_key_exists( 'egGitHubDefaultRepo', $this->globals ) ) {
62
			$this->defaultGitHubRepo = $this->globals['egGitHubDefaultRepo'];
63
		}
64
65
		if ( array_key_exists( 'egGitHubCacheTime', $this->globals ) ) {
66
			$this->cacheTime = $this->globals['egGitHubCacheTime'];
67
		}
68
69
		if ( array_key_exists( 'egGitHubUrl', $this->globals ) ) {
70
			$this->gitHubUrl = $this->globals['egGitHubUrl'];
71
		}
72
73
		if ( array_key_exists( 'egGitHubFetcher', $this->globals ) ) {
74
			$this->gitHubFetcher = $this->globals['egGitHubFetcher'];
75
		}
76
	}
77
78
	private function registerParserHookHandler() {
79
		$self = $this;
80
81
		$this->globals['wgHooks']['ParserFirstCallInit'][] = function( \Parser &$parser ) use ( $self ) {
82
			$hookRegistrant = new HookRegistrant( $parser );
83
84
			$hookRegistrant->registerFunction(
85
				new FunctionRunner(
86
					$self->getGitHubHookDefinition(),
87
					$self->getGitHubHookHandler(),
88
					array(
89
						FunctionRunner::OPT_DO_PARSE => false
90
					)
91
				)
92
			);
93
94
			return true;
95
		};
96
	}
97
98
	public function newFileFetcher() {
99
		return new CachingFileFetcher(
100
			$this->gitHubFetcher === 'mediawiki' ? new MediaWikiFileFetcher() : new SimpleFileFetcher(),
101
			new CombinatoryCache( array(
102
				new SimpleInMemoryCache(),
103
				new MediaWikiCache( wfGetMainCache(), $this->cacheTime )
104
			) )
105
		);
106
	}
107
108
	/**
109
	 * @since 1.0
110
	 *
111
	 * @return HookDefinition
112
	 */
113
	public function getGitHubHookDefinition() {
114
		return new HookDefinition(
115
			'github',
116
			array(
0 ignored issues
show
Documentation introduced by
array('file' => array('d...ithub-par-branchname')) is of type array<string,array<strin...ssage\":\"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...
117
				'file' => array(
118
					'default' => 'README.md',
119
					'aliases' => 'filename',
120
					'message' => 'github-par-filename',
121
				),
122
				'repo' => array(
123
					'default' => $this->defaultGitHubRepo,
124
					'aliases' => 'reponame',
125
					'message' => 'github-par-reponame',
126
				),
127
				'branch' => array(
128
					'default' => 'master',
129
					'aliases' => 'branchname',
130
					'message' => 'github-par-branchname',
131
				),
132
			),
133
			array( 'file', 'repo', 'branch' )
134
		);
135
	}
136
137
	public function getGitHubHookHandler() {
138
		return new GitHubParserHook(
139
			$this->newFileFetcher(),
140
			$this->gitHubUrl
141
		);
142
	}
143
144
}
145