| Conditions | 15 |
| Paths | 21 |
| Total Lines | 63 |
| Code Lines | 37 |
| 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 |
||
| 148 | private static function extractPageRecord( $externalData, $pageTitle ) { |
||
| 149 | // If there is a special case with only one returned page |
||
| 150 | // we can cheat, and only return |
||
| 151 | // the single page in the "pages" substructure. |
||
| 152 | if ( isset( $externalData['query']['pages'] ) ) { |
||
| 153 | $pages = array_values( $externalData['query']['pages'] ); |
||
| 154 | if ( count( $pages ) === 1 ) { |
||
| 155 | return $pages[0]; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | // This is only used during internal testing, as it is assumed |
||
| 159 | // a more optimal (and lossfree) storage. |
||
| 160 | // Make initial checks and return if prerequisites are not meet. |
||
| 161 | if ( !is_array( $externalData ) || !isset( $externalData['query'] ) ) { |
||
| 162 | return false; |
||
| 163 | } |
||
| 164 | // Loop over the tree different named structures, that otherwise are similar |
||
| 165 | $structs = [ |
||
| 166 | 'normalized' => 'from', |
||
| 167 | 'converted' => 'from', |
||
| 168 | 'redirects' => 'from', |
||
| 169 | 'pages' => 'title' |
||
| 170 | ]; |
||
| 171 | foreach ( $structs as $listId => $fieldId ) { |
||
| 172 | // Check if the substructure exist at all. |
||
| 173 | if ( !isset( $externalData['query'][$listId] ) ) { |
||
| 174 | continue; |
||
| 175 | } |
||
| 176 | // Filter the substructure down to what we actually are using. |
||
| 177 | $collectedHits = array_filter( |
||
| 178 | array_values( $externalData['query'][$listId] ), |
||
| 179 | function ( $a ) use ( $fieldId, $pageTitle ) { |
||
| 180 | return $a[$fieldId] === $pageTitle; |
||
| 181 | } |
||
| 182 | ); |
||
| 183 | // If still looping over normalization, conversion or redirects, |
||
| 184 | // then we need to keep the new page title for later rounds. |
||
| 185 | if ( $fieldId === 'from' && is_array( $collectedHits ) ) { |
||
| 186 | switch ( count( $collectedHits ) ) { |
||
| 187 | case 0: |
||
| 188 | break; |
||
| 189 | case 1: |
||
| 190 | $pageTitle = $collectedHits[0]['to']; |
||
| 191 | break; |
||
| 192 | default: |
||
| 193 | return false; |
||
| 194 | } |
||
| 195 | } elseif ( $fieldId === 'title' && is_array( $collectedHits ) ) { |
||
| 196 | // If on the pages structure we should prepare for returning. |
||
| 197 | |||
| 198 | switch ( count( $collectedHits ) ) { |
||
| 199 | case 0: |
||
| 200 | return false; |
||
| 201 | case 1: |
||
| 202 | return array_shift( $collectedHits ); |
||
| 203 | default: |
||
| 204 | return false; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | } |
||
| 208 | // should never be here |
||
| 209 | return false; |
||
| 210 | } |
||
| 211 | |||
| 213 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.