Jump to content

Funny code snippets


Recommended Posts

Hi all,

 

To have some fun while waiting the upcoming new version, I propose to paste here the most funny code snippets found in PrestaShop. This can be any piece of code !

 

Here goes the first one, pure frenglish style :)

 

// Many, many children. So many it takes an 's'
$childrens = Category::getChildren($id_category, $id_lang);

  • Like 1
Link to comment
Share on other sites

The comments are mine ! This is just the name of the variable :)

 

This one is interesting :

How much does count() return on an instance of a class ? Obviously true.

Even if an instance of a class created via the constructor will never be null or false, it's better to check :lol:

 

$category = new CMSCategory($id, $id_lang);
if (count($category))
{
   ...
}

Link to comment
Share on other sites

The comments are mine ! This is just the name of the variable :) This one is interesting : How much does count() return on an instance of a class ? Obviously true. Even if an instance of a class created via the constructor will never be null or false, it's better to check :lol:
 $category = new CMSCategory($id, $id_lang); if (count($category)) { ... } 

 

LOL

Link to comment
Share on other sites

Just for notice, there is a lot of places in the code where we have :

$result = Db::getInstance()->executeS('sql req');
foreach ($result as $item) {
bla bla
}

 

But, the executeS may return FALSE, which is not an array on which the foreach may loop

Link to comment
Share on other sites

This one is not really "funny", but rather a "coding horror" :)

 

What does the $n variable in the code below ? You have 15 seconds... Took me a while to figure it

 

Notice the lack of curly braces and abusive use of ternary operators that make the code almost unreadable.

 

 $vars = (!$array) ? '' : array();
 $vars_nb = array('n', 'search_query');
 $vars_sort = array('orderby', 'orderway');
 $vars_pagination = array('p');
 $n = 0;
 foreach ($_GET as $k => $value)
  if ($k != 'id_'.$type && $k != 'controller')
  {
   if (Configuration::get('PS_REWRITING_SETTINGS') && ($k == 'isolang' || $k == 'id_lang'))
 continue;
   $if_nb = (!$nb || ($nb && !in_array($k, $vars_nb)));
   $if_sort = (!$sort || ($sort && !in_array($k, $vars_sort)));
   $if_pagination = (!$pagination || ($pagination && !in_array($k, $vars_pagination)));
   if ($if_nb && $if_sort && $if_pagination && !is_array($value))
 !$array ? ($vars .= ((!$n++ && ($this->allow == 1 || $url == $this->url)) ? '?' : '&').urlencode($k).'='.urlencode($value)) : ($vars[urlencode($k)] = urlencode($value));
  }
 if (!$array)
  return $url.$vars;

Link to comment
Share on other sites

Actually I opened a ticket about this piece of code, but to make it take array parameters into consideration : http://forge.prestashop.com/browse/PSFV-786

Maxence de Flotte just told me on Twitter he is refactoring it. Great !

 

---

 

Yep, taking adavantage of the fact that PHP casts 0 to false is not expressive.

This variable is a boolean ! At first sight when I see $n I think it's some counter...

 

It should be named $isFirst for example.

 

Even better, http_build_query() function should be used (http://fr2.php.net/http_build_query) : this way you get rid of the repeated if ($array)

Link to comment
Share on other sites

Hi all, To have some fun while waiting the upcoming new version, I propose to paste here the most funny code snippets found in PrestaShop. This can be any piece of code ! Here goes the first one, pure frenglish style :)
 // Many, many children. So many it takes an 's'
$childrens = Category::getChildren($id_category, $id_lang); 

 

On 1.5, I changed for children & child (instead of childrens and children): http://scm.prestasho...29&r2=14482

Link to comment
Share on other sites

  • 2 months later...

Sp.. Sep... Sparate... seperated... :D

 

{if $show_option_allow_sparate_package}
<p>
<input type="checkbox" name="allow_seperated_package" id="allow_seperated_package" {if $cart->allow_seperated_package}checked="checked"{/if} />
<label for="allow_seperated_package">{l s='Send the available products first'}</label>
</p>
{/if}

Link to comment
Share on other sites

×
×
  • Create New...