Conditions | 16 |
Paths | 864 |
Total Lines | 99 |
Code Lines | 64 |
Lines | 15 |
Ratio | 15.15 % |
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 |
||
52 | private function run( $resultPageSet = null ) { |
||
53 | $params = $this->extractRequestParams(); |
||
54 | |||
55 | $query = $params['query']; |
||
56 | $protocol = self::getProtocolPrefix( $params['protocol'] ); |
||
57 | |||
58 | $this->addTables( [ 'page', 'externallinks' ] ); // must be in this order for 'USE INDEX' |
||
59 | $this->addOption( 'USE INDEX', 'el_index' ); |
||
60 | $this->addWhere( 'page_id=el_from' ); |
||
61 | |||
62 | $miser_ns = []; |
||
63 | View Code Duplication | if ( $this->getConfig()->get( 'MiserMode' ) ) { |
|
64 | $miser_ns = $params['namespace']; |
||
65 | } else { |
||
66 | $this->addWhereFld( 'page_namespace', $params['namespace'] ); |
||
67 | } |
||
68 | |||
69 | // Normalize query to match the normalization applied for the externallinks table |
||
70 | $query = Parser::normalizeLinkUrl( $query ); |
||
71 | |||
72 | $whereQuery = $this->prepareUrlQuerySearchString( $query, $protocol ); |
||
73 | |||
74 | if ( $whereQuery !== null ) { |
||
75 | $this->addWhere( $whereQuery ); |
||
76 | } |
||
77 | |||
78 | $prop = array_flip( $params['prop'] ); |
||
79 | $fld_ids = isset( $prop['ids'] ); |
||
80 | $fld_title = isset( $prop['title'] ); |
||
81 | $fld_url = isset( $prop['url'] ); |
||
82 | |||
83 | View Code Duplication | if ( is_null( $resultPageSet ) ) { |
|
84 | $this->addFields( [ |
||
85 | 'page_id', |
||
86 | 'page_namespace', |
||
87 | 'page_title' |
||
88 | ] ); |
||
89 | $this->addFieldsIf( 'el_to', $fld_url ); |
||
90 | } else { |
||
91 | $this->addFields( $resultPageSet->getPageTableFields() ); |
||
92 | } |
||
93 | |||
94 | $limit = $params['limit']; |
||
95 | $offset = $params['offset']; |
||
96 | $this->addOption( 'LIMIT', $limit + 1 ); |
||
97 | if ( isset( $offset ) ) { |
||
98 | $this->addOption( 'OFFSET', $offset ); |
||
99 | } |
||
100 | |||
101 | $res = $this->select( __METHOD__ ); |
||
102 | |||
103 | $result = $this->getResult(); |
||
104 | $count = 0; |
||
105 | foreach ( $res as $row ) { |
||
106 | if ( ++$count > $limit ) { |
||
107 | // We've reached the one extra which shows that there are |
||
108 | // additional pages to be had. Stop here... |
||
109 | $this->setContinueEnumParameter( 'offset', $offset + $limit ); |
||
110 | break; |
||
111 | } |
||
112 | |||
113 | if ( count( $miser_ns ) && !in_array( $row->page_namespace, $miser_ns ) ) { |
||
114 | continue; |
||
115 | } |
||
116 | |||
117 | if ( is_null( $resultPageSet ) ) { |
||
118 | $vals = [ |
||
119 | ApiResult::META_TYPE => 'assoc', |
||
120 | ]; |
||
121 | if ( $fld_ids ) { |
||
122 | $vals['pageid'] = intval( $row->page_id ); |
||
123 | } |
||
124 | if ( $fld_title ) { |
||
125 | $title = Title::makeTitle( $row->page_namespace, $row->page_title ); |
||
126 | ApiQueryBase::addTitleInfo( $vals, $title ); |
||
127 | } |
||
128 | if ( $fld_url ) { |
||
129 | $to = $row->el_to; |
||
130 | // expand protocol-relative urls |
||
131 | if ( $params['expandurl'] ) { |
||
132 | $to = wfExpandUrl( $to, PROTO_CANONICAL ); |
||
133 | } |
||
134 | $vals['url'] = $to; |
||
135 | } |
||
136 | $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals ); |
||
137 | if ( !$fit ) { |
||
138 | $this->setContinueEnumParameter( 'offset', $offset + $count - 1 ); |
||
139 | break; |
||
140 | } |
||
141 | } else { |
||
142 | $resultPageSet->processDbRow( $row ); |
||
|
|||
143 | } |
||
144 | } |
||
145 | |||
146 | if ( is_null( $resultPageSet ) ) { |
||
147 | $result->addIndexedTagName( [ 'query', $this->getModuleName() ], |
||
148 | $this->getModulePrefix() ); |
||
149 | } |
||
150 | } |
||
151 | |||
236 |
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: