EditCLI   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 74
Duplicated Lines 6.76 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 5
loc 74
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
C execute() 5 58 15

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
 * Make a page edit.
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 to make a page edit.
28
 *
29
 * @ingroup Maintenance
30
 */
31
class EditCLI extends Maintenance {
32
	public function __construct() {
33
		parent::__construct();
34
		$this->addDescription( 'Edit an article from the command line, text is from stdin' );
35
		$this->addOption( 'user', 'Username', false, true, 'u' );
36
		$this->addOption( 'summary', 'Edit summary', false, true, 's' );
37
		$this->addOption( 'minor', 'Minor edit', false, false, 'm' );
38
		$this->addOption( 'bot', 'Bot edit', false, false, 'b' );
39
		$this->addOption( 'autosummary', 'Enable autosummary', false, false, 'a' );
40
		$this->addOption( 'no-rc', 'Do not show the change in recent changes', false, false, 'r' );
41
		$this->addOption( 'nocreate', 'Don\'t create new pages', false, false );
42
		$this->addOption( 'createonly', 'Only create new pages', false, false );
43
		$this->addArg( 'title', 'Title of article to edit' );
44
	}
45
46
	public function execute() {
47
		global $wgUser;
48
49
		$userName = $this->getOption( 'user', false );
50
		$summary = $this->getOption( 'summary', '' );
51
		$minor = $this->hasOption( 'minor' );
52
		$bot = $this->hasOption( 'bot' );
53
		$autoSummary = $this->hasOption( 'autosummary' );
54
		$noRC = $this->hasOption( 'no-rc' );
55
56 View Code Duplication
		if ( $userName === false ) {
57
			$wgUser = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
58
		} else {
59
			$wgUser = User::newFromName( $userName );
60
		}
61
		if ( !$wgUser ) {
62
			$this->error( "Invalid username", true );
63
		}
64
		if ( $wgUser->isAnon() ) {
65
			$wgUser->addToDatabase();
66
		}
67
68
		$title = Title::newFromText( $this->getArg() );
69
		if ( !$title ) {
70
			$this->error( "Invalid title", true );
71
		}
72
73
		if ( $this->hasOption( 'nocreate' ) && !$title->exists() ) {
74
			$this->error( "Page does not exist", true );
75
		} elseif ( $this->hasOption( 'createonly' ) && $title->exists() ) {
76
			$this->error( "Page already exists", true );
77
		}
78
79
		$page = WikiPage::factory( $title );
0 ignored issues
show
Bug introduced by
It seems like $title 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...
80
81
		# Read the text
82
		$text = $this->getStdin( Maintenance::STDIN_ALL );
83
		$content = ContentHandler::makeContent( $text, $title );
0 ignored issues
show
Bug introduced by
It seems like $text defined by $this->getStdin(\Maintenance::STDIN_ALL) on line 82 can also be of type resource; however, ContentHandler::makeContent() does only seem to accept string, 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...
84
85
		# Do the edit
86
		$this->output( "Saving... " );
87
		$status = $page->doEditContent( $content, $summary,
88
			( $minor ? EDIT_MINOR : 0 ) |
89
			( $bot ? EDIT_FORCE_BOT : 0 ) |
90
			( $autoSummary ? EDIT_AUTOSUMMARY : 0 ) |
91
			( $noRC ? EDIT_SUPPRESS_RC : 0 ) );
92
		if ( $status->isOK() ) {
93
			$this->output( "done\n" );
94
			$exit = 0;
95
		} else {
96
			$this->output( "failed\n" );
97
			$exit = 1;
98
		}
99
		if ( !$status->isGood() ) {
100
			$this->output( $status->getWikiText( false, false, 'en' ) . "\n" );
101
		}
102
		exit( $exit );
0 ignored issues
show
Coding Style Compatibility introduced by
The method execute() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
103
	}
104
}
105
106
$maintClass = "EditCLI";
107
require_once RUN_MAINTENANCE_IF_MAIN;
108