Friday, December 19, 2008

WTF: Refactoring snippet of the day

<?php
class SortingClassNameHere {
public function __construct($string) {
$this->string = $string;
}

private static $c = null;

public static function cmp($a, $b) {
$r = strnatcasecmp($a[self::$c], $b[self::$c]);
return ($r > 0 ? 1 : ($r < 0 ? -1 : 0));
}

public function process($data) {
self::$c = $this->string;
if (!usort($data, array("SortingClassNameHere", "cmp"))) {
throw new Exception('Unable to sort results.');
}

return $data;
}
}


Hints:
* SortingClassNameHere::cmp() is never called anywhere else in the code base apart from process()
* If you don't know why this is bad, I will shoot you.

Reblog this post [with Zemanta]
Post a Comment