Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
35 | class ResetUserTokens extends Maintenance { |
||
36 | View Code Duplication | public function __construct() { |
|
51 | |||
52 | public function execute() { |
||
53 | $this->nullsOnly = $this->getOption( 'nulls' ); |
||
54 | |||
55 | if ( !$this->getOption( 'nowarn' ) ) { |
||
56 | if ( $this->nullsOnly ) { |
||
57 | $this->output( "The script is about to reset the user_token " |
||
58 | . "for USERS WITH NULL TOKENS in the database.\n" ); |
||
59 | } else { |
||
60 | $this->output( "The script is about to reset the user_token for ALL USERS in the database.\n" ); |
||
61 | $this->output( "This may log some of them out and is not necessary unless you believe your\n" ); |
||
62 | $this->output( "user table has been compromised.\n" ); |
||
63 | } |
||
64 | $this->output( "\n" ); |
||
65 | $this->output( "Abort with control-c in the next five seconds " |
||
66 | . "(skip this countdown with --nowarn) ... " ); |
||
67 | wfCountDown( 5 ); |
||
68 | } |
||
69 | |||
70 | // We list user by user_id from one of the replica DBs |
||
71 | // We list user by user_id from one of the slave database |
||
72 | $dbr = $this->getDB( DB_REPLICA ); |
||
73 | |||
74 | $where = []; |
||
75 | if ( $this->nullsOnly ) { |
||
76 | // Have to build this by hand, because \ is escaped in helper functions |
||
77 | $where = [ 'user_token = \'' . str_repeat( '\0', 32 ) . '\'' ]; |
||
78 | } |
||
79 | |||
80 | $maxid = $dbr->selectField( 'user', 'MAX(user_id)', [], __METHOD__ ); |
||
81 | |||
82 | $min = 0; |
||
83 | $max = $this->mBatchSize; |
||
84 | |||
85 | do { |
||
86 | $result = $dbr->select( 'user', |
||
87 | [ 'user_id' ], |
||
88 | array_merge( |
||
89 | $where, |
||
90 | [ 'user_id > ' . $dbr->addQuotes( $min ), |
||
91 | 'user_id <= ' . $dbr->addQuotes( $max ) |
||
92 | ] |
||
93 | ), |
||
94 | __METHOD__ |
||
95 | ); |
||
96 | |||
97 | foreach ( $result as $user ) { |
||
98 | $this->updateUser( $user->user_id ); |
||
99 | } |
||
100 | |||
101 | $min = $max; |
||
102 | $max = $min + $this->mBatchSize; |
||
103 | |||
104 | wfWaitForSlaves(); |
||
105 | } while ( $min <= $maxid ); |
||
106 | } |
||
107 | |||
108 | private function updateUser( $userid ) { |
||
117 | } |
||
118 | |||
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.