| Conditions | 23 |
| Paths | 96 |
| Total Lines | 143 |
| Code Lines | 100 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 61 | function formatValue( $name, $value ) { |
||
| 62 | static $msg = null; |
||
| 63 | if ( $msg === null ) { |
||
| 64 | $keys = [ |
||
| 65 | 'anononlyblock', |
||
| 66 | 'createaccountblock', |
||
| 67 | 'noautoblockblock', |
||
| 68 | 'emailblock', |
||
| 69 | 'blocklist-nousertalk', |
||
| 70 | 'unblocklink', |
||
| 71 | 'change-blocklink', |
||
| 72 | ]; |
||
| 73 | |||
| 74 | foreach ( $keys as $key ) { |
||
| 75 | $msg[$key] = $this->msg( $key )->escaped(); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | /** @var $row object */ |
||
| 80 | $row = $this->mCurrentRow; |
||
| 81 | |||
| 82 | $language = $this->getLanguage(); |
||
| 83 | |||
| 84 | $formatted = ''; |
||
| 85 | |||
| 86 | switch ( $name ) { |
||
| 87 | case 'ipb_timestamp': |
||
| 88 | $formatted = htmlspecialchars( $language->userTimeAndDate( $value, $this->getUser() ) ); |
||
| 89 | break; |
||
| 90 | |||
| 91 | case 'ipb_target': |
||
| 92 | if ( $row->ipb_auto ) { |
||
| 93 | $formatted = $this->msg( 'autoblockid', $row->ipb_id )->parse(); |
||
| 94 | } else { |
||
| 95 | list( $target, $type ) = Block::parseTarget( $row->ipb_address ); |
||
| 96 | switch ( $type ) { |
||
| 97 | case Block::TYPE_USER: |
||
| 98 | case Block::TYPE_IP: |
||
| 99 | $formatted = Linker::userLink( $target->getId(), $target ); |
||
| 100 | $formatted .= Linker::userToolLinks( |
||
| 101 | $target->getId(), |
||
| 102 | $target, |
||
| 103 | false, |
||
| 104 | Linker::TOOL_LINKS_NOBLOCK |
||
| 105 | ); |
||
| 106 | break; |
||
| 107 | case Block::TYPE_RANGE: |
||
| 108 | $formatted = htmlspecialchars( $target ); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | break; |
||
| 112 | |||
| 113 | case 'ipb_expiry': |
||
| 114 | $formatted = htmlspecialchars( $language->formatExpiry( |
||
| 115 | $value, |
||
| 116 | /* User preference timezone */true |
||
| 117 | ) ); |
||
| 118 | if ( $this->getUser()->isAllowed( 'block' ) ) { |
||
| 119 | if ( $row->ipb_auto ) { |
||
| 120 | $links[] = Linker::linkKnown( |
||
|
|
|||
| 121 | SpecialPage::getTitleFor( 'Unblock' ), |
||
| 122 | $msg['unblocklink'], |
||
| 123 | [], |
||
| 124 | [ 'wpTarget' => "#{$row->ipb_id}" ] |
||
| 125 | ); |
||
| 126 | } else { |
||
| 127 | $links[] = Linker::linkKnown( |
||
| 128 | SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ), |
||
| 129 | $msg['unblocklink'] |
||
| 130 | ); |
||
| 131 | $links[] = Linker::linkKnown( |
||
| 132 | SpecialPage::getTitleFor( 'Block', $row->ipb_address ), |
||
| 133 | $msg['change-blocklink'] |
||
| 134 | ); |
||
| 135 | } |
||
| 136 | $formatted .= ' ' . Html::rawElement( |
||
| 137 | 'span', |
||
| 138 | [ 'class' => 'mw-blocklist-actions' ], |
||
| 139 | $this->msg( 'parentheses' )->rawParams( |
||
| 140 | $language->pipeList( $links ) )->escaped() |
||
| 141 | ); |
||
| 142 | } |
||
| 143 | if ( $value !== 'infinity' ) { |
||
| 144 | $timestamp = new MWTimestamp( $value ); |
||
| 145 | $formatted .= '<br />' . $this->msg( |
||
| 146 | 'ipb-blocklist-duration-left', |
||
| 147 | $language->formatDuration( |
||
| 148 | $timestamp->getTimestamp() - time(), |
||
| 149 | // reasonable output |
||
| 150 | [ |
||
| 151 | 'minutes', |
||
| 152 | 'hours', |
||
| 153 | 'days', |
||
| 154 | 'years', |
||
| 155 | ] |
||
| 156 | ) |
||
| 157 | )->escaped(); |
||
| 158 | } |
||
| 159 | break; |
||
| 160 | |||
| 161 | case 'ipb_by': |
||
| 162 | if ( isset( $row->by_user_name ) ) { |
||
| 163 | $formatted = Linker::userLink( $value, $row->by_user_name ); |
||
| 164 | $formatted .= Linker::userToolLinks( $value, $row->by_user_name ); |
||
| 165 | } else { |
||
| 166 | $formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user? |
||
| 167 | } |
||
| 168 | break; |
||
| 169 | |||
| 170 | case 'ipb_reason': |
||
| 171 | $formatted = Linker::formatComment( $value ); |
||
| 172 | break; |
||
| 173 | |||
| 174 | case 'ipb_params': |
||
| 175 | $properties = []; |
||
| 176 | if ( $row->ipb_anon_only ) { |
||
| 177 | $properties[] = $msg['anononlyblock']; |
||
| 178 | } |
||
| 179 | if ( $row->ipb_create_account ) { |
||
| 180 | $properties[] = $msg['createaccountblock']; |
||
| 181 | } |
||
| 182 | if ( $row->ipb_user && !$row->ipb_enable_autoblock ) { |
||
| 183 | $properties[] = $msg['noautoblockblock']; |
||
| 184 | } |
||
| 185 | |||
| 186 | if ( $row->ipb_block_email ) { |
||
| 187 | $properties[] = $msg['emailblock']; |
||
| 188 | } |
||
| 189 | |||
| 190 | if ( !$row->ipb_allow_usertalk ) { |
||
| 191 | $properties[] = $msg['blocklist-nousertalk']; |
||
| 192 | } |
||
| 193 | |||
| 194 | $formatted = $language->commaList( $properties ); |
||
| 195 | break; |
||
| 196 | |||
| 197 | default: |
||
| 198 | $formatted = "Unable to format $name"; |
||
| 199 | break; |
||
| 200 | } |
||
| 201 | |||
| 202 | return $formatted; |
||
| 203 | } |
||
| 204 | |||
| 283 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.