[1.4.0] Wrap up common code in less error-prone functions.
Posted: Wed Jan 27, 2010 4:45 am
In all the controller files I've opened up, I've seen code like this:
Repeated over and over. A simple in-class function like:
(Called with $this->loadDefaultValue('company', '');)
Would potentially allow hundreds of lines of code to be removed from the software, improve overall code readability, and reduce the amount of time needed to test. Of course, there might be URL "route" issues (make sure no one can do route=checkout/guest_step_1/loadDefaultValue).
Code: Select all
if (isset($this->request->post['company'])) {
$this->data['company'] = $this->request->post['company'];
} elseif (isset($this->session->data['guest']['company'])) {
$this->data['company'] = $this->session->data['guest']['company'];
} else {
$this->data['company'] = '';
}
Code: Select all
private function loadDefaultValue($key, $default)
{
if (isset($this->request->post[$key])) {
$this->data[$key] = $this->request->post[$key];
} elseif (isset($this->session->data['guest'][$key])) {
$this->data[$key] = $this->session->data['guest'][$key];
} else {
$this->data[$key] = $default;
}
}
Would potentially allow hundreds of lines of code to be removed from the software, improve overall code readability, and reduce the amount of time needed to test. Of course, there might be URL "route" issues (make sure no one can do route=checkout/guest_step_1/loadDefaultValue).