r106726 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106725‎ | r106726 | r106727 >
Date:23:44, 19 December 2011
Author:reedy
Status:ok
Tags:
Comment:
Add some braces and also some documentation
Modified paths:
  • /trunk/extensions/ParserFunctions/Convert.php (modified) (history)
  • /trunk/extensions/ParserFunctions/Expr.php (modified) (history)
  • /trunk/extensions/Score/Score.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Score/Score.php
@@ -70,7 +70,7 @@
7171 /**
7272 * Init routine.
7373 *
74 - * @param $parser Mediawiki parser
 74+ * @param $parser Parser Mediawiki parser
7575 *
7676 * @return true if initialisation was successful, false otherwise.
7777 */
Index: trunk/extensions/ParserFunctions/Expr.php
@@ -157,6 +157,8 @@
158158 * The algorithm here is based on the infix to RPN algorithm given in
159159 * http://montcs.bloomu.edu/~bobmon/Information/RPN/infix2rpn.shtml
160160 * It's essentially the same as Dijkstra's shunting yard algorithm.
 161+ * @param $expr string
 162+ * @return string
161163 */
162164 function doExpression( $expr ) {
163165 $operands = array();
@@ -371,181 +373,260 @@
372374 return implode( "<br />\n", $operands );
373375 }
374376
 377+ /**
 378+ * @param $op int
 379+ * @param $stack array
 380+ * @throws ExprError
 381+ */
375382 function doOperation( $op, &$stack ) {
376383 switch ( $op ) {
377384 case EXPR_NEGATIVE:
378 - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 385+ if ( count( $stack ) < 1 ) {
 386+ throw new ExprError( 'missing_operand', $this->names[$op] );
 387+ }
379388 $arg = array_pop( $stack );
380389 $stack[] = -$arg;
381390 break;
382391 case EXPR_POSITIVE:
383 - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 392+ if ( count( $stack ) < 1 ) {
 393+ throw new ExprError( 'missing_operand', $this->names[$op] );
 394+ }
384395 break;
385396 case EXPR_TIMES:
386 - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 397+ if ( count( $stack ) < 2 ) {
 398+ throw new ExprError( 'missing_operand', $this->names[$op] );
 399+ }
387400 $right = array_pop( $stack );
388401 $left = array_pop( $stack );
389402 $stack[] = $left * $right;
390403 break;
391404 case EXPR_DIVIDE:
392 - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 405+ if ( count( $stack ) < 2 ) {
 406+ throw new ExprError( 'missing_operand', $this->names[$op] );
 407+ }
393408 $right = array_pop( $stack );
394409 $left = array_pop( $stack );
395 - if ( $right == 0 ) throw new ExprError( 'division_by_zero', $this->names[$op] );
 410+ if ( $right == 0 ) {
 411+ throw new ExprError( 'division_by_zero', $this->names[$op] );
 412+ }
396413 $stack[] = $left / $right;
397414 break;
398415 case EXPR_MOD:
399 - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 416+ if ( count( $stack ) < 2 ) {
 417+ throw new ExprError( 'missing_operand', $this->names[$op] );
 418+ }
400419 $right = array_pop( $stack );
401420 $left = array_pop( $stack );
402 - if ( $right == 0 ) throw new ExprError( 'division_by_zero', $this->names[$op] );
 421+ if ( $right == 0 ) {
 422+ throw new ExprError( 'division_by_zero', $this->names[$op] );
 423+ }
403424 $stack[] = $left % $right;
404425 break;
405426 case EXPR_PLUS:
406 - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 427+ if ( count( $stack ) < 2 ) {
 428+ throw new ExprError( 'missing_operand', $this->names[$op] );
 429+ }
407430 $right = array_pop( $stack );
408431 $left = array_pop( $stack );
409432 $stack[] = $left + $right;
410433 break;
411434 case EXPR_MINUS:
412 - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 435+ if ( count( $stack ) < 2 ) {
 436+ throw new ExprError( 'missing_operand', $this->names[$op] );
 437+ }
413438 $right = array_pop( $stack );
414439 $left = array_pop( $stack );
415440 $stack[] = $left - $right;
416441 break;
417442 case EXPR_AND:
418 - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 443+ if ( count( $stack ) < 2 ) {
 444+ throw new ExprError( 'missing_operand', $this->names[$op] );
 445+ }
419446 $right = array_pop( $stack );
420447 $left = array_pop( $stack );
421448 $stack[] = ( $left && $right ) ? 1 : 0;
422449 break;
423450 case EXPR_OR:
424 - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 451+ if ( count( $stack ) < 2 ) {
 452+ throw new ExprError( 'missing_operand', $this->names[$op] );
 453+ }
425454 $right = array_pop( $stack );
426455 $left = array_pop( $stack );
427456 $stack[] = ( $left || $right ) ? 1 : 0;
428457 break;
429458 case EXPR_EQUALITY:
430 - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 459+ if ( count( $stack ) < 2 ) {
 460+ throw new ExprError( 'missing_operand', $this->names[$op] );
 461+ }
431462 $right = array_pop( $stack );
432463 $left = array_pop( $stack );
433464 $stack[] = ( $left == $right ) ? 1 : 0;
434465 break;
435466 case EXPR_NOT:
436 - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 467+ if ( count( $stack ) < 1 ) {
 468+ throw new ExprError( 'missing_operand', $this->names[$op] );
 469+ }
437470 $arg = array_pop( $stack );
438471 $stack[] = ( !$arg ) ? 1 : 0;
439472 break;
440473 case EXPR_ROUND:
441 - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 474+ if ( count( $stack ) < 2 ) {
 475+ throw new ExprError( 'missing_operand', $this->names[$op] );
 476+ }
442477 $digits = intval( array_pop( $stack ) );
443478 $value = array_pop( $stack );
444479 $stack[] = round( $value, $digits );
445480 break;
446481 case EXPR_LESS:
447 - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 482+ if ( count( $stack ) < 2 ) {
 483+ throw new ExprError( 'missing_operand', $this->names[$op] );
 484+ }
448485 $right = array_pop( $stack );
449486 $left = array_pop( $stack );
450487 $stack[] = ( $left < $right ) ? 1 : 0;
451488 break;
452489 case EXPR_GREATER:
453 - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 490+ if ( count( $stack ) < 2 ) {
 491+ throw new ExprError( 'missing_operand', $this->names[$op] );
 492+ }
454493 $right = array_pop( $stack );
455494 $left = array_pop( $stack );
456495 $stack[] = ( $left > $right ) ? 1 : 0;
457496 break;
458497 case EXPR_LESSEQ:
459 - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 498+ if ( count( $stack ) < 2 ) {
 499+ throw new ExprError( 'missing_operand', $this->names[$op] );
 500+ }
460501 $right = array_pop( $stack );
461502 $left = array_pop( $stack );
462503 $stack[] = ( $left <= $right ) ? 1 : 0;
463504 break;
464505 case EXPR_GREATEREQ:
465 - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 506+ if ( count( $stack ) < 2 ) {
 507+ throw new ExprError( 'missing_operand', $this->names[$op] );
 508+ }
466509 $right = array_pop( $stack );
467510 $left = array_pop( $stack );
468511 $stack[] = ( $left >= $right ) ? 1 : 0;
469512 break;
470513 case EXPR_NOTEQ:
471 - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 514+ if ( count( $stack ) < 2 ) {
 515+ throw new ExprError( 'missing_operand', $this->names[$op] );
 516+ }
472517 $right = array_pop( $stack );
473518 $left = array_pop( $stack );
474519 $stack[] = ( $left != $right ) ? 1 : 0;
475520 break;
476521 case EXPR_EXPONENT:
477 - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 522+ if ( count( $stack ) < 2 ) {
 523+ throw new ExprError( 'missing_operand', $this->names[$op] );
 524+ }
478525 $right = array_pop( $stack );
479526 $left = array_pop( $stack );
480527 $stack[] = $left * pow( 10, $right );
481528 break;
482529 case EXPR_SINE:
483 - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 530+ if ( count( $stack ) < 1 ) {
 531+ throw new ExprError( 'missing_operand', $this->names[$op] );
 532+ }
484533 $arg = array_pop( $stack );
485534 $stack[] = sin( $arg );
486535 break;
487536 case EXPR_COSINE:
488 - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 537+ if ( count( $stack ) < 1 ) {
 538+ throw new ExprError( 'missing_operand', $this->names[$op] );
 539+ }
489540 $arg = array_pop( $stack );
490541 $stack[] = cos( $arg );
491542 break;
492543 case EXPR_TANGENS:
493 - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 544+ if ( count( $stack ) < 1 ) {
 545+ throw new ExprError( 'missing_operand', $this->names[$op] );
 546+ }
494547 $arg = array_pop( $stack );
495548 $stack[] = tan( $arg );
496549 break;
497550 case EXPR_ARCSINE:
498 - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 551+ if ( count( $stack ) < 1 ) {
 552+ throw new ExprError( 'missing_operand', $this->names[$op] );
 553+ }
499554 $arg = array_pop( $stack );
500 - if ( $arg < -1 || $arg > 1 ) throw new ExprError( 'invalid_argument', $this->names[$op] );
 555+ if ( $arg < -1 || $arg > 1 ) {
 556+ throw new ExprError( 'invalid_argument', $this->names[$op] );
 557+ }
501558 $stack[] = asin( $arg );
502559 break;
503560 case EXPR_ARCCOS:
504 - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 561+ if ( count( $stack ) < 1 ) {
 562+ throw new ExprError( 'missing_operand', $this->names[$op] );
 563+ }
505564 $arg = array_pop( $stack );
506 - if ( $arg < -1 || $arg > 1 ) throw new ExprError( 'invalid_argument', $this->names[$op] );
 565+ if ( $arg < -1 || $arg > 1 ) {
 566+ throw new ExprError( 'invalid_argument', $this->names[$op] );
 567+ }
507568 $stack[] = acos( $arg );
508569 break;
509570 case EXPR_ARCTAN:
510 - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 571+ if ( count( $stack ) < 1 ) {
 572+ throw new ExprError( 'missing_operand', $this->names[$op] );
 573+ }
511574 $arg = array_pop( $stack );
512575 $stack[] = atan( $arg );
513576 break;
514577 case EXPR_EXP:
515 - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 578+ if ( count( $stack ) < 1 ) {
 579+ throw new ExprError( 'missing_operand', $this->names[$op] );
 580+ }
516581 $arg = array_pop( $stack );
517582 $stack[] = exp( $arg );
518583 break;
519584 case EXPR_LN:
520 - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 585+ if ( count( $stack ) < 1 ) {
 586+ throw new ExprError( 'missing_operand', $this->names[$op] );
 587+ }
521588 $arg = array_pop( $stack );
522 - if ( $arg <= 0 ) throw new ExprError( 'invalid_argument_ln', $this->names[$op] );
 589+ if ( $arg <= 0 ) {
 590+ throw new ExprError( 'invalid_argument_ln', $this->names[$op] );
 591+ }
523592 $stack[] = log( $arg );
524593 break;
525594 case EXPR_ABS:
526 - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 595+ if ( count( $stack ) < 1 ) {
 596+ throw new ExprError( 'missing_operand', $this->names[$op] );
 597+ }
527598 $arg = array_pop( $stack );
528599 $stack[] = abs( $arg );
529600 break;
530601 case EXPR_FLOOR:
531 - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 602+ if ( count( $stack ) < 1 ) {
 603+ throw new ExprError( 'missing_operand', $this->names[$op] );
 604+ }
532605 $arg = array_pop( $stack );
533606 $stack[] = floor( $arg );
534607 break;
535608 case EXPR_TRUNC:
536 - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 609+ if ( count( $stack ) < 1 ) {
 610+ throw new ExprError( 'missing_operand', $this->names[$op] );
 611+ }
537612 $arg = array_pop( $stack );
538613 $stack[] = (int)$arg;
539614 break;
540615 case EXPR_CEIL:
541 - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 616+ if ( count( $stack ) < 1 ) {
 617+ throw new ExprError( 'missing_operand', $this->names[$op] );
 618+ }
542619 $arg = array_pop( $stack );
543620 $stack[] = ceil( $arg );
544621 break;
545622 case EXPR_POW:
546 - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] );
 623+ if ( count( $stack ) < 2 ) {
 624+ throw new ExprError( 'missing_operand', $this->names[$op] );
 625+ }
547626 $right = array_pop( $stack );
548627 $left = array_pop( $stack );
549 - if ( false === ( $stack[] = pow( $left, $right ) ) ) throw new ExprError( 'division_by_zero', $this->names[$op] );
 628+ if ( false === ( $stack[] = pow( $left, $right ) ) ) {
 629+ throw new ExprError( 'division_by_zero', $this->names[$op] );
 630+ }
550631 break;
551632 default:
552633 // Should be impossible to reach here.
Index: trunk/extensions/ParserFunctions/Convert.php
@@ -54,6 +54,8 @@
5555 # The last value converted, which will be used for PLURAL evaluation
5656 protected $lastValue;
5757
 58+ protected $precision;
 59+
5860 /**
5961 * Reset the parser so it isn't contaminated by the results of previous parses
6062 */

Status & tagging log