Conditions | 23 |
Paths | > 20000 |
Total Lines | 110 |
Code Lines | 72 |
Lines | 6 |
Ratio | 5.45 % |
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 |
||
31 | public function execute() { |
||
32 | global $wgContLang; |
||
33 | |||
34 | $params = $this->extractRequestParams(); |
||
35 | |||
36 | $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' ); |
||
37 | $titleObj = $pageObj->getTitle(); |
||
38 | |||
39 | $errors = $titleObj->getUserPermissionsErrors( 'protect', $this->getUser() ); |
||
40 | if ( $errors ) { |
||
41 | // We don't care about multiple errors, just report one of them |
||
42 | $this->dieUsageMsg( reset( $errors ) ); |
||
43 | } |
||
44 | |||
45 | $user = $this->getUser(); |
||
46 | $tags = $params['tags']; |
||
47 | |||
48 | // Check if user can add tags |
||
49 | View Code Duplication | if ( !is_null( $tags ) ) { |
|
50 | $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $tags, $user ); |
||
51 | if ( !$ableToTag->isOK() ) { |
||
52 | $this->dieStatus( $ableToTag ); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | $expiry = (array)$params['expiry']; |
||
57 | if ( count( $expiry ) != count( $params['protections'] ) ) { |
||
58 | if ( count( $expiry ) == 1 ) { |
||
59 | $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] ); |
||
60 | } else { |
||
61 | $this->dieUsageMsg( [ |
||
62 | 'toofewexpiries', |
||
63 | count( $expiry ), |
||
64 | count( $params['protections'] ) |
||
65 | ] ); |
||
66 | } |
||
67 | } |
||
68 | |||
69 | $restrictionTypes = $titleObj->getRestrictionTypes(); |
||
70 | |||
71 | $protections = []; |
||
72 | $expiryarray = []; |
||
73 | $resultProtections = []; |
||
74 | foreach ( $params['protections'] as $i => $prot ) { |
||
75 | $p = explode( '=', $prot ); |
||
76 | $protections[$p[0]] = ( $p[1] == 'all' ? '' : $p[1] ); |
||
77 | |||
78 | if ( $titleObj->exists() && $p[0] == 'create' ) { |
||
79 | $this->dieUsageMsg( 'create-titleexists' ); |
||
80 | } |
||
81 | if ( !$titleObj->exists() && $p[0] != 'create' ) { |
||
82 | $this->dieUsageMsg( 'missingtitle-createonly' ); |
||
83 | } |
||
84 | |||
85 | if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) { |
||
86 | $this->dieUsageMsg( [ 'protect-invalidaction', $p[0] ] ); |
||
87 | } |
||
88 | if ( !in_array( $p[1], $this->getConfig()->get( 'RestrictionLevels' ) ) && $p[1] != 'all' ) { |
||
89 | $this->dieUsageMsg( [ 'protect-invalidlevel', $p[1] ] ); |
||
90 | } |
||
91 | |||
92 | if ( wfIsInfinity( $expiry[$i] ) ) { |
||
93 | $expiryarray[$p[0]] = 'infinity'; |
||
94 | } else { |
||
95 | $exp = strtotime( $expiry[$i] ); |
||
96 | if ( $exp < 0 || !$exp ) { |
||
97 | $this->dieUsageMsg( [ 'invalidexpiry', $expiry[$i] ] ); |
||
98 | } |
||
99 | |||
100 | $exp = wfTimestamp( TS_MW, $exp ); |
||
101 | if ( $exp < wfTimestampNow() ) { |
||
102 | $this->dieUsageMsg( [ 'pastexpiry', $expiry[$i] ] ); |
||
103 | } |
||
104 | $expiryarray[$p[0]] = $exp; |
||
105 | } |
||
106 | $resultProtections[] = [ |
||
107 | $p[0] => $protections[$p[0]], |
||
108 | 'expiry' => $wgContLang->formatExpiry( $expiryarray[$p[0]], TS_ISO_8601, 'infinite' ), |
||
109 | ]; |
||
110 | } |
||
111 | |||
112 | $cascade = $params['cascade']; |
||
113 | |||
114 | $watch = $params['watch'] ? 'watch' : $params['watchlist']; |
||
115 | $this->setWatch( $watch, $titleObj, 'watchdefault' ); |
||
116 | |||
117 | $status = $pageObj->doUpdateRestrictions( |
||
118 | $protections, |
||
119 | $expiryarray, |
||
120 | $cascade, |
||
121 | $params['reason'], |
||
122 | $user, |
||
123 | $tags |
||
124 | ); |
||
125 | |||
126 | if ( !$status->isOK() ) { |
||
127 | $this->dieStatus( $status ); |
||
128 | } |
||
129 | $res = [ |
||
130 | 'title' => $titleObj->getPrefixedText(), |
||
131 | 'reason' => $params['reason'] |
||
132 | ]; |
||
133 | if ( $cascade ) { |
||
134 | $res['cascade'] = true; |
||
135 | } |
||
136 | $res['protections'] = $resultProtections; |
||
137 | $result = $this->getResult(); |
||
138 | ApiResult::setIndexedTagName( $res['protections'], 'protection' ); |
||
139 | $result->addValue( null, $this->getModuleName(), $res ); |
||
140 | } |
||
141 | |||
211 |