r104551 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r104550‎ | r104551 | r104552 >
Date:11:45, 29 November 2011
Author:hashar
Status:ok
Tags:
Comment:
abstract out $data and name it 'sectionData'

Makes things easier to handle when calling other methods. This avoid method
with ton of parameters.
Also moved duplicate code that reset section data to a new clearSection()
method. Less lines makes code easier to read.

Follow r104549
Modified paths:
  • /trunk/phase3/tests/testHelpers.inc (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/testHelpers.inc
@@ -356,6 +356,7 @@
357357 private $index = 0;
358358 private $test;
359359 private $section = null; /** String|null: current test section being analyzed */
 360+ private $sectionData = array();
360361 private $lineNum;
361362 private $eof;
362363
@@ -410,8 +411,7 @@
411412 }
412413
413414 function readNextTest() {
414 - $data = array();
415 - $this->section = null;
 415+ $this->clearSection();
416416
417417 while ( false !== ( $line = fgets( $this->fh ) ) ) {
418418 $this->lineNum++;
@@ -421,21 +421,20 @@
422422 $this->section = strtolower( $matches[1] );
423423
424424 if ( $this->section == 'endarticle' ) {
425 - $this->checkSection( $data, 'text' );
426 - $this->checkSection( $data, 'article' );
 425+ $this->checkSection( 'text' );
 426+ $this->checkSection( 'article' );
427427
428 - $this->parserTest->addArticle( ParserTest::chomp( $data['article'] ), $data['text'], $this->lineNum );
 428+ $this->parserTest->addArticle( ParserTest::chomp( $this->sectionData['article'] ), $this->sectionData['text'], $this->lineNum );
429429
430 - $data = array();
431 - $this->section = null;
 430+ $this->clearSection();
432431
433432 continue;
434433 }
435434
436435 if ( $this->section == 'endhooks' ) {
437 - $this->checkSection( $data, 'hooks' );
 436+ $this->checkSection( 'hooks' );
438437
439 - foreach ( explode( "\n", $data['hooks'] ) as $line ) {
 438+ foreach ( explode( "\n", $this->sectionData['hooks'] ) as $line ) {
440439 $line = trim( $line );
441440
442441 if ( $line ) {
@@ -445,16 +444,15 @@
446445 }
447446 }
448447
449 - $data = array();
450 - $this->section = null;
 448+ $this->clearSection();
451449
452450 continue;
453451 }
454452
455453 if ( $this->section == 'endfunctionhooks' ) {
456 - $this->checkSection( $data, 'functionhooks' );
 454+ $this->checkSection( 'functionhooks' );
457455
458 - foreach ( explode( "\n", $data['functionhooks'] ) as $line ) {
 456+ foreach ( explode( "\n", $this->sectionData['functionhooks'] ) as $line ) {
459457 $line = trim( $line );
460458
461459 if ( $line ) {
@@ -464,55 +462,53 @@
465463 }
466464 }
467465
468 - $data = array();
469 - $this->section = null;
 466+ $this->clearSection();
470467
471468 continue;
472469 }
473470
474471 if ( $this->section == 'end' ) {
475 - $this->checkSection( $data, 'test' );
476 - $this->checkSection( $data, 'input' );
477 - $this->checkSection( $data, 'result' );
 472+ $this->checkSection( 'test' );
 473+ $this->checkSection( 'input' );
 474+ $this->checkSection( 'result' );
478475
479 - if ( !isset( $data['options'] ) ) {
480 - $data['options'] = '';
 476+ if ( !isset( $this->sectionData['options'] ) ) {
 477+ $this->sectionData['options'] = '';
481478 }
482479
483 - if ( !isset( $data['config'] ) )
484 - $data['config'] = '';
 480+ if ( !isset( $this->sectionData['config'] ) )
 481+ $this->sectionData['config'] = '';
485482
486 - if ( ( ( preg_match( '/\\bdisabled\\b/i', $data['options'] ) && !$this->parserTest->runDisabled )
487 - || !preg_match( "/" . $this->parserTest->regex . "/i", $data['test'] ) ) ) {
 483+ if ( ( ( preg_match( '/\\bdisabled\\b/i', $this->sectionData['options'] ) && !$this->parserTest->runDisabled )
 484+ || !preg_match( "/" . $this->parserTest->regex . "/i", $this->sectionData['test'] ) ) ) {
488485 # disabled test
489 - $data = array();
490 - $this->section = null;
 486+ $this->clearSection();
491487
492488 continue;
493489 }
494490
495491 $this->test = array(
496 - 'test' => ParserTest::chomp( $data['test'] ),
497 - 'input' => ParserTest::chomp( $data['input'] ),
498 - 'result' => ParserTest::chomp( $data['result'] ),
499 - 'options' => ParserTest::chomp( $data['options'] ),
500 - 'config' => ParserTest::chomp( $data['config'] ),
 492+ 'test' => ParserTest::chomp( $this->sectionData['test'] ),
 493+ 'input' => ParserTest::chomp( $this->sectionData['input'] ),
 494+ 'result' => ParserTest::chomp( $this->sectionData['result'] ),
 495+ 'options' => ParserTest::chomp( $this->sectionData['options'] ),
 496+ 'config' => ParserTest::chomp( $this->sectionData['config'] ),
501497 );
502498
503499 return true;
504500 }
505501
506 - if ( isset ( $data[$this->section] ) ) {
 502+ if ( isset ( $this->sectionData[$this->section] ) ) {
507503 throw new MWException( "duplicate section '$section' at line {$this->lineNum} of $this->file\n" );
508504 }
509505
510 - $data[$this->section] = '';
 506+ $this->sectionData[$this->section] = '';
511507
512508 continue;
513509 }
514510
515511 if ( $this->section ) {
516 - $data[$this->section] .= $line;
 512+ $this->sectionData[$this->section] .= $line;
517513 }
518514 }
519515
@@ -520,22 +516,29 @@
521517 }
522518
523519
 520+ /**
 521+ * Clear section name and its data
 522+ */
 523+ private function clearSection() {
 524+ $this->sectionData = array();
 525+ $this->section = null;
 526+
 527+ }
524528
525529 /**
526 - * Verify the first parameter array ($data) has a value for the second
527 - * parameter key name ($token).
 530+ * Verify the current section data has some value for the given token
 531+ * name (first parameter).
528532 * Throw an exception if it is not set, referencing current section
529533 * and adding the current file name and line number
530534 *
531 - * @param $data Array: an array of parser test data. See readNextTest()
532535 * @param $token String: expected token that should have been mentionned before closing this section
533536 */
534 - private function checkSection( $data, $token ) {
 537+ private function checkSection( $token ) {
535538 if( is_null( $this->section ) ) {
536 - throw new MWException( __METHOD__ . " could not verify a null section!\n" );
 539+ throw new MWException( __METHOD__ . " can not verify a null section!\n" );
537540 }
538541
539 - if( !isset($data[$token]) ) {
 542+ if( !isset($this->sectionData[$token]) ) {
540543 throw new MWException( sprintf(
541544 "'%s' without '%s' at line %s of %s\n",
542545 $this->section,

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r104549abstract out section sanity check...hashar11:27, 29 November 2011

Status & tagging log