Conditions | 10 |
Paths | 36 |
Total Lines | 59 |
Code Lines | 42 |
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 | $this->output( "Remove unused accounts\n\n" ); |
||
44 | |||
45 | # Do an initial scan for inactive accounts and report the result |
||
46 | $this->output( "Checking for unused user accounts...\n" ); |
||
47 | $del = []; |
||
48 | $dbr = $this->getDB( DB_REPLICA ); |
||
49 | $res = $dbr->select( 'user', [ 'user_id', 'user_name', 'user_touched' ], '', __METHOD__ ); |
||
50 | if ( $this->hasOption( 'ignore-groups' ) ) { |
||
51 | $excludedGroups = explode( ',', $this->getOption( 'ignore-groups' ) ); |
||
52 | } else { |
||
53 | $excludedGroups = []; |
||
54 | } |
||
55 | $touched = $this->getOption( 'ignore-touched', "1" ); |
||
56 | if ( !ctype_digit( $touched ) ) { |
||
57 | $this->error( "Please put a valid positive integer on the --ignore-touched parameter.", true ); |
||
58 | } |
||
59 | $touchedSeconds = 86400 * $touched; |
||
60 | foreach ( $res as $row ) { |
||
61 | # Check the account, but ignore it if it's within a $excludedGroups |
||
62 | # group or if it's touched within the $touchedSeconds seconds. |
||
63 | $instance = User::newFromId( $row->user_id ); |
||
64 | if ( count( array_intersect( $instance->getEffectiveGroups(), $excludedGroups ) ) == 0 |
||
65 | && $this->isInactiveAccount( $row->user_id, true ) |
||
66 | && wfTimestamp( TS_UNIX, $row->user_touched ) < wfTimestamp( TS_UNIX, time() - $touchedSeconds ) |
||
67 | ) { |
||
68 | # Inactive; print out the name and flag it |
||
69 | $del[] = $row->user_id; |
||
70 | $this->output( $row->user_name . "\n" ); |
||
71 | } |
||
72 | } |
||
73 | $count = count( $del ); |
||
74 | $this->output( "...found {$count}.\n" ); |
||
75 | |||
76 | # If required, go back and delete each marked account |
||
77 | if ( $count > 0 && $this->hasOption( 'delete' ) ) { |
||
78 | $this->output( "\nDeleting unused accounts..." ); |
||
79 | $dbw = $this->getDB( DB_MASTER ); |
||
80 | $dbw->delete( 'user', [ 'user_id' => $del ], __METHOD__ ); |
||
81 | $dbw->delete( 'user_groups', [ 'ug_user' => $del ], __METHOD__ ); |
||
82 | $dbw->delete( 'user_former_groups', [ 'ufg_user' => $del ], __METHOD__ ); |
||
83 | $dbw->delete( 'user_properties', [ 'up_user' => $del ], __METHOD__ ); |
||
84 | $dbw->delete( 'logging', [ 'log_user' => $del ], __METHOD__ ); |
||
85 | $dbw->delete( 'recentchanges', [ 'rc_user' => $del ], __METHOD__ ); |
||
86 | $this->output( "done.\n" ); |
||
87 | # Update the site_stats.ss_users field |
||
88 | $users = $dbw->selectField( 'user', 'COUNT(*)', [], __METHOD__ ); |
||
89 | $dbw->update( |
||
90 | 'site_stats', |
||
91 | [ 'ss_users' => $users ], |
||
92 | [ 'ss_row_id' => 1 ], |
||
93 | __METHOD__ |
||
94 | ); |
||
95 | } elseif ( $count > 0 ) { |
||
96 | $this->output( "\nRun the script again with --delete to remove them from the database.\n" ); |
||
97 | } |
||
98 | $this->output( "\n" ); |
||
99 | } |
||
100 | |||
137 |
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.