Completed
Pull Request — master (#75)
by mw
05:07 queued 03:21
created

AppFactory::getOption()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 4
cp 0
crap 6
1
<?php
2
3
namespace SESP;
4
5
use Psr\Log\NullLogger;
6
use Psr\Log\LoggerInterface;
7
use Psr\Log\LoggerAwareInterface;
8
use Title;
9
use WikiPage;
10
use User;
11
12
/**
13
 * @ingroup SESP
14
 *
15
 * @license GNU GPL v2+
16
 * @since 1.3
17
 *
18
 * @author mwjames
19
 */
20
class AppFactory implements LoggerAwareInterface {
21
22
	/**
23
	 * @var array
24
	 */
25
	private $connection;
26
27
	/**
28
	 * @var array
29
	 */
30
	private $options;
31
32
	/**
33
	 * @var LoggerInterface
34
	 */
35
	private $logger;
36
37
	/**
38
	 * @var PropertyDefinitions
39
	 */
40
	private $propertyDefinitions;
41
42
	/**
43
	 * @since 2.0
44
	 *
45
	 * @param Database $connection
46
	 * @param array $options
47
	 */
48
	public function __construct( $connection, array $options = array() ) {
49
		$this->connection = $connection;
0 ignored issues
show
Documentation Bug introduced by
It seems like $connection of type object<SESP\Database> 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...
50
		$this->options = $options;
51
	}
52
53
	/**
54
	 * @see LoggerAwareInterface::setLogger
55
	 *
56
	 * @since 2.5
57
	 *
58
	 * @param LoggerInterface $logger
59
	 */
60
	public function setLogger( LoggerInterface $logger ) {
61
		$this->logger = $logger;
62
	}
63
64
	/**
65
	 * @since 3.0
66
	 *
67
	 * @param LoggerInterface
68
	 */
69
	public function getLogger() {
70
71
		if ( $this->logger === null ) {
72
			return new NullLogger();
73
		}
74
75
		return $this->logger;
76
	}
77
78
	/**
79
	 * @since 2.4
80
	 *
81
	 * @param string $key
82
	 * @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...
83
	 *
84
	 * @return mixed|false
85
	 */
86
	public function getOption( $key, $default = false ) {
87
88
		if ( isset( $this->options[$key] ) ) {
89
			return $this->options[$key];
90
		}
91
92
		return $default;
93
	}
94
95
	/**
96
	 * @since 2.0
97
	 *
98
	 * @return PropertyDefinitions
99
	 */
100
	public function getPropertyDefinitions() {
101
102
		if ( $this->propertyDefinitions !== null ) {
103
			return $this->propertyDefinitions;
104
		}
105
106
		$this->propertyDefinitions = new PropertyDefinitions(
107
			$this->getOption( 'sespPropertyDefinitionFile' )
108
		);
109
110
		$this->propertyDefinitions->setLocalPropertyDefinitions(
111
			$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...
112
		);
113
114
		return $this->propertyDefinitions;
115
	}
116
117
	/**
118
	 * @since 1.3
119
	 *
120
	 * @return DatabaseBase
121
	 */
122
	public function getConnection() {
123
		return $this->connection;
124
	}
125
126
	/**
127
	 * @since 1.3
128
	 *
129
	 * @param Title $title
130
	 *
131
	 * @return WikiPage
132
	 */
133
	public function newWikiPage( Title $title ) {
134
135
		// #55
136
		// Fight a possible DB corruption and avoid "NS_MEDIA is a virtual namespace; use NS_FILE"
137
		if ( $title->getNamespace() === NS_MEDIA ) {
138
			$title = Title::makeTitleSafe(
139
				NS_FILE,
140
				$title->getDBkey(),
141
				$title->getInterwiki(),
142
				$title->getFragment()
143
			);
144
		}
145
146
		return WikiPage::factory( $title );
147
	}
148
149
	/**
150
	 * @since 1.3
151
	 *
152
	 * @param Title $title
153
	 *
154
	 * @return User
155
	 */
156
	public function newUserFromTitle( Title $title ) {
157
		return User::newFromName( $title->getText() );
158
	}
159
160
	/**
161
	 * @since 1.3
162
	 *
163
	 * @param $id
164
	 *
165
	 * @return User
166
	 */
167
	public function newUserFromID( $id ) {
168
		return User::newFromId( $id );
169
	}
170
171
}
172