I have a PHP function which sets/unsets something (category and tags) in the cookie and then makes a dynamic query based on it:
public function tagged(){
$this->specific_tag = true;
function unset_cookie($cookie_name) {
setcookie($cookie_name, "", time() - 3600, '/');
unset($_COOKIE[$cookie_name]);
}
function set_cookie($cookie_name, $cookie_value) {
setcookie($cookie_name, $cookie_value, 2147483647, '/');
$_COOKIE[$cookie_name] = $cookie_value;
}
if ( isset($_GET['c']) ) {
if ( empty($_GET['c']) ) {
unset_cookie('qanda_questions_category');
} else {
set_cookie('qanda_questions_category', $_GET['c']);
}
}
if ( isset($_GET['t']) ) {
if ( empty($_GET['t']) ) {
unset_cookie('qanda_questions_tag');
} else {
set_cookie('qanda_questions_tag', $_GET['t']);
}
}
$this->consider_category_tag_cookies();
if ( $this->category_cookie || $this->tag_cookie ) {
$query_where = $query_join = '';
if ( $this->category_cookie ) {
$query_where .= "AND qa.category = :c";
$this->parameters[":c"] = $_COOKIE['qanda_questions_category'];
}
if ( $this->tag_cookie ) {
$query_join .= " INNER JOIN qanda_tags qt ON qt.qanda_id = qa.id
INNER JOIN tags ON tags.id = qt.tag_id";
$query_where .= " AND tags.name = :t";
$this->parameters[":t"] = $_COOKIE['qanda_questions_tag'];
}
return $this->index($query_where, $query_join, __FUNCTION__);
} else {
return header("Location: /myweb/questions".make_url_query($_GET, [],['c','t']));
}
}
I've tried so much to write it clean and functional, but it contains lots of if
statements. It bothers me and I think it should be possible to reduce some of them. Do you have any idea?
Do I need to add more function to the code? Or should I chop the code into multiple separated functions? Anyway, how can I write it more cleanly?
1 Answer 1
First suggestion is that you shouldn't write the same code twice.
Your $_GET-if-conditions
can be put into a separate function.
$this->handleCookieByGetParameter('c', 'qanda_questions_category');
$this->handleCookieByGetParameter('t', 'qanda_questions_tag');
public function handleCookieByGetParameter($parameter, $name)
{
if ( isset($_GET[$parameter]) ) {
if ( empty($_GET[$parameter]) ) {
unset_cookie($name);
} else {
set_cookie($name, $_GET[$parameter]);
}
}
}
and then I recommend that you check your error before you handle your other stuff:
if ( !$this->category_cookie && !$this->tag_cookie ) {
return header("Location: /myweb/questions".make_url_query($_GET, [],['c','t']));
// better an RedirectException
}
$query_where = $query_join = '';
if ( $this->category_cookie ) {
$query_where .= "AND qa.category = :c";
$this->parameters[":c"] = $_COOKIE['qanda_questions_category'];
}
if ( $this->tag_cookie ) {
$query_join .= " INNER JOIN qanda_tags qt ON qt.qanda_id = qa.id
INNER JOIN tags ON tags.id = qt.tag_id";
$query_where .= " AND tags.name = :t";
$this->parameters[":t"] = $_COOKIE['qanda_questions_tag'];
}
return $this->index($query_where, $query_join, __FUNCTION__);
my next step would be to look to extract the query code into a separate function, so I could use it for other methods too.