Another copy-paste wtf



  • Okay so I'm a big lurker, and  here dare I post a first WTF snippet ... moreover in a foreign language

    I maintain PHP (i know, i know) websites at work, and I know i'm not the best developer in the world, but the previous webmaster had.. hm.. interesting practices.

    on our main website, there is this nifty little function :

    (this is my first post containing code, I hope the forum software won't mess it up)

    <?
    function cvt_date($d_in$type)
    {
      
    // Fonction allowing to translate a mysql timestamp
      // in various formats
      
    $d substr($d_in,6,2); // jour
      
    $m substr($d_in,4,2); // mois
      
    $y substr($d_in,0,4); // année

      
    switch ($type)
      {
        case 
    "UNIX":
          
    // TIMESTAMP mysql >> TIMESTAMP UNIX
          
    return mktime(0,0,0,$m$d$y);
          break;

        case 
    "FR":
          
    // TIMESTAMP mysql >> Date format FR
          
    return $d."/".$m."/".$y;
          break;

        case 
    "ENG":
          
    // TIMESTAMP mysql >> Date format ENG
          
    return $y."/".$m."/".$d;
      }
    }
    ?>


    In fact I found where he got this masterpiece. A bit odd, but not so bad actually. Well it does what it's supposed to. kind of.

    What is a bit funnier is that he did not put this function in a single file and then included that file... but copy-pasted it in every file where it was needed, at the very beginning of each file... with the occasionnal alteration of the (pretty useless) comments (in order to prevent me to do an efficient site-wide search & replace, I presume)..
     
    there are much more of these in our homebrew intranet php app, but that'll be for another post....



  • Maybe you can teach him about require.  At least he was kind enough to put it in the beginning of every file so you know <i>exactly</i> where it is.

    It can be OK to use PHP...but not like that.

     



  • That's just amazing how much of these bad practices lie around, popping out of google's first result when looking for help. When it comes to certain matter, the "I'm lucky" button should actually be labeled "I'm cursed". It works much like contamination. It's not even a blog, the website's name is "phpinfo". Oh, my ...

    I love how he generates a string based on current date and then parses it. Again, what we have here is people focusing on data representation, much like testing if the first character of a number is "-". They should be sent to an assembly course and then another one about data structures.

     Anyway due to leap years you have to compare separately year, month and day. While orx57's solution seems quite horribly verbose ( it's php after all ... why use DateTime.now.day in some nice language when you can write that splendid $current_date["mday"] ? ), I think it's actually correct. It could use a little refactoring though.



  • if you use dreamweaver or ultraedit (or perhaps other programming editors as well) you can do regular expression based search & replace.  Then you can look for the whole function by just including its fingerprint features and regular expressioning out the rest.  From a glance, I'd look for something like this (with a bunch of extra regular expression stuff in there to fill in the blankspaces).  Fun times!!

     function cvt_date($d_in, $type)
    {
    <FONT face="Courier New">  </FONT><FONT face="Courier New">substr($d_in
    </FONT><FONT face="Courier New"> 
    </FONT><FONT face="Courier New">  switch ($type</FONT><FONT face="Courier New">)
      {
        case 
    "UNIX"</FONT><FONT face="Courier New">: 
    </FONT>
    <FONT face="Courier New">    case "FR"</FONT><FONT face="Courier New">: 
    </FONT>
    <FONT face="Courier New">    case "ENG"</FONT><FONT face="Courier New">: 
    </FONT>
    <FONT face="Courier New">  }
    }</FONT>



  • How about this?

    <?
    function cvt_date($d_in, $type)
    {
      $timestamp = strtotime($d_in);

      switch ($type)
      {
        case "UNIX":
          return $timestamp;
        case "FR":
          return date("d/m/Y", $timestamp);
        case "ENG":
          return date("Y/m/d", $timestamp);
      }
    }
    ?>



  • @boolean said:

    How about this?

    <?
    function cvt_date($d_in, $type)
    {
      $timestamp = strtotime($d_in);

      switch ($type)
      {
        case "UNIX":
          return $timestamp;
        case "FR":
          return date("d/m/Y", $timestamp);
        case "ENG":
          return date("Y/m/d", $timestamp);
      }
    }
    ?>

     Yep. Quite exactly what I had in mind.

    I would have chosen "EN" rather than "ENG" as the original function does, but I'm nitpicking... (wouldn't cause problems, I don't even think we use the ENG case at all in our (french) website.)

    BTW, I've been seeing this code nearly every day for quite some time now, and I didn't even noticed the switch lacked a "default" case !... bah. I use my own functions now, and I haven't kept in touch with the previous webmaster

     Thank you pauluskc for your advice, I didn't know dreamweaver could do this (but there are a lot of things that I don't know about dw...) I'll look into it
     



  • Oh my...

    Here's another snippet from the same intranet, by the same person...


    if ($ordre != "" ){
        if($customer){
            if($cal_date != ""){
                $todo = "SELECT * FROM todo WHERE done_todo = 0 AND id_customer=$customer AND date_end=\"$cal_date\" ORDER BY $ordre";
            }else{
                $todo = "SELECT * FROM todo WHERE done_todo = 0 AND id_customer=$customer ORDER BY $ordre";
            }
        }else{
            if($cal_date != ""){
            $todo = "SELECT * FROM todo WHERE done_todo = 0 AND date_end=\"$cal_date\" ORDER BY $ordre";
            }else{
            $todo = "SELECT * FROM todo WHERE done_todo = 0 ORDER BY $ordre";
            }
        }
    }
    else {
        if($customer){
            if($cal_date != ""){
                $todo = "SELECT * FROM todo WHERE done_todo = 0 AND id_customer = $customer AND date_end = \"$cal_date\" ORDER BY date_end ASC";
            }else{
                $todo = "SELECT * FROM todo WHERE done_todo = 0 AND id_customer = $customer ORDER BY date_end ASC";
            }
        }else{
            if($cal_date != ""){
                $todo = "SELECT * FROM todo WHERE done_todo = 0 AND date_end = \"$cal_date\" ORDER BY date_end ASC";
            }else{
                $todo = "SELECT * FROM todo WHERE done_todo = 0 ORDER BY date_end ASC";
            }
        }
    }
    if ($p_do = @mysql_query($todo,$conn)){

            // SNIP a lot of things

            while ($rs_do = @mysql_fetch_array($p_do)){

             // SNIP more things

     
            <?
    $SQL_customer = "SELECT * FROM customers WHERE id_customer = $rs_do[id_customer] ";
    if ($p_customer = @mysql_query($SQL_customer,$conn)){
    $rs_customer = @mysql_fetch_array($p_customer)
    ?>
          <b>
          <?= $rs_customer[denomination_customer] ?>
          </b><br>
          <?= $rs_customer[nom_customer] ?>
          <?= $rs_customer[prenom_customer] ?>
          <?
    }
    ?>

    // .... SNIP yet more stuff

      <?
    }// end condition loop
          } // end loop
    } // end condition
    ?>

     

    WTF ? This intranet is such a mess... 

    I've just reduced a 200-lines-long piece of shit to a clean and readable file, acomplishing the same task in less than 50... sigh

     



  • Thing is, who the hell uses y/m/d? y-m-d (hyphenated) with a four-figure year, sure, but that's still non-standard usage. Define "English"? If you mean British English, it's d/m/y (same as France, apparently) but US English is m/d/y. y-m-d is much safer, but y/m/d? I am not sure I've ever seen that anywhere. Sadly, y-m-d is preferable on the Web simply due to the painful ambiguity of slash-delimited English dates. I use y-m-d when I'm cutting down on space, but full dates like "(Monday) 18th June 2007" anywhere I can. (Whether I include the day is relatively arbitrary, since I'm hopelessly arbitrary even across my own site ;)

    Incidentally, "English" needs a new flag. I find it interesting to see which flag people use for English on sites -- which people associate the language with the United States, and which people associate the language with the United Kingdom. Poor old Canada, Australia, New Zealand and so on never get a look in. At least one site has the flag divided diagonally to include the US and UK flags simultaneously.

    The same is true of other languages, where the flag favours a particular country. German for example -- an Austrian would have to choose the German flag. I don't know how much Swiss German and Austrian differ from German German, but US and British English are similar enough, on sites that offer a choice of English versus other languages, that people from either country would cope. (They tend not to get bogged down in things like dummy/pacifier, nappy/diaper, lifebelt/life preserver, pavement/sidewalk, lorry/truck etc.) But I have no idea how the German dialects vary and whether they'd qualify for a new "German language" nation-independent flag ... Ditto Spanish, French, Dutch, Chinese ...

    Maybe I just think too much? :-)



  • @Daniel Beardsmore said:

    The same is true of other languages, where the flag favours a particular country. German for example -- an Austrian would have to choose the German flag. I don't know how much Swiss German and Austrian differ from German German, but US and British English are similar enough, on sites that offer a choice of English versus other languages, that people from either country would cope. (They tend not to get bogged down in things like dummy/pacifier, nappy/diaper, lifebelt/life preserver, pavement/sidewalk, lorry/truck etc.) But I have no idea how the German dialects vary and whether they'd qualify for a new "German language" nation-independent flag ... Ditto Spanish, French, Dutch, Chinese ...

    Maybe I just think too much? :-)

    Standard German is understood pretty much everywhere, even in regions like Bavaria where the native dialect is a crazy moon language. While there are entirely different languages (Low German, Romansh, etc.) spoken in the German-speaking countries, Standard German is understood everywhere.

    Little tidbit: Coincidentally, Standard German is the commonly used "dialect" in exactly those parts of North Germany where the (now dying) native language used to be Low German, which in turn is the only non-German native language in Germany. (Yes, "Low German" sounds like it's a subset of German, but it's more like a mix between German, Dutch, English, lots of Umlauts and a sometimes rather crass choice of wording. Example, courtesy of Wikipedia: "Plattdüütsch höört to de westgermaanschen Spraken. Se hett den tweten germaanschen Luudwannel nich mitmaakt un is dormit ene nedderdüütsche Spraak. De Utdrück Nedderdüütsch un Neddersassisch warrt denn ok ganz faken jüstso bruukt as Plattdüütsch.")


    That should tell you everything you never wanted to know about the German language but weren't afrait to ask about. 


Log in to reply