mexique1 Posted March 26, 2012 Share Posted March 26, 2012 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); 1 Link to comment Share on other sites More sharing options...
Raphaël Malié Posted March 26, 2012 Share Posted March 26, 2012 Hehe funny, I didn't know this one I my memory I wrote 1 or 2 funny (rage) comments, but can't remember where Link to comment Share on other sites More sharing options...
mexique1 Posted March 26, 2012 Author Share Posted March 26, 2012 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 $category = new CMSCategory($id, $id_lang); if (count($category)) { ... } Link to comment Share on other sites More sharing options...
Raphaël Malié Posted March 26, 2012 Share Posted March 26, 2012 Ahhh okay I agree with you, some parts of the code are really ... strage :/ One day I saw this (and fixed it in 1.5) : list($join, $where) = array('',''); LOL An other one I found : $qty = (int)(abs(Tools::getValue(‘qty’, 1))) just in cast abs() don't get an int xD ? Link to comment Share on other sites More sharing options...
Broceliande Posted March 27, 2012 Share Posted March 27, 2012 Excellent ! I already found such funny comments or code snippets in Prestashop's core, wich indeed made me smile. Really good Idea to post and share them . Link to comment Share on other sites More sharing options...
olea Posted March 27, 2012 Share Posted March 27, 2012 Another one in /controller/CartController.php, PS1.5 SVN14201, begin of postProcess() if ($this->isTokenValid()) $this->errors[] = Tools::displayError('Invalid token'); 1 Link to comment Share on other sites More sharing options...
mexique1 Posted March 27, 2012 Author Share Posted March 27, 2012 Link to comment Share on other sites More sharing options...
Leadz Posted March 27, 2012 Share Posted March 27, 2012 Another one in /controller/CartController.php, PS1.5 SVN14201, begin of postProcess() if ($this->isTokenValid()) $this->errors[] = Tools::displayError('Invalid token'); LOL Fixed ! Link to comment Share on other sites More sharing options...
Leadz Posted March 27, 2012 Share Posted March 27, 2012 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 $category = new CMSCategory($id, $id_lang); if (count($category)) { ... } LOL Link to comment Share on other sites More sharing options...
olea Posted March 27, 2012 Share Posted March 27, 2012 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 More sharing options...
indus Posted March 27, 2012 Share Posted March 27, 2012 Another one in /controller/CartController.php, PS1.5 SVN14201, begin of postProcess() if ($this->isTokenValid()) $this->errors[] = Tools::displayError('Invalid token'); lol. 1 Link to comment Share on other sites More sharing options...
mexique1 Posted April 4, 2012 Author Share Posted April 4, 2012 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 More sharing options...
olea Posted April 4, 2012 Share Posted April 4, 2012 I agree that the !$n++ notation should not be use as it is very confusing. You should note the lienwhere you've found it so that they can fix it. Link to comment Share on other sites More sharing options...
mexique1 Posted April 4, 2012 Author Share Posted April 4, 2012 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 More sharing options...
mexique1 Posted April 4, 2012 Author Share Posted April 4, 2012 Hey, also opened a ticket to add the curly braces in any statement : http://forge.prestashop.com/browse/PSFV-788 Do you agree ? Link to comment Share on other sites More sharing options...
Maxence de Flotte Posted April 5, 2012 Share Posted April 5, 2012 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 More sharing options...
mexique1 Posted June 18, 2012 Author Share Posted June 18, 2012 Sp.. Sep... Sparate... seperated... {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 More sharing options...
Maxence de Flotte Posted June 19, 2012 Share Posted June 19, 2012 Oh, thanks, this is my fault :/ I fixed it on the SVN. Link to comment Share on other sites More sharing options...
Raphaël Malié Posted June 19, 2012 Share Posted June 19, 2012 it's always Maxence's fault, never forget this Link to comment Share on other sites More sharing options...
Recommended Posts