Conditions | 8 |
Paths | 64 |
Total Lines | 55 |
Code Lines | 32 |
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 |
||
45 | function doDelete( $delete = false, $args = [] ) { |
||
46 | |||
47 | # Data should come off the master, wrapped in a transaction |
||
48 | $dbw = $this->getDB( DB_MASTER ); |
||
49 | $this->beginTransaction( $dbw, __METHOD__ ); |
||
50 | |||
51 | $pageConds = []; |
||
52 | $revConds = []; |
||
53 | |||
54 | # If a list of page_ids was provided, limit results to that set of page_ids |
||
55 | if ( count( $args ) > 0 ) { |
||
56 | $pageConds['page_id'] = $args; |
||
57 | $revConds['rev_page'] = $args; |
||
58 | $this->output( "Limiting to page IDs " . implode( ',', $args ) . "\n" ); |
||
59 | } |
||
60 | |||
61 | # Get "active" revisions from the page table |
||
62 | $this->output( "Searching for active revisions..." ); |
||
63 | $res = $dbw->select( 'page', 'page_latest', $pageConds, __METHOD__ ); |
||
64 | $latestRevs = []; |
||
65 | foreach ( $res as $row ) { |
||
66 | $latestRevs[] = $row->page_latest; |
||
67 | } |
||
68 | $this->output( "done.\n" ); |
||
69 | |||
70 | # Get all revisions that aren't in this set |
||
71 | $this->output( "Searching for inactive revisions..." ); |
||
72 | if ( count( $latestRevs ) > 0 ) { |
||
73 | $revConds[] = 'rev_id NOT IN (' . $dbw->makeList( $latestRevs ) . ')'; |
||
74 | } |
||
75 | $res = $dbw->select( 'revision', 'rev_id', $revConds, __METHOD__ ); |
||
76 | $oldRevs = []; |
||
77 | foreach ( $res as $row ) { |
||
78 | $oldRevs[] = $row->rev_id; |
||
79 | } |
||
80 | $this->output( "done.\n" ); |
||
81 | |||
82 | # Inform the user of what we're going to do |
||
83 | $count = count( $oldRevs ); |
||
84 | $this->output( "$count old revisions found.\n" ); |
||
85 | |||
86 | # Delete as appropriate |
||
87 | if ( $delete && $count ) { |
||
88 | $this->output( "Deleting..." ); |
||
89 | $dbw->delete( 'revision', [ 'rev_id' => $oldRevs ], __METHOD__ ); |
||
90 | $this->output( "done.\n" ); |
||
91 | } |
||
92 | |||
93 | # This bit's done |
||
94 | # Purge redundant text records |
||
95 | $this->commitTransaction( $dbw, __METHOD__ ); |
||
96 | if ( $delete ) { |
||
97 | $this->purgeRedundantText( true ); |
||
98 | } |
||
99 | } |
||
100 | } |
||
104 |
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.