Browse Source

Add deprecation warnings to I18n Package.

Mark Story 8 years ago
parent
commit
d5fcf2745d

+ 8 - 0
src/I18n/FrozenTime.php

@@ -255,6 +255,10 @@ class FrozenTime extends Chronos implements JsonSerializable
     {
         $tmp = trim($timeInterval);
         if (is_numeric($tmp)) {
+            deprecationWarning(
+                'Passing int/numeric string into FrozenTime::wasWithinLast() is deprecated. ' .
+                'Pass strings including interval eg. "6 days"'
+            );
             $timeInterval = $tmp . ' days';
         }
 
@@ -276,6 +280,10 @@ class FrozenTime extends Chronos implements JsonSerializable
     {
         $tmp = trim($timeInterval);
         if (is_numeric($tmp)) {
+            deprecationWarning(
+                'Passing int/numeric string into FrozenTime::isWithinNext() is deprecated. ' .
+                'Pass strings including interval eg. "6 days"'
+            );
             $timeInterval = $tmp . ' days';
         }
 

+ 15 - 0
src/I18n/I18n.php

@@ -125,6 +125,10 @@ class I18n
      */
     public static function translator($name = 'default', $locale = null, callable $loader = null)
     {
+        deprecationWarning(
+            'I18n::translator() is deprecated. ' .
+            'Use I18n::setTranslator()/getTranslator() instead.'
+        );
         if ($loader !== null) {
             static::setTranslator($name, $loader, $locale);
 
@@ -271,6 +275,10 @@ class I18n
      */
     public static function locale($locale = null)
     {
+        deprecationWarning(
+            'I18n::locale() is deprecated. ' .
+            'Use I18n::setLocale()/getLocale() instead.'
+        );
         if (!empty($locale)) {
             static::setLocale($locale);
 
@@ -324,6 +332,8 @@ class I18n
      */
     public static function defaultLocale()
     {
+        deprecationWarning('I18n::defaultLocale() is deprecated. Use I18n::getDefaultLocale() instead.');
+
         return static::getDefaultLocale();
     }
 
@@ -359,6 +369,11 @@ class I18n
      */
     public static function defaultFormatter($name = null)
     {
+        deprecationWarning(
+            'I18n::defaultFormatter() is deprecated. ' .
+            'Use I18n::setDefaultFormatter()/getDefaultFormatter() instead.'
+        );
+
         return static::translators()->defaultFormatter($name);
     }
 

+ 8 - 0
src/I18n/Time.php

@@ -334,6 +334,10 @@ class Time extends MutableDateTime implements JsonSerializable
     {
         $tmp = trim($timeInterval);
         if (is_numeric($tmp)) {
+            deprecationWarning(
+                'Passing int/numeric string into Time::wasWithinLast() is deprecated. ' .
+                'Pass strings including interval eg. "6 days"'
+            );
             $timeInterval = $tmp . ' days';
         }
 
@@ -355,6 +359,10 @@ class Time extends MutableDateTime implements JsonSerializable
     {
         $tmp = trim($timeInterval);
         if (is_numeric($tmp)) {
+            deprecationWarning(
+                'Passing int/numeric string into Time::isWithinNext() is deprecated. ' .
+                'Pass strings including interval eg. "6 days"'
+            );
             $timeInterval = $tmp . ' days';
         }
 

+ 2 - 0
src/View/Helper/TimeHelper.php

@@ -300,6 +300,7 @@ class TimeHelper extends Helper
      *
      * @param string|int $timeInterval the numeric value with space then time type.
      *    Example of valid types: 6 hours, 2 days, 1 minute.
+     *    Integer input values are deprecated and support will be removed in 4.0.0
      * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
      * @param string|\DateTimeZone|null $timezone User's timezone string or DateTimeZone object
      * @return bool
@@ -315,6 +316,7 @@ class TimeHelper extends Helper
      *
      * @param string|int $timeInterval the numeric value with space then time type.
      *    Example of valid types: 6 hours, 2 days, 1 minute.
+     *    Integer input values are deprecated and support will be removed in 4.0.0
      * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
      * @param string|\DateTimeZone|null $timezone User's timezone string or DateTimeZone object
      * @return bool

+ 15 - 12
tests/TestCase/I18n/I18nTest.php

@@ -54,7 +54,7 @@ class I18nTest extends TestCase
     {
         parent::tearDown();
         I18n::clear();
-        I18n::defaultFormatter('default');
+        I18n::setDefaultFormatter('default');
         I18n::setLocale($this->locale);
         Plugin::unload();
         Cache::clear(false, '_cake_core_');
@@ -63,14 +63,17 @@ class I18nTest extends TestCase
     /**
      * Tests that the default locale is set correctly
      *
+     * @group deprecated
      * @return void
      */
     public function testDefaultLocale()
     {
-        $newLocale = 'de_DE';
-        I18n::setLocale($newLocale);
-        $this->assertEquals($newLocale, I18n::getLocale());
-        $this->assertEquals($this->locale, I18n::getDefaultLocale());
+        $this->deprecated(function () {
+            $newLocale = 'de_DE';
+            I18n::setLocale($newLocale);
+            $this->assertEquals($newLocale, I18n::getLocale());
+            $this->assertEquals($this->locale, I18n::getDefaultLocale());
+        });
     }
 
     /**
@@ -106,7 +109,7 @@ class I18nTest extends TestCase
      */
     public function testPluralSelection()
     {
-        I18n::defaultFormatter('sprintf');
+        I18n::setDefaultFormatter('sprintf');
         $translator = I18n::getTranslator(); // en_US
         $result = $translator->translate('%d = 0 or > 1', ['_count' => 1]);
         $this->assertEquals('1 is 1 (po translated)', $result);
@@ -254,7 +257,7 @@ class I18nTest extends TestCase
      */
     public function testBasicTranslateFunction()
     {
-        I18n::defaultFormatter('sprintf');
+        I18n::setDefaultFormatter('sprintf');
         $this->assertEquals('%d is 1 (po translated)', __('%d = 1'));
         $this->assertEquals('1 is 1 (po translated)', __('%d = 1', 1));
         $this->assertEquals('1 is 1 (po translated)', __('%d = 1', [1]));
@@ -301,7 +304,7 @@ class I18nTest extends TestCase
      */
     public function testBasicTranslateFunctionPluralData()
     {
-        I18n::defaultFormatter('sprintf');
+        I18n::setDefaultFormatter('sprintf');
         $this->assertEquals('%d is 1 (po translated)', __('%d = 0 or > 1'));
     }
 
@@ -312,7 +315,7 @@ class I18nTest extends TestCase
      */
     public function testBasicTranslatePluralFunction()
     {
-        I18n::defaultFormatter('sprintf');
+        I18n::setDefaultFormatter('sprintf');
         $result = __n('singular msg', '%d = 0 or > 1', 1);
         $this->assertEquals('1 is 1 (po translated)', $result);
 
@@ -333,7 +336,7 @@ class I18nTest extends TestCase
      */
     public function testBasicTranslatePluralFunctionSingularMessage()
     {
-        I18n::defaultFormatter('sprintf');
+        I18n::setDefaultFormatter('sprintf');
         $result = __n('No translation needed', 'not used', 1);
         $this->assertEquals('No translation needed', $result);
     }
@@ -867,7 +870,7 @@ class I18nTest extends TestCase
      */
     public function testEmptyTranslationString()
     {
-        I18n::defaultFormatter('sprintf');
+        I18n::setDefaultFormatter('sprintf');
         $result = __('No translation needed');
         $this->assertEquals('No translation needed', $result);
     }
@@ -879,7 +882,7 @@ class I18nTest extends TestCase
      */
     public function testPluralTranslationsFromDomain()
     {
-        I18n::locale('de');
+        I18n::setLocale('de');
         $this->assertEquals('Standorte', __dn('wa', 'Location', 'Locations', 0));
         $this->assertEquals('Standort', __dn('wa', 'Location', 'Locations', 1));
         $this->assertEquals('Standorte', __dn('wa', 'Location', 'Locations', 2));

+ 1 - 1
tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php

@@ -55,7 +55,7 @@ class TranslateBehaviorTest extends TestCase
     public function tearDown()
     {
         parent::tearDown();
-        I18n::setLocale(I18n::defaultLocale());
+        I18n::setLocale(I18n::getDefaultLocale());
         TableRegistry::clear();
     }
 

+ 15 - 4
tests/TestCase/View/Helper/TimeHelperTest.php

@@ -448,11 +448,22 @@ class TimeHelperTest extends TestCase
         $this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
         $this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
         $this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
+    }
 
-        $this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days'));
-        $this->assertTrue($this->Time->wasWithinLast('1   ', '-1 hour'));
-        $this->assertTrue($this->Time->wasWithinLast('1   ', '-1 minute'));
-        $this->assertTrue($this->Time->wasWithinLast('1   ', '-23 hours -59 minutes -59 seconds'));
+    /**
+     * test deprecated usage of wasWithinLast()
+     *
+     * @group deprecated
+     * @return void
+     */
+    public function testWasWithinLastNumericString()
+    {
+        $this->deprecated(function () {
+            $this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days'));
+            $this->assertTrue($this->Time->wasWithinLast('1   ', '-1 hour'));
+            $this->assertTrue($this->Time->wasWithinLast('1   ', '-1 minute'));
+            $this->assertTrue($this->Time->wasWithinLast('1   ', '-23 hours -59 minutes -59 seconds'));
+        });
     }
 
     /**