Completed
Push — refact-v2 ( 0317e0 )
by mw
04:47
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 SESP\Annotator\ShortUrlAnnotator;
6
use SESP\Annotator\ExifDataAnnotator;
7
use SMW\SemanticData;
8
use File;
9
use Title;
10
use Psr\Log\NullLogger;
11
use Psr\Log\LoggerInterface;
12
use Psr\Log\LoggerAwareInterface;
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 $connection;
28
29
	/**
30
	 * @var array
31
	 */
32
	private $options;
33
34
	/**
35
	 * @var LoggerInterface
36
	 */
37
	private $logger;
38
39
	/**
40
	 * @var PropertyDefinitions
41
	 */
42
	private $propertyDefinitions;
43
44
	/**
45
	 * @since 2.0
46
	 *
47
	 * @param Database $connection
48
	 * @param array $options
49
	 */
50
	public function __construct( $connection, array $options = array() ) {
51
		$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...
52
		$this->options = $options;
53
		$this->logger = new NullLogger();
54
	}
55
56
	/**
57
	 * @see LoggerAwareInterface::setLogger
58
	 *
59
	 * @since 2.5
60
	 *
61
	 * @param LoggerInterface $logger
62
	 */
63
	public function setLogger( LoggerInterface $logger ) {
64
		$this->logger = $logger;
65
	}
66
67
	/**
68
	 * @since 3.0
69
	 *
70
	 * @param LoggerInterface
71
	 */
72
	public function getLogger() {
73
		return $this->logger;
74
	}
75
76
	/**
77
	 * @since 2.4
78
	 *
79
	 * @param string $key
80
	 * @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...
81
	 *
82
	 * @return mixed|false
83
	 */
84
	public function getOption( $key, $default = false ) {
85
86
		if ( isset( $this->options[$key] ) ) {
87
			return $this->options[$key];
88
		}
89
90
		return $default;
91
	}
92
93
	/**
94
	 * @since 2.0
95
	 *
96
	 * @return PropertyDefinitions
97
	 */
98
	public function getPropertyDefinitions() {
99
100
		if ( $this->propertyDefinitions !== null ) {
101
			return $this->propertyDefinitions;
102
		}
103
104
		$this->propertyDefinitions = new PropertyDefinitions(
105
			$this->getOption( 'sespPropertyDefinitionFile' )
106
		);
107
108
		$this->propertyDefinitions->setLocalPropertyDefinitions(
109
			$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...
110
		);
111
112
		return $this->propertyDefinitions;
113
	}
114
115
	/**
116
	 * @since 1.3
117
	 *
118
	 * @return DatabaseBase
119
	 */
120
	public function getConnection() {
121
		return $this->connection;
122
	}
123
124
	/**
125
	 * @since 1.3
126
	 *
127
	 * @param Title $title
128
	 *
129
	 * @return WikiPage
130
	 */
131
	public function newWikiPage( Title $title ) {
132
133
		// #55
134
		// Fight a possible DB corruption and avoid "NS_MEDIA is a virtual namespace; use NS_FILE"
135
		if ( $title->getNamespace() === NS_MEDIA ) {
136
			$title = Title::makeTitleSafe(
137
				NS_FILE,
138
				$title->getDBkey(),
139
				$title->getInterwiki(),
140
				$title->getFragment()
141
			);
142
		}
143
144
		return \WikiPage::factory( $title );
145
	}
146
147
	/**
148
	 * @since 1.3
149
	 *
150
	 * @param Title $title
151
	 *
152
	 * @return User
153
	 */
154
	public function newUserFromTitle( Title $title ) {
155
		return \User::newFromName( $title->getText() );
156
	}
157
158
	/**
159
	 * @since 1.3
160
	 *
161
	 * @param $id
162
	 *
163
	 * @return User
164
	 */
165
	public function newUserFromID( $id ) {
166
		return \User::newFromId( $id );
167
	}
168
169
}
170