以更简洁的方式重写此代码,无需太多其他代码

发布于 2024-11-14 02:37:07 字数 1239 浏览 1 评论 0原文

我想重写这段代码,而不需要那么多“其他”,但在不需要时不检查事物或运行查询方面仍然保持高效。

有人可以建议更好的方法来编写这个函数吗?

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;
}

谢谢

I want to rewrite this code without so many "else's", but still keep it efficient in terms of not checking things or running queries if not needed.

Can someone suggest a better way to write this function?

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;
}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鲜肉鲜肉永远不皱 2024-11-21 02:37:07

将其重构为返回而不是 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');

Refactored it into a method that returns instead of else statements

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;
}

I don't understand this line, is there a bug there?

$mod_date = strtotime('date_modified');
旧瑾黎汐 2024-11-21 02:37:07

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

if 语句测试 $soldout 是否为 NULL 且结果正确后运行查询。

Set $soldout to NULL. Then remove the else $soldout = self::query() statement.

After the if statement test $soldout for NULL and it true run the query.

一人独醉 2024-11-21 02:37:07

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

A switch-case block would work wonders here. You'd just have a break statement that would point to a default case. However, if I were in your shoes, I'd attempt to refactor the whole thing, which would take more than a quick fix.

幸福丶如此 2024-11-21 02:37:07

像这样的东西可能会起作用。我不确定所有的 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;
}

Something like this might work. I'm not sure what's happening inside all the ifs and why you need so many, it might be more compact.

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;
}
聽兲甴掵 2024-11-21 02:37:07

在我看来,好像你可以通过在第一个 if 检查之前将其设置为 self::query() 来默认 $soldout 为 self::query() ,然后删除所有其他内容,因此如果条件不匹配,它仍然是 self::query ()。可能不起作用,具体取决于 self::query() 的作用。

Seems to me as if you could default $soldout to be self::query() by setting it to that before the first if check then remove all the elses, so if the conditions doesn't match it will still be self::query(). Might not work depending on what self::query() does.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文