From 9ca0f6c62021352347811e6a316ebcd6d1164d6e Mon Sep 17 00:00:00 2001 From: Marius Hoch Date: Thu, 23 Jun 2016 12:01:59 +0200 Subject: [PATCH] Only attempt to calculate the TTL in Language::sprintfDate if needed Change-Id: Ifd24c9206be05bb4fd2277efc574c9d1018e1957 --- languages/Language.php | 6 ++++-- tests/phpunit/languages/LanguageTest.php | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/languages/Language.php b/languages/Language.php index f0c7d76c0a..37f4137a7e 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -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 ); diff --git a/tests/phpunit/languages/LanguageTest.php b/tests/phpunit/languages/LanguageTest.php index 9e41a4eabb..e2e64920ef 100644 --- a/tests/phpunit/languages/LanguageTest.php +++ b/tests/phpunit/languages/LanguageTest.php @@ -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 [ [ -- 2.20.1