Completed
Push — master ( 49ddd3...7189a8 )
by mw
02:22
created

AppFactory::setLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
3
namespace SESP;
4
5
use Psr\Log\NullLogger;
6
use Psr\Log\LoggerInterface;
7
use Psr\Log\LoggerAwareInterface;
8
use Onoi\Cache\Cache;
9
use Onoi\Cache\NullCache;
10
use Title;
11
use WikiPage;
12
use User;
13
14
/**
15
 * @ingroup SESP
16
 *
17
 * @license GNU GPL v2+
18
 * @since 1.3
19
 *
20
 * @author mwjames
21
 */
22
class AppFactory implements LoggerAwareInterface {
23
24
	/**
25
	 * @var array
26
	 */
27
	private $options;
28
29
	/**
30
	 * @var Cache
31
	 */
32
	private $cache;
33
34
	/**
35
	 * @var array
36
	 */
37
	private $connection;
38
39
	/**
40
	 * @var LoggerInterface
41
	 */
42
	private $logger;
43
44
	/**
45
	 * @var PropertyDefinitions
46
	 */
47
	private $propertyDefinitions;
48
49
	/**
50
	 * @since 2.0
51
	 *
52
	 * @param array $options
53
	 * @param Cache|null $cache
54
	 */
55 9
	public function __construct( array $options = array(), Cache $cache = null ) {
56 9
		$this->options = $options;
57 9
		$this->cache = $cache;
58 9
	}
59
60
	/**
61
	 * @since 2.0
62
	 */
63 1
	public function setConnection( \DatabaseBase $connection ) {
64 1
		$this->connection = $connection;
0 ignored issues
show
Documentation Bug introduced by
It seems like $connection of type object<DatabaseBase> is incompatible with the declared type array of property $connection.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
65 1
	}
66
67
	/**
68
	 * @since 1.3
69
	 *
70
	 * @return DatabaseBase
71
	 */
72 1
	public function getConnection() {
73
74 1
		if ( $this->connection === null ) {
75
			$this->connection = wfGetDB( DB_SLAVE );
76
		}
77
78 1
		return $this->connection;
79
	}
80
81
	/**
82
	 * @see LoggerAwareInterface::setLogger
83
	 *
84
	 * @since 2.0
85
	 *
86
	 * @param LoggerInterface $logger
87
	 */
88 1
	public function setLogger( LoggerInterface $logger ) {
89 1
		$this->logger = $logger;
90 1
	}
91
92
	/**
93
	 * @since 2.0
94
	 *
95
	 * @param LoggerInterface
96
	 */
97 1
	public function getLogger() {
98
99 1
		if ( $this->logger === null ) {
100 1
			return new NullLogger();
101
		}
102
103 1
		return $this->logger;
104
	}
105
106
	/**
107
	 * @since 2.0
108
	 *
109
	 * @param string $key
110
	 * @param $default $mixed
0 ignored issues
show
Documentation introduced by
The doc-type $default could not be parsed: Unknown type name "$default" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
Bug introduced by
There is no parameter named $mixed. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
111
	 *
112
	 * @return mixed|false
113
	 */
114 2
	public function getOption( $key, $default = false ) {
115
116 2
		if ( isset( $this->options[$key] ) ) {
117 2
			return $this->options[$key];
118
		}
119
120 1
		return $default;
121
	}
122
123
	/**
124
	 * @since 2.0
125
	 *
126
	 * @return PropertyDefinitions
127
	 */
128 1
	public function getPropertyDefinitions() {
129
130 1
		if ( $this->propertyDefinitions !== null ) {
131 1
			return $this->propertyDefinitions;
132
		}
133
134 1
		$labelFetcher = new LabelFetcher(
135 1
			$this->cache,
136 1
			$GLOBALS['wgLang']->getCode()
137 1
		);
138
139 1
		$labelFetcher->setLabelCacheVersion(
140 1
			$this->getOption( 'sespLabelCacheVersion', 0 )
0 ignored issues
show
Documentation introduced by
0 is of type integer, but the function expects a boolean.

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...
141 1
		);
142
143 1
		$this->propertyDefinitions = new PropertyDefinitions(
144 1
			$labelFetcher,
145 1
			$this->getOption( 'sespPropertyDefinitionFile' )
146 1
		);
147
148 1
		$this->propertyDefinitions->setLocalPropertyDefinitions(
149 1
			$this->getOption( 'sespLocalPropertyDefinitions', array() )
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a boolean.

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...
150 1
		);
151
152 1
		return $this->propertyDefinitions;
153
	}
154
155
	/**
156
	 * @since 1.3
157
	 *
158
	 * @param Title $title
159
	 *
160
	 * @return WikiPage
161
	 */
162 2
	public function newWikiPage( Title $title ) {
163
164
		// #55
165
		// Fight a possible DB corruption and avoid "NS_MEDIA is a virtual namespace; use NS_FILE"
166 2
		if ( $title->getNamespace() === NS_MEDIA ) {
167 1
			$title = Title::makeTitleSafe(
168 1
				NS_FILE,
169 1
				$title->getDBkey(),
170 1
				$title->getInterwiki(),
171 1
				$title->getFragment()
172 1
			);
173 1
		}
174
175 2
		return WikiPage::factory( $title );
176
	}
177
178
	/**
179
	 * @since 1.3
180
	 *
181
	 * @param Title $title
182
	 *
183
	 * @return User
184
	 */
185 1
	public function newUserFromTitle( Title $title ) {
186 1
		return User::newFromName( $title->getText() );
187
	}
188
189
	/**
190
	 * @since 1.3
191
	 *
192
	 * @param $id
193
	 *
194
	 * @return User
195
	 */
196 1
	public function newUserFromID( $id ) {
197 1
		return User::newFromId( $id );
198
	}
199
200
}
201