| Conditions | 9 |
| Paths | 9 |
| Total Lines | 57 |
| 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 |
||
| 100 | private function cleanupArticle( $id, $domain ) { |
||
| 101 | $title = Title::newFromID( $id ); |
||
| 102 | if ( !$title ) { |
||
| 103 | $this->error( "Internal error: no page for ID $id" ); |
||
| 104 | |||
| 105 | return; |
||
| 106 | } |
||
| 107 | |||
| 108 | $this->output( $title->getPrefixedDBkey() . " ..." ); |
||
| 109 | $rev = Revision::newFromTitle( $title ); |
||
| 110 | $currentRevId = $rev->getId(); |
||
| 111 | |||
| 112 | while ( $rev && ( $rev->isDeleted( Revision::DELETED_TEXT ) |
||
| 113 | || LinkFilter::matchEntry( $rev->getContent( Revision::RAW ), $domain ) ) |
||
| 114 | ) { |
||
| 115 | $rev = $rev->getPrevious(); |
||
| 116 | } |
||
| 117 | |||
| 118 | if ( $rev && $rev->getId() == $currentRevId ) { |
||
| 119 | // The regex didn't match the current article text |
||
| 120 | // This happens e.g. when a link comes from a template rather than the page itself |
||
| 121 | $this->output( "False match\n" ); |
||
| 122 | } else { |
||
| 123 | $dbw = $this->getDB( DB_MASTER ); |
||
| 124 | $this->beginTransaction( $dbw, __METHOD__ ); |
||
| 125 | $page = WikiPage::factory( $title ); |
||
| 126 | if ( $rev ) { |
||
| 127 | // Revert to this revision |
||
| 128 | $content = $rev->getContent( Revision::RAW ); |
||
| 129 | |||
| 130 | $this->output( "reverting\n" ); |
||
| 131 | $page->doEditContent( |
||
| 132 | $content, |
||
| 133 | wfMessage( 'spam_reverting', $domain )->inContentLanguage()->text(), |
||
| 134 | EDIT_UPDATE, |
||
| 135 | $rev->getId() |
||
| 136 | ); |
||
| 137 | } elseif ( $this->hasOption( 'delete' ) ) { |
||
| 138 | // Didn't find a non-spammy revision, blank the page |
||
| 139 | $this->output( "deleting\n" ); |
||
| 140 | $page->doDeleteArticle( |
||
| 141 | wfMessage( 'spam_deleting', $domain )->inContentLanguage()->text() |
||
| 142 | ); |
||
| 143 | } else { |
||
| 144 | // Didn't find a non-spammy revision, blank the page |
||
| 145 | $handler = ContentHandler::getForTitle( $title ); |
||
| 146 | $content = $handler->makeEmptyContent(); |
||
| 147 | |||
| 148 | $this->output( "blanking\n" ); |
||
| 149 | $page->doEditContent( |
||
| 150 | $content, |
||
| 151 | wfMessage( 'spam_blanking', $domain )->inContentLanguage()->text() |
||
| 152 | ); |
||
| 153 | } |
||
| 154 | $this->commitTransaction( $dbw, __METHOD__ ); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } |
||
| 161 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.