Only attempt to calculate the TTL in Language::sprintfDate if needed
authorMarius Hoch <hoo@online.de>
Thu, 23 Jun 2016 10:01:59 +0000 (12:01 +0200)
committerMarius Hoch <hoo@online.de>
Thu, 23 Jun 2016 10:36:25 +0000 (12:36 +0200)
Change-Id: Ifd24c9206be05bb4fd2277efc574c9d1018e1957

languages/Language.php
tests/phpunit/languages/LanguageTest.php

index f0c7d76..37f4137 100644 (file)
@@ -1087,7 +1087,7 @@ class Language {
         * @throws MWException
         * @return string
         */
-       public function sprintfDate( $format, $ts, DateTimeZone $zone = null, &$ttl = null ) {
+       public function sprintfDate( $format, $ts, DateTimeZone $zone = null, &$ttl = 'unused' ) {
                $s = '';
                $raw = false;
                $roman = false;
@@ -1452,7 +1452,9 @@ class Language {
                        }
                }
 
-               if ( $usedSecond ) {
+               if ( $ttl === 'unused' ) {
+                       // No need to calculate the TTL, the caller wont use it anyway.
+               } elseif ( $usedSecond ) {
                        $ttl = 1;
                } elseif ( $usedMinute ) {
                        $ttl = 60 - substr( $ts, 12, 2 );
index 9e41a4e..e2e6492 100644 (file)
@@ -638,6 +638,24 @@ class LanguageTest extends LanguageClassesTestCase {
                );
        }
 
+       /**
+        * sprintfDate should only calculate a TTL if the caller is going to use it.
+        * @covers Language::sprintfDate
+        */
+       public function testSprintfDateNoTtlIfNotNeeded() {
+               $noTtl = 'unused'; // Value used to represent that the caller didn't pass a variable in.
+               $ttl = null;
+               $this->getLang()->sprintfDate( 'YmdHis', wfTimestampNow(), null, $noTtl );
+               $this->getLang()->sprintfDate( 'YmdHis', wfTimestampNow(), null, $ttl );
+
+               $this->assertSame(
+                       'unused',
+                       $noTtl,
+                       'If the caller does not set the $ttl variable, do not compute it.'
+               );
+               $this->assertInternalType( 'int', $ttl, 'TTL should have been computed.' );
+       }
+
        public static function provideSprintfDateSamples() {
                return [
                        [