| Conditions | 6 |
| Paths | 12 |
| 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 declare( strict_types=1 ); |
||
| 96 | protected function updateVotePage( array $pages ) { |
||
| 97 | $votePage = $this->getConfig()->get( 'ric-vote-page' ); |
||
| 98 | $content = $this->getController()->getPageContent( $votePage ); |
||
| 99 | |||
| 100 | $titles = []; |
||
| 101 | foreach ( $pages as $page ) { |
||
| 102 | $titles[] = preg_quote( $page->getTitle() ); |
||
| 103 | } |
||
| 104 | $titleReg = implode( '|', $titles ); |
||
| 105 | $search = "!^\*.+ La \[\[($titleReg)\|procedura]] termina.+\n!gm"; |
||
| 106 | |||
| 107 | $newContent = preg_replace( $search, '', $content ); |
||
| 108 | // Make sure the last line ends with a full stop |
||
| 109 | $sectionReg = '!(^;È in corso.+riconferma tacita.+amministratori.+\n(?:\*.+[;\.]\n)+\*.+)[\.;]!m'; |
||
| 110 | $newContent = preg_replace( $sectionReg, '$1.', $newContent ); |
||
| 111 | |||
| 112 | $newLines = ''; |
||
| 113 | $time = WikiController::getTimeWithArticle( time() + ( 60 * 60 * 24 * 14 ) ); |
||
| 114 | foreach ( $pages as $page ) { |
||
| 115 | $newLines .= "*[[Utente:{$page->getUser()}|]]. La [[{$page->getTitle()}|votazione]] termina $time;\n"; |
||
| 116 | } |
||
| 117 | |||
| 118 | $introReg = '!^Si vota per la \[\[Wikipedia:Amministratori/Riconferma annuale.+!m'; |
||
| 119 | if ( preg_match( $introReg, strip_tags( $content ) ) ) { |
||
| 120 | // Put before the existing ones, if they're found outside comments |
||
| 121 | $newContent = preg_replace( $introReg, '$0' . "\n$newLines", $newContent, 1 ); |
||
| 122 | } else { |
||
| 123 | // Start section |
||
| 124 | $matches = []; |
||
| 125 | if ( preg_match( $introReg, $content, $matches ) === false ) { |
||
| 126 | throw new TaskException( 'Intro not found in vote page' ); |
||
| 127 | } |
||
| 128 | $beforeReg = '!INSERIRE LA NOTIZIA PIÙ NUOVA IN CIMA.+!m'; |
||
| 129 | // Replace semicolon with full stop |
||
| 130 | $newLines = substr( $newLines, 0, -2 ) . ".\n"; |
||
| 131 | $newContent = preg_replace( $beforeReg, '$0' . "\n{$matches[0]}\n$newLines", $newContent, 1 ); |
||
| 132 | } |
||
| 133 | |||
| 134 | $summary = strtr( |
||
| 135 | $this->getConfig()->get( 'vote-start-vote-page-summary' ), |
||
| 136 | [ '$num' => count( $pages ) ] |
||
| 137 | ); |
||
| 138 | $summary = preg_replace_callback( |
||
| 139 | '!\{\{$plur|(\d+)|([^|]+)|([^|]+)}}!', |
||
| 140 | function ( $matches ) { |
||
| 141 | return intval( $matches[1] ) > 1 ? trim( $matches[3] ) : trim( $matches[2] ); |
||
| 142 | }, |
||
| 143 | $summary |
||
| 144 | ); |
||
| 145 | |||
| 146 | $params = [ |
||
| 147 | 'title' => $votePage, |
||
| 148 | 'text' => $newContent, |
||
| 149 | 'summary' => $summary |
||
| 150 | ]; |
||
| 151 | |||
| 152 | $this->getController()->editPage( $params ); |
||
| 153 | } |
||
| 205 |