Friday, May 23, 2014

Too many comments!

$_____________________________ = "this variable is to separate the two commented code part";

Wednesday, May 21, 2014

Longest CSS selector ever

When more developers shit into the same CSS, well, things get complicated.

body.forum-archive.bbpress.archive.post-type-archive.post-type-archive-forum.logged-in.admin-bar.blog.customize-support .container.blog .header img.logo{ margin:0px; }


Tuesday, April 15, 2014

A function for array to object conversion

Ironically the title of this post is way longer than the function itself, which I've just found:

public static function arrayToObject($d) {
return (object) $d;
}

Monday, April 7, 2014

"NO TOUCHES"

You know how PHP works, great, that's a thing, but please. Don't torture me having to debug this.

$$$ENTITY_NAME=$e[$i]; //no touches

Yes, this guy used 3 freaking dollar signs the valid way. (I wouldn't post how the validity came out 'cuz of space). Now I can prove, egoism also exists in coding style.

Trust me, it's random!

Remember this soon-to-be-classic picture from xkcd?


There's a story for this, too. Not so obvious of course, but still rocks. Enjoy the new findings of Project Legacy Code:

$a = random8digit();
$b = random8digit();
$c = random8digit();
$d = random8digit();

for($i=0;$i<25;++$i){
$randomhash[$i] = $a.$b.$c.$d;
}

Trust me, they are chosen by a strong random value generator algorythm. This code is part of a free ecommerce script which -turns out- also has integrated parts of PayPal API. I would accept a bank transfer of random 8 digits :)

Sunday, April 6, 2014

Remainders of an obfuscated PHP core library

Working with something that's built on an obsolete legacy core library is priceless. The functions themselves look like this


Simulating mouse button hold

... is the easiest using a simple recursive function which continuously does clicking, thought Mr. Legacy Coder, and found out, that the best jQuery implementation for this would be onclick="$(this).click();". Too bad, that this throws a Maximum call stack size exceeded error. Yea, there's always hope, that such script execution ends in a finite time. The key element of legacy code is always Magic. But this came to his mind, too - turns out from the following few lines: preventDefault() called after 3000 milliseconds timeout. #genius

Friday, April 4, 2014

PHP SPL is a mess

The standard PHP library is a mess. There's no system in function naming neither parameter order whatsoever. Two key things, which would be crucial for learning the language without having to memorize all kind of random sh*t. Even more confusing, if you - as me, few seconds ago - face an incredible composition of two functions, that seemingly produce different behaviour. They don't. They are just named this way, thanks to the great SPL. Yes, I've met html_entity_decode(htmlentities($content)) in a code that's meant to clean some ckeditor input text. Just as horrible, as it seems. The more anti-conventional PHP is, the more newbies will make these mistakes. I'm seriously waiting the moment when camels come, dollar signs disappear, and PHP turns to an object-oriented language, while it stays dynamically typed. Sweet dreams, huh? (until then, Ruby is almost the whole package).

Reading directory contents the right way

I stumbled upon another magical line that uses the same scheme for logical evaluation. Goes for sure.

 while(false !== ( $file = readdir($dir)) )

After pasting this, I'm some kind of suffering from cognitive dissonance, so let me fix it.

while( $file = readdir($dir) )

The emptiness strikes

An empty included file can mess up character encoding if it's saved not as UTF8. But why does one include a blank document?

Sunday, March 30, 2014

How to overload a PHP constructor properly?

The answer is obviously this:

if(func_num_args()==0){
//do nothing
}else{
if(func_num_args()>0 && isset(func_get_args[0])){

}
}

Saturday, March 29, 2014

Query massacre

Wow! I got myself over the shock of facing another deprecated mysql_ function, but look at this construction:

for($i=0;$i<5;$i++){
$q[$i]=mysql_query("SELECT * FROM users LIMIT $i,1");
}

Uses a for loop to execute multiple queries selecting one line each instead of LIMIT 0,5. GENIUS.

Thursday, March 27, 2014

Trust me, it's magic!

You know, something's fscking wrong with the code when you find out that the longest class is actually called Magic.


Unconditonally loving WHERE

What if we glue the query string's segments together as a future condition string in a WHERE clause of an SQL statement, but there are no conditions set? Easy. Just substr($cond,0,-5) , today's legacy code tells. But what if it's followed by a space? No problem, check it five different times like this: if( isset($cond) && ($cond!='W') && $cond!=''). Check again right before building the query, if($cond=='W') $cond =''; .

The Byte Order Mark

Internal Server Error. The number one symptom of messed up .htaccess.

When you're debugging a longer .htaccess with all of its tasty regular expressions and hard-to-figure-out stuff. Minutes, hours pass, the code is completely rewritten, but does not work. Asking for the help of audience, posting it on Stackoverflow looking for possible answers. Nothing. For some reason replacing the code with one single line even thinking about RewriteEngine being corrupt. It does not work. "Fsck this sht" - deleted the file. A short while later got courage to face the problem again. Created a file. Pasted the code. Works. Wow. Curiousity takes over me, I must find the reason somehow. From the backups, the mystery is now solved. The code was made with an ancient junky editor, that added a Byte Order Mark, and Mr. Apache couldn't defeat it.

Deny From All

Given some legacy code (admin surface) doing AJAX calls. The error either could be in the file that calls AJAX and the file being called. That's for sure. Or is it? Checked both, rewritten all PHP without any magic effect. Turns out, that /controller/ folder had a crucial .htaccess with three magic words: Deny From All, which restricted access to everything in that folder (except includes and requires), including the precious script that should have been called.

How to assign a boolean value properly

I pray to this sublime line since the time I found it. Mostly not to take away my life.

($loggedin) ? $loggedin = true : $loggedin = false;

The mysterious case of custom web framework

Few weeks ago I've come across one of the most annoying debugging sessions of all time. The task was to change few things and implement a custom PHP framework having the code provided. The project should have been live on Monday, and I've added new functionality on Friday, so I had plenty of time. On Monday morning I've uploaded the final version of the improved system with some more fast changes in the code.

But it didn't work. The most annoying after all, there were no error messages. I rarely get no error messages at all, even with xdebug turned on. No messages typically indicate a missing semicolon or an unneccessary opening curly bracket. But not in this case. Since everything worked by Friday, the error must be in the few lines I just added, so I started analyzing the new stuff.

I decomposed the new functions into smaller parts, smaller methods, essential fragments, as always. Finally I said, it must be dependency problem, something with the classes these new functions are built on. Debugging all classes took a lot of time, but still found nothing. Couldn't resurrect the code. It was Tuesday, deadline yesterday, what should I do... Continued debugging the parent classes of these, got rid of many unneccessary lines, but added way too much more.

By Thursday I had investigated and rewritten everything. Still no good result. Taking deep breaths, I started to look at the code again being pretty sure that the code is right. After a while it just popped up in my mind. The server ran ancient PHP 5.3, while the framework was using the shorthand array syntax first introduced in PHP 5.4.

What's this?

As developers, we have lots of stupendous stories about the time spent debugging legacy code. This blog is designed to highlight some of the most annoying / interesting / hair-rising / hilarious solutions of the past.