Conditions | 16 |
Paths | 1368 |
Total Lines | 112 |
Code Lines | 73 |
Lines | 112 |
Ratio | 100 % |
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 |
||
50 | public function run( $resultPageSet = null ) { |
||
51 | $params = $this->extractRequestParams(); |
||
52 | |||
53 | if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) { |
||
54 | $this->dieUsageMsg( [ 'missingparam', 'lang' ] ); |
||
55 | } |
||
56 | |||
57 | if ( !is_null( $params['continue'] ) ) { |
||
58 | $cont = explode( '|', $params['continue'] ); |
||
59 | $this->dieContinueUsageIf( count( $cont ) != 3 ); |
||
60 | |||
61 | $db = $this->getDB(); |
||
62 | $op = $params['dir'] == 'descending' ? '<' : '>'; |
||
63 | $prefix = $db->addQuotes( $cont[0] ); |
||
64 | $title = $db->addQuotes( $cont[1] ); |
||
65 | $from = intval( $cont[2] ); |
||
66 | $this->addWhere( |
||
67 | "ll_lang $op $prefix OR " . |
||
68 | "(ll_lang = $prefix AND " . |
||
69 | "(ll_title $op $title OR " . |
||
70 | "(ll_title = $title AND " . |
||
71 | "ll_from $op= $from)))" |
||
72 | ); |
||
73 | } |
||
74 | |||
75 | $prop = array_flip( $params['prop'] ); |
||
76 | $lllang = isset( $prop['lllang'] ); |
||
77 | $lltitle = isset( $prop['lltitle'] ); |
||
78 | |||
79 | $this->addTables( [ 'langlinks', 'page' ] ); |
||
80 | $this->addWhere( 'll_from = page_id' ); |
||
81 | |||
82 | $this->addFields( [ 'page_id', 'page_title', 'page_namespace', 'page_is_redirect', |
||
83 | 'll_from', 'll_lang', 'll_title' ] ); |
||
84 | |||
85 | $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' ); |
||
86 | if ( isset( $params['lang'] ) ) { |
||
87 | $this->addWhereFld( 'll_lang', $params['lang'] ); |
||
88 | if ( isset( $params['title'] ) ) { |
||
89 | $this->addWhereFld( 'll_title', $params['title'] ); |
||
90 | $this->addOption( 'ORDER BY', 'll_from' . $sort ); |
||
91 | } else { |
||
92 | $this->addOption( 'ORDER BY', [ |
||
93 | 'll_title' . $sort, |
||
94 | 'll_from' . $sort |
||
95 | ] ); |
||
96 | } |
||
97 | } else { |
||
98 | $this->addOption( 'ORDER BY', [ |
||
99 | 'll_lang' . $sort, |
||
100 | 'll_title' . $sort, |
||
101 | 'll_from' . $sort |
||
102 | ] ); |
||
103 | } |
||
104 | |||
105 | $this->addOption( 'LIMIT', $params['limit'] + 1 ); |
||
106 | |||
107 | $res = $this->select( __METHOD__ ); |
||
108 | |||
109 | $pages = []; |
||
110 | |||
111 | $count = 0; |
||
112 | $result = $this->getResult(); |
||
113 | foreach ( $res as $row ) { |
||
114 | if ( ++$count > $params['limit'] ) { |
||
115 | // We've reached the one extra which shows that there are |
||
116 | // additional pages to be had. Stop here... Continue string |
||
117 | // preserved in case the redirect query doesn't pass the limit. |
||
118 | $this->setContinueEnumParameter( |
||
119 | 'continue', |
||
120 | "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}" |
||
121 | ); |
||
122 | break; |
||
123 | } |
||
124 | |||
125 | if ( !is_null( $resultPageSet ) ) { |
||
126 | $pages[] = Title::newFromRow( $row ); |
||
127 | } else { |
||
128 | $entry = [ 'pageid' => $row->page_id ]; |
||
129 | |||
130 | $title = Title::makeTitle( $row->page_namespace, $row->page_title ); |
||
131 | ApiQueryBase::addTitleInfo( $entry, $title ); |
||
132 | |||
133 | if ( $row->page_is_redirect ) { |
||
134 | $entry['redirect'] = true; |
||
135 | } |
||
136 | |||
137 | if ( $lllang ) { |
||
138 | $entry['lllang'] = $row->ll_lang; |
||
139 | } |
||
140 | |||
141 | if ( $lltitle ) { |
||
142 | $entry['lltitle'] = $row->ll_title; |
||
143 | } |
||
144 | |||
145 | $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $entry ); |
||
146 | if ( !$fit ) { |
||
147 | $this->setContinueEnumParameter( |
||
148 | 'continue', |
||
149 | "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}" |
||
150 | ); |
||
151 | break; |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | |||
156 | if ( is_null( $resultPageSet ) ) { |
||
157 | $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'll' ); |
||
158 | } else { |
||
159 | $resultPageSet->populateFromTitles( $pages ); |
||
160 | } |
||
161 | } |
||
162 | |||
213 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.