| Conditions | 13 |
| Paths | 293 |
| Total Lines | 80 |
| Code Lines | 50 |
| Lines | 5 |
| Ratio | 6.25 % |
| 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 |
||
| 174 | public function saveSites( array $sites ) { |
||
| 175 | if ( empty( $sites ) ) { |
||
| 176 | return true; |
||
| 177 | } |
||
| 178 | |||
| 179 | $dbw = $this->dbLoadBalancer->getConnection( DB_MASTER ); |
||
| 180 | |||
| 181 | $dbw->startAtomic( __METHOD__ ); |
||
| 182 | |||
| 183 | $success = true; |
||
| 184 | |||
| 185 | $internalIds = []; |
||
| 186 | $localIds = []; |
||
| 187 | |||
| 188 | foreach ( $sites as $site ) { |
||
| 189 | if ( $site->getInternalId() !== null ) { |
||
| 190 | $internalIds[] = $site->getInternalId(); |
||
| 191 | } |
||
| 192 | |||
| 193 | $fields = [ |
||
| 194 | // Site data |
||
| 195 | 'site_global_key' => $site->getGlobalId(), // TODO: check not null |
||
| 196 | 'site_type' => $site->getType(), |
||
| 197 | 'site_group' => $site->getGroup(), |
||
| 198 | 'site_source' => $site->getSource(), |
||
| 199 | 'site_language' => $site->getLanguageCode() === null ? '' : $site->getLanguageCode(), |
||
| 200 | 'site_protocol' => $site->getProtocol(), |
||
| 201 | 'site_domain' => strrev( $site->getDomain() ) . '.', |
||
| 202 | 'site_data' => serialize( $site->getExtraData() ), |
||
| 203 | |||
| 204 | // Site config |
||
| 205 | 'site_forward' => $site->shouldForward() ? 1 : 0, |
||
| 206 | 'site_config' => serialize( $site->getExtraConfig() ), |
||
| 207 | ]; |
||
| 208 | |||
| 209 | $rowId = $site->getInternalId(); |
||
| 210 | if ( $rowId !== null ) { |
||
| 211 | $success = $dbw->update( |
||
| 212 | 'sites', $fields, [ 'site_id' => $rowId ], __METHOD__ |
||
| 213 | ) && $success; |
||
| 214 | } else { |
||
| 215 | $rowId = $dbw->nextSequenceValue( 'sites_site_id_seq' ); |
||
| 216 | $fields['site_id'] = $rowId; |
||
| 217 | $success = $dbw->insert( 'sites', $fields, __METHOD__ ) && $success; |
||
| 218 | $rowId = $dbw->insertId(); |
||
| 219 | } |
||
| 220 | |||
| 221 | View Code Duplication | foreach ( $site->getLocalIds() as $idType => $ids ) { |
|
| 222 | foreach ( $ids as $id ) { |
||
| 223 | $localIds[] = [ $rowId, $idType, $id ]; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | if ( $internalIds !== [] ) { |
||
| 229 | $dbw->delete( |
||
| 230 | 'site_identifiers', |
||
| 231 | [ 'si_site' => $internalIds ], |
||
| 232 | __METHOD__ |
||
| 233 | ); |
||
| 234 | } |
||
| 235 | |||
| 236 | foreach ( $localIds as $localId ) { |
||
| 237 | $dbw->insert( |
||
| 238 | 'site_identifiers', |
||
| 239 | [ |
||
| 240 | 'si_site' => $localId[0], |
||
| 241 | 'si_type' => $localId[1], |
||
| 242 | 'si_key' => $localId[2], |
||
| 243 | ], |
||
| 244 | __METHOD__ |
||
| 245 | ); |
||
| 246 | } |
||
| 247 | |||
| 248 | $dbw->endAtomic( __METHOD__ ); |
||
| 249 | |||
| 250 | $this->reset(); |
||
| 251 | |||
| 252 | return $success; |
||
| 253 | } |
||
| 254 | |||
| 285 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: