Completed
Branch master (227f0c)
by
unknown
30:54
created

RollbackAction::onView()   D

Complexity

Conditions 15
Paths 12

Size

Total Lines 98
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 15
eloc 62
c 2
b 0
f 1
nc 12
nop 0
dl 0
loc 98
rs 4.9121

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Edit rollback user interface
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
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18
 *
19
 * @file
20
 * @ingroup Actions
21
 */
22
23
/**
24
 * User interface for the rollback action
25
 *
26
 * @ingroup Actions
27
 */
28
class RollbackAction extends FormlessAction {
29
30
	public function getName() {
31
		return 'rollback';
32
	}
33
34
	public function getRestriction() {
35
		return 'rollback';
36
	}
37
38
	/**
39
	 * Temporarily unused message keys due to T88044/T136375:
40
	 * - confirm-rollback-top
41
	 * - confirm-rollback-button
42
	 * - rollbackfailed
43
	 * - rollback-missingparam
44
	 */
45
46
	/**
47
	 * @throws ErrorPageError
48
	 */
49
	public function onView() {
50
		// TODO: use $this->useTransactionalTimeLimit(); when POST only
51
		wfTransactionalTimeLimit();
52
53
		$request = $this->getRequest();
54
		$user = $this->getUser();
55
		$from = $request->getVal( 'from' );
56
		$rev = $this->page->getRevision();
0 ignored issues
show
Bug introduced by
The method getRevision does only exist in Article and CategoryPage... ImagePage and WikiPage, but not in Page.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
57
		if ( $from === null || $from === '' ) {
58
			throw new ErrorPageError( 'rollbackfailed', 'rollback-missingparam' );
59
		}
60
		if ( $from !== $rev->getUserText() ) {
61
			throw new ErrorPageError( 'rollbackfailed', 'alreadyrolled', [
62
				$this->getTitle()->getPrefixedText(),
63
				$from,
64
				$rev->getUserText()
65
			] );
66
		}
67
68
		$data = null;
69
		$errors = $this->page->doRollback(
0 ignored issues
show
Bug introduced by
The method doRollback does only exist in Article and CategoryPage... ImagePage and WikiPage, but not in Page.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
70
			$from,
71
			$request->getText( 'summary' ),
72
			$request->getVal( 'token' ),
73
			$request->getBool( 'bot' ),
74
			$data,
75
			$this->getUser()
76
		);
77
78
		if ( in_array( [ 'actionthrottledtext' ], $errors ) ) {
79
			throw new ThrottledError;
80
		}
81
82
		if ( isset( $errors[0][0] ) &&
83
			( $errors[0][0] == 'alreadyrolled' || $errors[0][0] == 'cantrollback' )
84
		) {
85
			$this->getOutput()->setPageTitle( $this->msg( 'rollbackfailed' ) );
86
			$errArray = $errors[0];
87
			$errMsg = array_shift( $errArray );
88
			$this->getOutput()->addWikiMsgArray( $errMsg, $errArray );
89
90
			if ( isset( $data['current'] ) ) {
91
				/** @var Revision $current */
92
				$current = $data['current'];
93
94
				if ( $current->getComment() != '' ) {
95
					$this->getOutput()->addHTML( $this->msg( 'editcomment' )->rawParams(
96
						Linker::formatComment( $current->getComment() ) )->parse() );
97
				}
98
			}
99
100
			return;
101
		}
102
103
		# NOTE: Permission errors already handled by Action::checkExecute.
104
		if ( $errors == [ [ 'readonlytext' ] ] ) {
105
			throw new ReadOnlyError;
106
		}
107
108
		# XXX: Would be nice if ErrorPageError could take multiple errors, and/or a status object.
109
		#      Right now, we only show the first error
110
		foreach ( $errors as $error ) {
111
			throw new ErrorPageError( 'rollbackfailed', $error[0], array_slice( $error, 1 ) );
112
		}
113
114
		/** @var Revision $current */
115
		$current = $data['current'];
116
		$target = $data['target'];
117
		$newId = $data['newid'];
118
		$this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
119
		$this->getOutput()->setRobotPolicy( 'noindex,nofollow' );
120
121
		$old = Linker::revUserTools( $current );
122
		$new = Linker::revUserTools( $target );
123
		$this->getOutput()->addHTML( $this->msg( 'rollback-success' )->rawParams( $old, $new )
124
			->parseAsBlock() );
125
126
		if ( $user->getBoolOption( 'watchrollback' ) ) {
127
			$user->addWatch( $this->page->getTitle(), User::IGNORE_USER_RIGHTS );
0 ignored issues
show
Bug introduced by
The method getTitle does only exist in Article and CategoryPage... ImagePage and WikiPage, but not in Page.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
128
		}
129
130
		$this->getOutput()->returnToMain( false, $this->getTitle() );
131
132
		if ( !$request->getBool( 'hidediff', false ) &&
133
			!$this->getUser()->getBoolOption( 'norollbackdiff' )
134
		) {
135
			$contentHandler = $current->getContentHandler();
136
			$de = $contentHandler->createDifferenceEngine(
137
				$this->getContext(),
138
				$current->getId(),
139
				$newId,
140
				false,
141
				true
142
			);
143
			$de->showDiff( '', '' );
144
		}
145
		return;
146
	}
147
148
	protected function getDescription() {
149
		return '';
150
	}
151
152
	public function doesWrites() {
153
		return true;
154
	}
155
}
156