Conditions | 10 |
Paths | 20 |
Total Lines | 64 |
Code Lines | 41 |
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 |
||
35 | public function execute() { |
||
36 | $user = $this->getUser(); |
||
37 | if ( !$user->isLoggedIn() ) { |
||
38 | $this->dieUsage( 'You must be logged-in to have a watchlist', 'notloggedin' ); |
||
39 | } |
||
40 | |||
41 | if ( !$user->isAllowed( 'editmywatchlist' ) ) { |
||
42 | $this->dieUsage( 'You don\'t have permission to edit your watchlist', 'permissiondenied' ); |
||
43 | } |
||
44 | |||
45 | $params = $this->extractRequestParams(); |
||
46 | |||
47 | $continuationManager = new ApiContinuationManager( $this, [], [] ); |
||
48 | $this->setContinuationManager( $continuationManager ); |
||
49 | |||
50 | $pageSet = $this->getPageSet(); |
||
51 | // by default we use pageset to extract the page to work on. |
||
52 | // title is still supported for backward compatibility |
||
53 | if ( !isset( $params['title'] ) ) { |
||
54 | $pageSet->execute(); |
||
55 | $res = $pageSet->getInvalidTitlesAndRevisions( [ |
||
56 | 'invalidTitles', |
||
57 | 'special', |
||
58 | 'missingIds', |
||
59 | 'missingRevIds', |
||
60 | 'interwikiTitles' |
||
61 | ] ); |
||
62 | |||
63 | foreach ( $pageSet->getMissingTitles() as $title ) { |
||
64 | $r = $this->watchTitle( $title, $user, $params ); |
||
65 | $r['missing'] = 1; |
||
66 | $res[] = $r; |
||
67 | } |
||
68 | |||
69 | foreach ( $pageSet->getGoodTitles() as $title ) { |
||
70 | $r = $this->watchTitle( $title, $user, $params ); |
||
71 | $res[] = $r; |
||
72 | } |
||
73 | ApiResult::setIndexedTagName( $res, 'w' ); |
||
74 | } else { |
||
75 | // dont allow use of old title parameter with new pageset parameters. |
||
76 | $extraParams = array_keys( array_filter( $pageSet->extractRequestParams(), function ( $x ) { |
||
77 | return $x !== null && $x !== false; |
||
78 | } ) ); |
||
79 | |||
80 | if ( $extraParams ) { |
||
81 | $p = $this->getModulePrefix(); |
||
82 | $this->dieUsage( |
||
83 | "The parameter {$p}title can not be used with " . implode( ', ', $extraParams ), |
||
84 | 'invalidparammix' |
||
85 | ); |
||
86 | } |
||
87 | |||
88 | $title = Title::newFromText( $params['title'] ); |
||
89 | if ( !$title || !$title->isWatchable() ) { |
||
90 | $this->dieUsageMsg( [ 'invalidtitle', $params['title'] ] ); |
||
91 | } |
||
92 | $res = $this->watchTitle( $title, $user, $params, true ); |
||
|
|||
93 | } |
||
94 | $this->getResult()->addValue( null, $this->getModuleName(), $res ); |
||
95 | |||
96 | $this->setContinuationManager( null ); |
||
97 | $continuationManager->setContinuationIntoResult( $this->getResult() ); |
||
98 | } |
||
99 | |||
194 |
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: