r99938 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99937‎ | r99938 | r99939 >
Date:03:27, 16 October 2011
Author:reedy
Status:ok (Comments)
Tags:
Comment:
Fixup some assignments in conditionals

Add/normalise some more return statements
Modified paths:
  • /trunk/phase3/includes/EditPage.php (modified) (history)
  • /trunk/phase3/includes/Exception.php (modified) (history)
  • /trunk/phase3/includes/Linker.php (modified) (history)
  • /trunk/phase3/includes/OutputPage.php (modified) (history)
  • /trunk/phase3/includes/Sanitizer.php (modified) (history)
  • /trunk/phase3/includes/Title.php (modified) (history)
  • /trunk/phase3/includes/installer/Ibm_db2Installer.php (modified) (history)
  • /trunk/phase3/includes/objectcache/DBABagOStuff.php (modified) (history)
  • /trunk/phase3/includes/search/SearchUpdate.php (modified) (history)
  • /trunk/phase3/includes/specials/SpecialSearch.php (modified) (history)
  • /trunk/phase3/languages/Language.php (modified) (history)
  • /trunk/phase3/maintenance/benchmarks/benchmarkHooks.php (modified) (history)
  • /trunk/phase3/maintenance/storage/checkStorage.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/search/SearchEngineTest.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/upload/UploadFromUrlTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/storage/checkStorage.php
@@ -99,6 +99,9 @@
100100 $res = $dbr->select( 'text', array( 'old_id', 'old_flags' ),
101101 'old_id IN (' . implode( ',', $this->oldIdMap ) . ')', __METHOD__ );
102102 foreach ( $res as $row ) {
 103+ /**
 104+ * @var $flags int
 105+ */
103106 $flags = $row->old_flags;
104107 $id = $row->old_id;
105108
Index: trunk/phase3/maintenance/benchmarks/benchmarkHooks.php
@@ -49,7 +49,7 @@
5050 $wgHooks['Test'][] = array( $this, 'test' );
5151 }
5252 $time = $this->benchHooks();
53 - $this->output( 'Loaded (ten) hook: ' . $time . "\n" );
 53+ $this->output( 'Loaded (one hundred) hook: ' . $time . "\n" );
5454 $this->output( "\n" );
5555 }
5656
Index: trunk/phase3/tests/phpunit/includes/upload/UploadFromUrlTest.php
@@ -36,7 +36,10 @@
3737 * Ensure that the job queue is empty before continuing
3838 */
3939 public function testClearQueue() {
40 - while ( $job = Job::pop() ) { }
 40+ $job = Job::pop();
 41+ while ( $job ) {
 42+ $job = Job::pop();
 43+ }
4144 $this->assertFalse( $job );
4245 }
4346
Index: trunk/phase3/tests/phpunit/includes/search/SearchEngineTest.php
@@ -64,8 +64,10 @@
6565 $this->assertTrue( is_object( $results ) );
6666
6767 $matches = array();
68 - while ( $row = $results->next() ) {
 68+ $row = $results->next();
 69+ while ( $row ) {
6970 $matches[] = $row->getTitle()->getPrefixedText();
 71+ $row = $results->next();
7072 }
7173 $results->free();
7274 # Search is not guaranteed to return results in a certain order;
Index: trunk/phase3/includes/search/SearchUpdate.php
@@ -37,7 +37,7 @@
3838 global $wgContLang, $wgDisableSearchUpdate;
3939
4040 if( $wgDisableSearchUpdate || !$this->mId ) {
41 - return false;
 41+ return;
4242 }
4343
4444 wfProfileIn( __METHOD__ );
Index: trunk/phase3/includes/objectcache/DBABagOStuff.php
@@ -184,8 +184,10 @@
185185
186186 $result[] = $k1;
187187
188 - while ( $key = dba_nextkey( $reader ) ) {
 188+ $key = dba_nextkey( $reader );
 189+ while ( $key ) {
189190 $result[] = $key;
 191+ $key = dba_nextkey( $reader )
190192 }
191193
192194 return $result;
Index: trunk/phase3/includes/Linker.php
@@ -1823,9 +1823,8 @@
18241824 return Linker::revDeleteLink( $query,
18251825 $rev->isDeleted( File::DELETED_RESTRICTED ), $canHide );
18261826 }
1827 - } else {
1828 - return '';
18291827 }
 1828+ return '';
18301829 }
18311830
18321831 /**
Index: trunk/phase3/includes/EditPage.php
@@ -2938,6 +2938,7 @@
29392939 $wgOut->permissionRequired( 'upload' );
29402940 return false;
29412941 }
 2942+ return false;
29422943 }
29432944
29442945 /**
Index: trunk/phase3/includes/OutputPage.php
@@ -1201,12 +1201,12 @@
12021202 public function addHTML( $text ) {
12031203 $this->mBodytext .= $text;
12041204 }
1205 -
 1205+
12061206 /**
12071207 * Shortcut for adding an Html::element via addHTML.
1208 - *
 1208+ *
12091209 * @since 1.19
1210 - *
 1210+ *
12111211 * @param $element string
12121212 * @param $attribs array
12131213 * @param $contents string
@@ -1730,6 +1730,7 @@
17311731 } elseif ( $this->mPreventClickjacking && $wgEditPageFrameOptions ) {
17321732 return $wgEditPageFrameOptions;
17331733 }
 1734+ return false;
17341735 }
17351736
17361737 /**
Index: trunk/phase3/includes/installer/Ibm_db2Installer.php
@@ -216,12 +216,13 @@
217217 $result = $this->db->query( 'SELECT PAGESIZE FROM SYSCAT.TABLESPACES' );
218218 if( $result == false ) {
219219 $status->fatal( 'config-connection-error', '' );
220 - }
221 - else {
222 - while ( $row = $this->db->fetchRow( $result ) ) {
 220+ } else {
 221+ $row = $this->db->fetchRow( $result );
 222+ while ( $row ) {
223223 if( $row[0] >= 32768 ) {
224224 return $status;
225225 }
 226+ $row = $this->db->fetchRow( $result );
226227 }
227228 $status->fatal( 'config-ibm_db2-low-db-pagesize', '' );
228229 }
Index: trunk/phase3/includes/Sanitizer.php
@@ -33,7 +33,7 @@
3434 * Regular expression to match various types of character references in
3535 * Sanitizer::normalizeCharReferences and Sanitizer::decodeCharReferences
3636 */
37 - const CHAR_REFS_REGEX =
 37+ const CHAR_REFS_REGEX =
3838 '/&([A-Za-z0-9\x80-\xff]+);
3939 |&\#([0-9]+);
4040 |&\#[xX]([0-9A-Fa-f]+);
@@ -335,7 +335,7 @@
336336 $attribFirst = '[:A-Z_a-z0-9]';
337337 $attrib = '[:A-Z_a-z-.0-9]';
338338 $space = '[\x09\x0a\x0d\x20]';
339 - self::$attribsRegex =
 339+ self::$attribsRegex =
340340 "/(?:^|$space)({$attribFirst}{$attrib}*)
341341 ($space*=$space*
342342 (?:
@@ -465,8 +465,14 @@
466466 if ( $t != $ot ) {
467467 # No match. Push the optional elements back again
468468 $badtag = true;
469 - while ( $ot = @array_pop( $optstack ) ) {
 469+ wfSuppressWarnings();
 470+ $ot = array_pop( $optstack );
 471+ wfRestoreWarnings();
 472+ while ( $ot ) {
470473 array_push( $tagstack, $ot );
 474+ wfSuppressWarnings();
 475+ $ot = array_pop( $optstack );
 476+ wfRestoreWarnings();
471477 }
472478 }
473479 } else {
@@ -608,7 +614,7 @@
609615 * This does not validate properties, so you should ensure that you call
610616 * validateTagAttributes AFTER this to ensure that the resulting style rule
611617 * this may add is safe.
612 - *
 618+ *
613619 * - Converts most presentational attributes like align into inline css
614620 *
615621 * @param $attribs Array
@@ -617,19 +623,19 @@
618624 */
619625 static function fixDeprecatedAttributes( $attribs, $element ) {
620626 global $wgHtml5, $wgCleanupPresentationalAttributes;
621 -
 627+
622628 // presentational attributes were removed from html5, we can leave them
623629 // in when html5 is turned off
624630 if ( !$wgHtml5 || !$wgCleanupPresentationalAttributes ) {
625631 return $attribs;
626632 }
627 -
 633+
628634 $table = array( 'table' );
629635 $cells = array( 'td', 'th' );
630636 $colls = array( 'col', 'colgroup' );
631637 $tblocks = array( 'tbody', 'tfoot', 'thead' );
632638 $h = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' );
633 -
 639+
634640 $presentationalAttribs = array(
635641 'align' => array( 'text-align', array_merge( array( 'caption', 'hr', 'div', 'p', 'tr' ), $table, $cells, $colls, $tblocks, $h ) ),
636642 'clear' => array( 'clear', array( 'br' ) ),
@@ -640,7 +646,7 @@
641647 'valign' => array( 'vertical-align', array_merge( $cells, $colls, $tblocks ) ),
642648 'width' => array( 'width', array_merge( array( 'hr', 'pre' ), $table, $cells, $colls ) ),
643649 );
644 -
 650+
645651 // Ensure that any upper case or mixed case attributes are converted to lowercase
646652 foreach ( $attribs as $attribute => $value ) {
647653 if ( $attribute !== strtolower( $attribute ) && array_key_exists( strtolower( $attribute ), $presentationalAttribs ) ) {
@@ -648,45 +654,45 @@
649655 unset( $attribs[$attribute] );
650656 }
651657 }
652 -
 658+
653659 $style = "";
654660 foreach ( $presentationalAttribs as $attribute => $info ) {
655661 list( $property, $elements ) = $info;
656 -
 662+
657663 // Skip if this attribute is not relevant to this element
658664 if ( !in_array( $element, $elements ) ) {
659665 continue;
660666 }
661 -
 667+
662668 // Skip if the attribute is not used
663669 if ( !array_key_exists( $attribute, $attribs ) ) {
664670 continue;
665671 }
666 -
 672+
667673 $value = $attribs[$attribute];
668 -
 674+
669675 // For nowrap the value should be nowrap instead of whatever text is in the value
670676 if ( $attribute === 'nowrap' ) {
671677 $value = 'nowrap';
672678 }
673 -
 679+
674680 // clear="all" is clear: both; in css
675681 if ( $attribute === 'clear' && strtolower( $value ) === 'all' ) {
676682 $value = 'both';
677683 }
678 -
 684+
679685 // Size based properties should have px applied to them if they have no unit
680686 if ( in_array( $attribute, array( 'height', 'width', 'size' ) ) ) {
681687 if ( preg_match( '/^[\d.]+$/', $value ) ) {
682688 $value = "{$value}px";
683689 }
684690 }
685 -
 691+
686692 $style .= " $property: $value;";
687 -
 693+
688694 unset( $attribs[$attribute] );
689695 }
690 -
 696+
691697 if ( $style ) {
692698 // Prepend our style rules so that they can be overridden by user css
693699 if ( isset($attribs['style']) ) {
@@ -694,7 +700,7 @@
695701 }
696702 $attribs['style'] = trim($style);
697703 }
698 -
 704+
699705 return $attribs;
700706 }
701707
@@ -766,7 +772,7 @@
767773 }
768774
769775 //RDFa and microdata properties allow URLs, URIs and/or CURIs. check them for sanity
770 - if ( $attribute === 'rel' || $attribute === 'rev' ||
 776+ if ( $attribute === 'rel' || $attribute === 'rev' ||
771777 $attribute === 'about' || $attribute === 'property' || $attribute === 'resource' || #RDFa
772778 $attribute === 'datatype' || $attribute === 'typeof' || #RDFa
773779 $attribute === 'itemid' || $attribute === 'itemprop' || $attribute === 'itemref' || #HTML5 microdata
@@ -774,7 +780,7 @@
775781
776782 //Paranoia. Allow "simple" values but suppress javascript
777783 if ( preg_match( self::EVIL_URI_PATTERN, $value ) ) {
778 - continue;
 784+ continue;
779785 }
780786 }
781787
@@ -839,7 +845,7 @@
840846 * returned string may contain character references given certain
841847 * clever input strings. These character references must
842848 * be escaped before the return value is embedded in HTML.
843 - *
 849+ *
844850 * @param $value String
845851 * @return String
846852 */
@@ -861,7 +867,7 @@
862868 $space = '[\\x20\\t\\r\\n\\f]';
863869 $nl = '(?:\\n|\\r\\n|\\r|\\f)';
864870 $backslash = '\\\\';
865 - $decodeRegex = "/ $backslash
 871+ $decodeRegex = "/ $backslash
866872 (?:
867873 ($nl) | # 1. Line continuation
868874 ([0-9A-Fa-f]{1,6})$space? | # 2. character number
@@ -871,7 +877,7 @@
872878 }
873879 $value = preg_replace_callback( $decodeRegex,
874880 array( __CLASS__, 'cssDecodeCallback' ), $value );
875 -
 881+
876882 // Remove any comments; IE gets token splitting wrong
877883 // This must be done AFTER decoding character references and
878884 // escape sequences, because those steps can introduce comments
@@ -1446,7 +1452,7 @@
14471453 if ( $wgAllowRdfaAttributes ) {
14481454 #RDFa attributes as specified in section 9 of http://www.w3.org/TR/2008/REC-rdfa-syntax-20081014
14491455 $common = array_merge( $common, array(
1450 - 'about', 'property', 'resource', 'datatype', 'typeof',
 1456+ 'about', 'property', 'resource', 'datatype', 'typeof',
14511457 ) );
14521458 }
14531459
@@ -1563,7 +1569,7 @@
15641570 'th' => array_merge( $common, $tablecell, $tablealign ),
15651571
15661572 # 12.2 # NOTE: <a> is not allowed directly, but the attrib whitelist is used from the Parser object
1567 - 'a' => array_merge( $common, array( 'href', 'rel', 'rev' ) ), # rel/rev esp. for RDFa
 1573+ 'a' => array_merge( $common, array( 'href', 'rel', 'rev' ) ), # rel/rev esp. for RDFa
15681574
15691575 # 13.2
15701576 # Not usually allowed, but may be used for extension-style hooks
@@ -1654,7 +1660,7 @@
16551661 $url = Sanitizer::decodeCharReferences( $url );
16561662
16571663 # Escape any control characters introduced by the above step
1658 - $url = preg_replace_callback( '/[\][<>"\\x00-\\x20\\x7F\|]/',
 1664+ $url = preg_replace_callback( '/[\][<>"\\x00-\\x20\\x7F\|]/',
16591665 array( __CLASS__, 'cleanUrlCallback' ), $url );
16601666
16611667 # Validate hostname portion
Index: trunk/phase3/includes/Title.php
@@ -2552,7 +2552,7 @@
25532553 */
25542554 public function invalidateCache() {
25552555 if ( wfReadOnly() ) {
2556 - return;
 2556+ return false;
25572557 }
25582558 $dbw = wfGetDB( DB_MASTER );
25592559 $success = $dbw->update(
Index: trunk/phase3/includes/specials/SpecialSearch.php
@@ -35,7 +35,7 @@
3636 * string when applicable. Extensions can add new profiles with hooks
3737 * with custom search options just for that profile.
3838 * null|string
39 - */
 39+ */
4040 protected $profile;
4141
4242 /// Search engine
@@ -497,8 +497,10 @@
498498 $out .= "\n<!-- {$infoLine} -->\n";
499499 }
500500 $out .= "<ul class='mw-search-results'>\n";
501 - while( $result = $matches->next() ) {
 501+ $result = $matches->next();
 502+ while( $result ) {
502503 $out .= $this->showHit( $result, $terms );
 504+ $result = $matches->next();
503505 }
504506 $out .= "</ul>\n";
505507
@@ -717,9 +719,11 @@
718720 }
719721
720722 $prev = null;
721 - while( $result = $matches->next() ) {
 723+ $result = $matches->next();
 724+ while( $result ) {
722725 $out .= $this->showInterwikiHit( $result, $prev, $terms, $query, $customCaptions );
723726 $prev = $result->getInterwikiPrefix();
 727+ $result = $matches->next();
724728 }
725729 // TODO: should support paging in a non-confusing way (not sure how though, maybe via ajax)..
726730 $out .= "</ul></div>\n";
Index: trunk/phase3/includes/Exception.php
@@ -70,8 +70,9 @@
7171 $result = null;
7272 }
7373
74 - if ( is_string( $result ) )
 74+ if ( is_string( $result ) ) {
7575 return $result;
 76+ }
7677 }
7778 }
7879
Index: trunk/phase3/languages/Language.php
@@ -3685,7 +3685,7 @@
36863686 $format['noabbrevs'] ? 'hours' : 'hours-abbrev' )->inLanguage( $this );
36873687 $daysMsg = wfMessage(
36883688 $format['noabbrevs'] ? 'days' : 'days-abbrev' )->inLanguage( $this );
3689 -
 3689+
36903690 if ( round( $seconds * 10 ) < 100 ) {
36913691 $s = $this->formatNum( sprintf( "%.1f", round( $seconds * 10 ) / 10 ) );
36923692 $s = $secondsMsg->params( $s )->text();

Follow-up revisions

RevisionCommit summaryAuthorDate
r99968Parse error (missing ';') in r99938.platonides19:53, 16 October 2011

Comments

#Comment by Platonides (talk | contribs)   19:53, 16 October 2011

Missing ; in dba_nextkey r1=99937&r2=99938. I fix it in r99965.

#Comment by 😂 (talk | contribs)   17:29, 6 December 2011

Looks good except Sanitizer.php. Why are we suppressing errors there at all?

#Comment by Reedy (talk | contribs)   20:26, 6 December 2011

No idea. As per the diff, I'm only updating it... But there's a similar usage just above it too....

#Comment by Tim Starling (talk | contribs)   02:05, 12 December 2011

I don't think it's a problem to use assignment in a while() to do iteration, if the interface is designed to be used like that. It can be replaced by foreach() where there's a proper iterator available or if one can be written, that's a better alternative, but I don't think what you've done here is better. Is there some reason for doing it that I don't know about?

#Comment by Reedy (talk | contribs)   14:24, 12 December 2011

Somewhat personal preference... Assigning variables in conditional statements has always seemed strange to me. And then somewhat due to me seeing enough people doing if ( $foo = 'bar' ) {} unknowingly and tgetting their fingers bitten (though, that is their fault)

I'm happy to revert the loops that don't have while -> foreach replacements if you want

#Comment by Catrope (talk | contribs)   15:50, 15 December 2011

I agree with Tim, things like while ( $result = $matches->next() ) { do stuff with $result } should just be allowed. Would be nice to revert those changes, but it's not a big deal. Marking OK.

Status & tagging log