| Conditions | 7 |
| Paths | 7 |
| Total Lines | 68 |
| Code Lines | 33 |
| 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 |
||
| 71 | public function normalizePageName( $pageName, $apiUrl ) { |
||
| 72 | |||
| 73 | // Check if we have strings as arguments. |
||
| 74 | if ( !is_string( $pageName ) ) { |
||
| 75 | throw new \MWException( '$pageName must be a string' ); |
||
| 76 | } |
||
| 77 | |||
| 78 | // Go on call the external site |
||
| 79 | |||
| 80 | // Make sure the string is normalized into NFC (due to T42017) |
||
| 81 | // but do nothing to the whitespaces, that should work appropriately. |
||
| 82 | // @see https://phabricator.wikimedia.org/T42017 |
||
| 83 | $pageName = Validator::cleanUp( $pageName ); |
||
| 84 | |||
| 85 | // Build the args for the specific call |
||
| 86 | $args = [ |
||
| 87 | 'action' => 'query', |
||
| 88 | 'prop' => 'info', |
||
| 89 | 'redirects' => true, |
||
| 90 | 'converttitles' => true, |
||
| 91 | 'format' => 'json', |
||
| 92 | 'titles' => $pageName, |
||
| 93 | // @todo options for maxlag and maxage |
||
| 94 | // Note that maxlag will lead to a long delay before a reply is made, |
||
| 95 | // but that maxage can avoid the extreme delay. On the other hand |
||
| 96 | // maxage could be nice to use anyhow as it stops unnecessary requests. |
||
| 97 | // Also consider smaxage if maxage is used. |
||
| 98 | ]; |
||
| 99 | |||
| 100 | $url = wfAppendQuery( $apiUrl, $args ); |
||
| 101 | |||
| 102 | // Go on call the external site |
||
| 103 | // @todo we need a good way to specify a timeout here. |
||
| 104 | $ret = $this->http->get( $url, [], __METHOD__ ); |
||
| 105 | |||
| 106 | if ( $ret === false ) { |
||
| 107 | wfDebugLog( "MediaWikiSite", "call to external site failed: $url" ); |
||
| 108 | return false; |
||
| 109 | } |
||
| 110 | |||
| 111 | $data = FormatJson::decode( $ret, true ); |
||
|
|
|||
| 112 | |||
| 113 | if ( !is_array( $data ) ) { |
||
| 114 | wfDebugLog( "MediaWikiSite", "call to <$url> returned bad json: " . $ret ); |
||
| 115 | return false; |
||
| 116 | } |
||
| 117 | |||
| 118 | $page = static::extractPageRecord( $data, $pageName ); |
||
| 119 | |||
| 120 | if ( isset( $page['missing'] ) ) { |
||
| 121 | wfDebugLog( "MediaWikiSite", "call to <$url> returned a marker for a missing page title! " |
||
| 122 | . $ret ); |
||
| 123 | return false; |
||
| 124 | } |
||
| 125 | |||
| 126 | if ( isset( $page['invalid'] ) ) { |
||
| 127 | wfDebugLog( "MediaWikiSite", "call to <$url> returned a marker for an invalid page title! " |
||
| 128 | . $ret ); |
||
| 129 | return false; |
||
| 130 | } |
||
| 131 | |||
| 132 | if ( !isset( $page['title'] ) ) { |
||
| 133 | wfDebugLog( "MediaWikiSite", "call to <$url> did not return a page title! " . $ret ); |
||
| 134 | return false; |
||
| 135 | } |
||
| 136 | |||
| 137 | return $page['title']; |
||
| 138 | } |
||
| 139 | |||
| 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.