Protect::execute()   C
last analyzed

Complexity

Conditions 8
Paths 96

Size

Total Lines 47
Code Lines 29

Duplication

Lines 5
Ratio 10.64 %

Importance

Changes 0
Metric Value
cc 8
eloc 29
nc 96
nop 0
dl 5
loc 47
rs 5.7377
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 31 and the first side effect is on line 24.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Protect or unprotect a page.
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License along
16
 * with this program; if not, write to the Free Software Foundation, Inc.,
17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
 * http://www.gnu.org/copyleft/gpl.html
19
 *
20
 * @file
21
 * @ingroup Maintenance
22
 */
23
24
require_once __DIR__ . '/Maintenance.php';
25
26
/**
27
 * Maintenance script that protects or unprotects a page.
28
 *
29
 * @ingroup Maintenance
30
 */
31
class Protect extends Maintenance {
32
	public function __construct() {
33
		parent::__construct();
34
		$this->addDescription( 'Protect or unprotect a page from the command line.' );
35
		$this->addOption( 'unprotect', 'Removes protection' );
36
		$this->addOption( 'semiprotect', 'Adds semi-protection' );
37
		$this->addOption( 'cascade', 'Add cascading protection' );
38
		$this->addOption( 'user', 'Username to protect with', false, true, 'u' );
39
		$this->addOption( 'reason', 'Reason for un/protection', false, true, 'r' );
40
		$this->addArg( 'title', 'Title to protect', true );
41
	}
42
43
	public function execute() {
44
		$userName = $this->getOption( 'u', false );
45
		$reason = $this->getOption( 'r', '' );
46
47
		$cascade = $this->hasOption( 'cascade' );
48
49
		$protection = "sysop";
50
		if ( $this->hasOption( 'semiprotect' ) ) {
51
			$protection = "autoconfirmed";
52
		} elseif ( $this->hasOption( 'unprotect' ) ) {
53
			$protection = "";
54
		}
55
56 View Code Duplication
		if ( $userName === false ) {
57
			$user = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
58
		} else {
59
			$user = User::newFromName( $userName );
60
		}
61
		if ( !$user ) {
62
			$this->error( "Invalid username", true );
63
		}
64
65
		// @todo FIXME: This is reset 7 lines down.
66
		$restrictions = [ 'edit' => $protection, 'move' => $protection ];
0 ignored issues
show
Unused Code introduced by
$restrictions is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
67
68
		$t = Title::newFromText( $this->getArg() );
69
		if ( !$t ) {
70
			$this->error( "Invalid title", true );
71
		}
72
73
		$restrictions = [];
74
		foreach ( $t->getRestrictionTypes() as $type ) {
75
			$restrictions[$type] = $protection;
76
		}
77
78
		# un/protect the article
79
		$this->output( "Updating protection status... " );
80
81
		$page = WikiPage::factory( $t );
0 ignored issues
show
Bug introduced by
It seems like $t defined by \Title::newFromText($this->getArg()) on line 68 can be null; however, WikiPage::factory() 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...
82
		$status = $page->doUpdateRestrictions( $restrictions, [], $cascade, $reason, $user );
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type false or null; however, WikiPage::doUpdateRestrictions() does only seem to accept object<User>, 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...
83
84
		if ( $status->isOK() ) {
85
			$this->output( "done\n" );
86
		} else {
87
			$this->output( "failed\n" );
88
		}
89
	}
90
}
91
92
$maintClass = "Protect";
93
require_once RUN_MAINTENANCE_IF_MAIN;
94