Completed
Push — master ( 21ccfa...6f5178 )
by Mark A.
08:49
created

src/AppFactory.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 10
	public function __construct( array $options = [], Cache $cache = null ) {
56 10
		$this->options = $options;
57 10
		$this->cache = $cache;
58 10
	}
59
60
	/**
61
	 * @since 2.0
62
	 */
63 2
	public function setConnection( \DatabaseBase $connection ) {
64 2
		$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 2
	}
66
67
	/**
68
	 * @since 1.3
69
	 *
70
	 * @return DatabaseBase
71
	 */
72 2
	public function getConnection() {
73
74 2
		if ( $this->connection === null ) {
75
			$this->connection = wfGetDB( DB_REPLICA );
76
		}
77
78 2
		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
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...
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( 'sespgLabelCacheVersion', 0 )
0 ignored issues
show
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( 'sespgDefinitionsFile' )
146 1
		);
147
148 1
		$this->propertyDefinitions->setLocalPropertyDefinitions(
149 1
			$this->getOption( 'sespgLocalDefinitions', [] )
0 ignored issues
show
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
	 * @since 2.0
202
	 *
203
	 * @param null|Title $title to get the DBLogReader
204
	 * @param string $type which log entries to get (default: approval)
205
	 * @return DatabaseLogReader
206
	 */
207 1
	public function newDatabaseLogReader( Title $title = null, $type = 'approval' ) {
208 1
		return new DatabaseLogReader( $this->getConnection(), $title, $type );
209
	}
210
}
211