ApiUnblock::getAllowedParams()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 *
5
 * Created on Sep 7, 2007
6
 *
7
 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 2 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License along
20
 * with this program; if not, write to the Free Software Foundation, Inc.,
21
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22
 * http://www.gnu.org/copyleft/gpl.html
23
 *
24
 * @file
25
 */
26
27
/**
28
 * API module that facilitates the unblocking of users. Requires API write mode
29
 * to be enabled.
30
 *
31
 * @ingroup API
32
 */
33
class ApiUnblock extends ApiBase {
34
35
	/**
36
	 * Unblocks the specified user or provides the reason the unblock failed.
37
	 */
38
	public function execute() {
39
		$user = $this->getUser();
40
		$params = $this->extractRequestParams();
41
42
		if ( is_null( $params['id'] ) && is_null( $params['user'] ) ) {
43
			$this->dieUsageMsg( 'unblock-notarget' );
44
		}
45
		if ( !is_null( $params['id'] ) && !is_null( $params['user'] ) ) {
46
			$this->dieUsageMsg( 'unblock-idanduser' );
47
		}
48
49
		if ( !$user->isAllowed( 'block' ) ) {
50
			$this->dieUsageMsg( 'cantunblock' );
51
		}
52
		# bug 15810: blocked admins should have limited access here
53 View Code Duplication
		if ( $user->isBlocked() ) {
54
			$status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
55
			if ( $status !== true ) {
56
				$msg = $this->parseMsg( $status );
0 ignored issues
show
Bug introduced by
It seems like $status defined by \SpecialBlock::checkUnbl...$params['user'], $user) on line 54 can also be of type boolean; however, ApiBase::parseMsg() does only seem to accept array|string|object<MessageSpecifier>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
57
				$this->dieUsage(
58
					$msg['info'],
59
					$msg['code'],
60
					0,
61
					[ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $user->getBlock() ) ]
0 ignored issues
show
Bug introduced by
It seems like $user->getBlock() can be null; however, getBlockInfo() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
62
				);
63
			}
64
		}
65
66
		// Check if user can add tags
67 View Code Duplication
		if ( !is_null( $params['tags'] ) ) {
68
			$ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
69
			if ( !$ableToTag->isOK() ) {
70
				$this->dieStatus( $ableToTag );
71
			}
72
		}
73
74
		$data = [
75
			'Target' => is_null( $params['id'] ) ? $params['user'] : "#{$params['id']}",
76
			'Reason' => $params['reason'],
77
			'Tags' => $params['tags']
78
		];
79
		$block = Block::newFromTarget( $data['Target'] );
80
		$retval = SpecialUnblock::processUnblock( $data, $this->getContext() );
81
		if ( $retval !== true ) {
82
			$this->dieUsageMsg( $retval[0] );
83
		}
84
85
		$res['id'] = $block->getId();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$res was never initialized. Although not strictly required by PHP, it is generally a good practice to add $res = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
86
		$target = $block->getType() == Block::TYPE_AUTO ? '' : $block->getTarget();
87
		$res['user'] = $target instanceof User ? $target->getName() : $target;
88
		$res['userid'] = $target instanceof User ? $target->getId() : 0;
89
		$res['reason'] = $params['reason'];
90
		$this->getResult()->addValue( null, $this->getModuleName(), $res );
91
	}
92
93
	public function mustBePosted() {
94
		return true;
95
	}
96
97
	public function isWriteMode() {
98
		return true;
99
	}
100
101 View Code Duplication
	public function getAllowedParams() {
102
		return [
103
			'id' => [
104
				ApiBase::PARAM_TYPE => 'integer',
105
			],
106
			'user' => null,
107
			'reason' => '',
108
			'tags' => [
109
				ApiBase::PARAM_TYPE => 'tags',
110
				ApiBase::PARAM_ISMULTI => true,
111
			],
112
		];
113
	}
114
115
	public function needsToken() {
116
		return 'csrf';
117
	}
118
119
	protected function getExamplesMessages() {
120
		return [
121
			'action=unblock&id=105'
122
				=> 'apihelp-unblock-example-id',
123
			'action=unblock&user=Bob&reason=Sorry%20Bob'
124
				=> 'apihelp-unblock-example-user',
125
		];
126
	}
127
128
	public function getHelpUrls() {
129
		return 'https://www.mediawiki.org/wiki/API:Block';
130
	}
131
}
132