Development

#1821 (Improvement of sfCultureInfo::simplify())

You must first sign up to be able to contribute.

Ticket #1821 (closed enhancement: fixed)

Opened 2 years ago

Last modified 1 year ago

Improvement of sfCultureInfo::simplify()

Reported by: Alexandre.Saunier Assigned to: fabien
Priority: minor Milestone: 1.0.10
Component: i18n Version:
Keywords: Cc:
Qualification: Unreviewed

Description

sfCultureInfo::simplify() implementation may be a little modernized using foreach() instead of for() loops. Resulting code is more concise and a little faster, especially since it may handle relatively big arrays (eg. the 195-item languages list).

Here is the patch:

Index: lib/i18n/sfCultureInfo.class.php
===================================================================
--- lib/i18n/sfCultureInfo.class.php    (révision 4161)
+++ lib/i18n/sfCultureInfo.class.php    (copie de travail)
@@ -623,15 +623,13 @@
    */
   protected function simplify($array)
   {
-    for ($i = 0, $max = count($array); $i < $max; $i++)
-    {
-      $key = key($array);
-      if (is_array($array[$key]) && count($array[$key]) == 1)
-      {
-        $array[$key] = $array[$key][0];
-      }
-      next($array);
-    }
+    foreach ($array as &$item)
+    {   
+      if (is_array($item) && count($item) == 1)
+      {   
+        $item = $item[0];
+      }   
+    } 
 
     return $array;
   }

I also wrote some basic unit tests:

<?php
include(dirname(__FILE__).'/../bootstrap/unit.php');
require_once($sf_symfony_lib_dir.'/i18n/sfCultureInfo.class.php');

$array1 = array(0 => 'hello', 1 => 'world');
$array2 = array(0 => array('hello'), 1 => 'world');
$array3 = array(0 => array('hello', 'hi'), 1 => 'world');

$ci = new sfCultureInfo;

$t = new lime_test(4, new lime_output_color());
$t->diag('sfCultureInfo::simplify()');
$t->isa_ok($ci->simplify($array1), 'array', 'returns an array');
$t->is_deeply($ci->simplify($array1), $array1, 'leaves 1D-arrays unchanged');
$t->is_deeply($ci->simplify($array2), $array1, 'simplifies arrays');
$t->is_deeply($ci->simplify($array3), $array3, 'leaves not-simplifiable arrays unchanged');

Please note that those tests only work if the method is switched from protected to public, which is not necessary in "real life". By the way it could be made static as well but a quick profiling has not revealed significant performance gain.

The change seems to pass the following tests as well: http://trac.symfony-project.com/trac/browser/trunk/test/unit/i18n/sfCultureInfoTest.php?rev=2763

Change History

06/05/07 15:28:08 changed by Alexandre.Saunier

  • type changed from defect to enhancement.

06/05/07 17:15:01 changed by Alexandre.Saunier

Oops! I missed the most recent revision of sfCultureInfo unit tests! http://trac.symfony-project.com/trac/browser/trunk/test/unit/i18n/sfCultureInfoTest.php But it's OK as well.

12/07/07 21:10:26 changed by fabien

  • qualification set to Unreviewed.
  • milestone set to 1.0.10.

12/07/07 21:11:14 changed by fabien

  • status changed from new to closed.
  • resolution set to fixed.

(In [6375]) changed implementation of sfCultureInfo::simplify (closes #1821)

12/07/07 21:11:37 changed by fabien

(In [6376]) changed implementation of sfCultureInfo::simplify (closes #1821)