以更整洁的方式重写此代码,不需要太多其他内容';s


Rewrite this code in a neater fashion, without so many else's

我想重写这段代码,而不需要太多"其他代码",但在不检查东西或在不需要时运行查询方面仍然保持高效。

有人能提出一个更好的方法来编写这个函数吗?

public static function fetch($content) {
    products_library::init();
    self::$cache = $cache = url::assetsPath() . '../cache/soldout_cache';
    //check the cache
    if (file_exists($cache)) {
        $cache_date = filectime($cache);
        db::select('date_modified');
        db::orderBy('date_modified DESC');
        db::limit(1);
        $mod_date = db::get('sc_module_products')->fetch(PDO::FETCH_ASSOC);
        if ($mod_date) {
            $mod_date = strtotime('date_modified');
            if ($cache_date >= $mod_date) { //serve the cache
                try {
                    $soldout = filewriter::read($cache);
                    $soldout = unserialize($soldout);
                } catch (Exception $e) {
                    $soldout = self::query();
                }
            }
            else
                $soldout = self::query();
        }
        else
            $soldout = self::query();
    }
    else
        $soldout = self::query();
    $data['items'] = $soldout; // print_r($items); exit;
    $html = view::load('Product_Display', $data, true);
    return $html;
}

感谢

将其重构为返回而不是else语句的方法

private static function getSoldout() {
    self::$cache = $cache = url::assetsPath() . '../cache/soldout_cache';
    //check the cache
    if (!file_exists($cache)) {
      return self::query();
    }
    $cache_date = filectime($cache);
    db::select('date_modified');
    db::orderBy('date_modified DESC');
    db::limit(1);
    $mod_date = db::get('sc_module_products')->fetch(PDO::FETCH_ASSOC);
    if (!$mod_date) {
      return self::query();
    }
    $mod_date = strtotime('date_modified');
    if ($cache_date < $mod_date) {
      return self::query();
    }
    try {
      //serve the cache
      $soldout = filewriter::read($cache);
      $soldout = unserialize($soldout);
      return $soldout;
    } catch (Exception $e) {
      return self::query();
    }
}
public static function fetch($content) {
    products_library::init();
    $soldout = self::getSoldout();
    $data['items'] = $soldout; // print_r($items); exit;
    $html = view::load('Product_Display', $data, true);
    return $html;
}

我不明白这句话,那里有虫子吗?

$mod_date = strtotime('date_modified');

将$soldout设置为NULL。然后删除else $soldout = self::query()语句。

if语句测试$soldout是否为NULL并且为true之后,运行查询。

开关用例块在这里会产生奇迹。您只需要一个指向默认情况的break语句。然而,如果我站在你的立场上,我会尝试重构整个东西,这需要的不仅仅是快速修复。

这样的东西可能会起作用。我不确定所有if内部发生了什么,也不知道为什么你需要这么多,它可能更紧凑。

public static function fetch($content) {
    products_library::init();
    self::$cache = $cache = url::assetsPath() . '../cache/soldout_cache';
    $soldout = self::fetchCache($cache);
    if ($soldout === false)
    {
        $soldout = self::query();
    }
    $data['items'] = $soldout; // print_r($items); exit;
    $html = view::load('Product_Display', $data, true);
    return $html;
}
public static function fetchCache($cache) {
    if (file_exists($cache)) {
        $cache_date = filectime($cache);
        db::select('date_modified');
        db::orderBy('date_modified DESC');
        db::limit(1);
        $mod_date = db::get('sc_module_products')->fetch(PDO::FETCH_ASSOC);
        if ($mod_date) {
            $mod_date = strtotime('date_modified');
            if ($cache_date >= $mod_date) { //serve the cache
                try {
                    $result = filewriter::read($cache);
                    $result = unserialize($soldout);
                    return $result;
                } catch (Exception $e) {
                    return false;
                }
            }
        }
    }
    return false;
}

在我看来,通过在第一次if检查之前将$soldout设置为self::query(),然后删除所有else,就可以将其默认为self::query。可能不起作用,这取决于self::query()的作用。