Conditions | 8 |
Paths | 25 |
Total Lines | 64 |
Code Lines | 44 |
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 |
||
41 | public function execute() { |
||
42 | |||
43 | $name = $this->getArg(); |
||
44 | $delete = $this->getOption( 'delete', false ); |
||
45 | |||
46 | $dbw = $this->getDB( DB_MASTER ); |
||
47 | $this->beginTransaction( $dbw, __METHOD__ ); |
||
48 | |||
49 | $tbl_pag = $dbw->tableName( 'page' ); |
||
50 | $tbl_rec = $dbw->tableName( 'recentchanges' ); |
||
51 | $tbl_rev = $dbw->tableName( 'revision' ); |
||
52 | |||
53 | # Get page ID |
||
54 | $this->output( "Searching for \"$name\"..." ); |
||
55 | $title = Title::newFromText( $name ); |
||
56 | if ( $title ) { |
||
57 | $id = $title->getArticleID(); |
||
58 | $real = $title->getPrefixedText(); |
||
59 | $isGoodArticle = $title->isContentPage(); |
||
60 | $this->output( "found \"$real\" with ID $id.\n" ); |
||
61 | |||
62 | # Get corresponding revisions |
||
63 | $this->output( "Searching for revisions..." ); |
||
64 | $res = $dbw->query( "SELECT rev_id FROM $tbl_rev WHERE rev_page = $id" ); |
||
65 | $revs = []; |
||
66 | foreach ( $res as $row ) { |
||
67 | $revs[] = $row->rev_id; |
||
68 | } |
||
69 | $count = count( $revs ); |
||
70 | $this->output( "found $count.\n" ); |
||
71 | |||
72 | # Delete the page record and associated recent changes entries |
||
73 | if ( $delete ) { |
||
74 | $this->output( "Deleting page record..." ); |
||
75 | $dbw->query( "DELETE FROM $tbl_pag WHERE page_id = $id" ); |
||
76 | $this->output( "done.\n" ); |
||
77 | $this->output( "Cleaning up recent changes..." ); |
||
78 | $dbw->query( "DELETE FROM $tbl_rec WHERE rc_cur_id = $id" ); |
||
79 | $this->output( "done.\n" ); |
||
80 | } |
||
81 | |||
82 | $this->commitTransaction( $dbw, __METHOD__ ); |
||
83 | |||
84 | # Delete revisions as appropriate |
||
85 | if ( $delete && $count ) { |
||
86 | $this->output( "Deleting revisions..." ); |
||
87 | $this->deleteRevisions( $revs ); |
||
88 | $this->output( "done.\n" ); |
||
89 | $this->purgeRedundantText( true ); |
||
90 | } |
||
91 | |||
92 | # Update stats as appropriate |
||
93 | if ( $delete ) { |
||
94 | $this->output( "Updating site stats..." ); |
||
95 | $ga = $isGoodArticle ? -1 : 0; // if it was good, decrement that too |
||
96 | $stats = new SiteStatsUpdate( 0, -$count, $ga, -1 ); |
||
97 | $stats->doUpdate(); |
||
98 | $this->output( "done.\n" ); |
||
99 | } |
||
100 | } else { |
||
101 | $this->output( "not found in database.\n" ); |
||
102 | $this->commitTransaction( $dbw, __METHOD__ ); |
||
103 | } |
||
104 | } |
||
105 | |||
121 |
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.