Conditions | 15 |
Paths | 13 |
Total Lines | 101 |
Code Lines | 64 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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(); |
||
|
|||
57 | if ( $from === null ) { |
||
58 | throw new ErrorPageError( 'rollbackfailed', 'rollback-missingparam' ); |
||
59 | } |
||
60 | if ( !$rev ) { |
||
61 | throw new ErrorPageError( 'rollbackfailed', 'rollback-missingrevision' ); |
||
62 | } |
||
63 | if ( $from !== $rev->getUserText() ) { |
||
64 | throw new ErrorPageError( 'rollbackfailed', 'alreadyrolled', [ |
||
65 | $this->getTitle()->getPrefixedText(), |
||
66 | $from, |
||
67 | $rev->getUserText() |
||
68 | ] ); |
||
69 | } |
||
70 | |||
71 | $data = null; |
||
72 | $errors = $this->page->doRollback( |
||
73 | $from, |
||
74 | $request->getText( 'summary' ), |
||
75 | $request->getVal( 'token' ), |
||
76 | $request->getBool( 'bot' ), |
||
77 | $data, |
||
78 | $this->getUser() |
||
79 | ); |
||
80 | |||
81 | if ( in_array( [ 'actionthrottledtext' ], $errors ) ) { |
||
82 | throw new ThrottledError; |
||
83 | } |
||
84 | |||
85 | if ( isset( $errors[0][0] ) && |
||
86 | ( $errors[0][0] == 'alreadyrolled' || $errors[0][0] == 'cantrollback' ) |
||
87 | ) { |
||
88 | $this->getOutput()->setPageTitle( $this->msg( 'rollbackfailed' ) ); |
||
89 | $errArray = $errors[0]; |
||
90 | $errMsg = array_shift( $errArray ); |
||
91 | $this->getOutput()->addWikiMsgArray( $errMsg, $errArray ); |
||
92 | |||
93 | if ( isset( $data['current'] ) ) { |
||
94 | /** @var Revision $current */ |
||
95 | $current = $data['current']; |
||
96 | |||
97 | if ( $current->getComment() != '' ) { |
||
98 | $this->getOutput()->addHTML( $this->msg( 'editcomment' )->rawParams( |
||
99 | Linker::formatComment( $current->getComment() ) )->parse() ); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | return; |
||
104 | } |
||
105 | |||
106 | # NOTE: Permission errors already handled by Action::checkExecute. |
||
107 | if ( $errors == [ [ 'readonlytext' ] ] ) { |
||
108 | throw new ReadOnlyError; |
||
109 | } |
||
110 | |||
111 | # XXX: Would be nice if ErrorPageError could take multiple errors, and/or a status object. |
||
112 | # Right now, we only show the first error |
||
113 | foreach ( $errors as $error ) { |
||
114 | throw new ErrorPageError( 'rollbackfailed', $error[0], array_slice( $error, 1 ) ); |
||
115 | } |
||
116 | |||
117 | /** @var Revision $current */ |
||
118 | $current = $data['current']; |
||
119 | $target = $data['target']; |
||
120 | $newId = $data['newid']; |
||
121 | $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) ); |
||
122 | $this->getOutput()->setRobotPolicy( 'noindex,nofollow' ); |
||
123 | |||
124 | $old = Linker::revUserTools( $current ); |
||
125 | $new = Linker::revUserTools( $target ); |
||
126 | $this->getOutput()->addHTML( $this->msg( 'rollback-success' )->rawParams( $old, $new ) |
||
127 | ->parseAsBlock() ); |
||
128 | |||
129 | if ( $user->getBoolOption( 'watchrollback' ) ) { |
||
130 | $user->addWatch( $this->page->getTitle(), User::IGNORE_USER_RIGHTS ); |
||
131 | } |
||
132 | |||
133 | $this->getOutput()->returnToMain( false, $this->getTitle() ); |
||
134 | |||
135 | if ( !$request->getBool( 'hidediff', false ) && |
||
136 | !$this->getUser()->getBoolOption( 'norollbackdiff' ) |
||
137 | ) { |
||
138 | $contentHandler = $current->getContentHandler(); |
||
139 | $de = $contentHandler->createDifferenceEngine( |
||
140 | $this->getContext(), |
||
141 | $current->getId(), |
||
142 | $newId, |
||
143 | false, |
||
144 | true |
||
145 | ); |
||
146 | $de->showDiff( '', '' ); |
||
147 | } |
||
148 | return; |
||
149 | } |
||
150 | |||
159 |
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:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: